NoClassDefFoundError: 安卓/图形/绘制/图标

到目前为止,我得到这个错误只有一个用户,谁使用一个根电话(SM-G900R7 Android 4.4.2)。错误是这样的:

Fatal Exception: java.lang.NoClassDefFoundError: android/graphics/drawable/Icon
at java.lang.Class.getDeclaredMethods(Class.java)
at java.lang.Class.getDeclaredMethods(Class.java:656)
at android.view.ViewDebug.getExportedPropertyMethods(ViewDebug.java:960)
at android.view.ViewDebug.exportMethods(ViewDebug.java:1047)
at android.view.ViewDebug.dumpViewProperties(ViewDebug.java:997)
at android.view.ViewDebug.dumpViewProperties(ViewDebug.java:983)
at android.view.ViewDebug.dumpView(ViewDebug.java:900)
at android.view.ViewDebug.dumpViewHierarchy(ViewDebug.java:870)
at android.view.ViewDebug.dumpViewHierarchy(ViewDebug.java:867)
at android.view.ViewDebug.dumpViewHierarchy(ViewDebug.java:867)
at android.view.ViewDebug.dumpViewHierarchy(ViewDebug.java:867)
at android.view.ViewDebug.dumpViewHierarchy(ViewDebug.java:867)
at android.view.ViewDebug.dumpViewHierarchy(ViewDebug.java:867)
at android.view.ViewDebug.dumpViewHierarchy(ViewDebug.java:867)
at android.view.ViewDebug.dumpViewHierarchy(ViewDebug.java:867)
at android.view.ViewDebug.dump(ViewDebug.java:793)
at android.view.ViewDebug.dispatchCommand(ViewDebug.java:416)
at android.view.ViewRootImpl$W.executeCommand(ViewRootImpl.java:6258)
at android.view.IWindow$Stub.onTransact(IWindow.java:65)
at android.os.Binder.execTransact(Binder.java:404)
at dalvik.system.NativeStart.run(NativeStart.java)

我从来不用 android Graphics 可绘制的。在我的代码中的图标,所有的用法来自 android.support.v4.graphics.drawable.IconCompat,我也从来没有在我的代码中使用该类..。

顺便说一下,我的支持库是 version 26.0.0, my minSdkVersion is 15 targetSdkVersion is 26.

谢谢

10635 次浏览

There are 2 options :

  1. Have you changed the support library version ? this is quite classic library issue when the resources sometimes aren't 'saved' with the same name, or at all. Its not you, its google. Try to use support lib 25, and see if this error still occurs.
  2. Try to clean the project and rebuild. Maybe you're kept with some old library versions in your build folder, and when you build your project it takes old values from it.

This crash related to 25.4.0 version of support library.

Use 25.3.1 version.

Replace

compile 'com.android.support:appcompat-v7:25.4.0'
compile 'com.android.support:support-v4:25.4.0'

With:

compile 'com.android.support:appcompat-v7:25.3.1'
compile 'com.android.support:support-v4:25.3.1'

Update

The issue is fixed in support library 27.0.0. If you update don't forget to change compileSdkVersion 27 as well.

What is happening?

Samsung devices with Android 4.4 crash like this when classes extending View define methods which return or take parameters of types that are not on classpath.

Starting with support library version 25.4.0 AppCompatImageView and AppCompatImageButton incorrectly overrides setImageIcon(Icon) method. Since Icon class was introduced in API 23 the app crashes on Samsung devices with API 19.

Similar thing happens when you try to override View.onApplyWindowInsets(WindowInsets).

Workaround for support library 26.1.0

Until this gets fixed in an official manner, If you're stuck with an older version of the support library, I made a modified version of appcompat-v7 where all traces of setImageIcon methods are removed. This means it won't crash on a Samsung with Android 4.4.

Put this at the bottom of your app's build.gradle:

repositories {
maven { url "https://dl.bintray.com/consp1racy/maven" }
}


configurations.all {
resolutionStrategy.eachDependency { details ->
def requested = details.requested
if (requested.group == 'com.android.support' && requested.name == 'appcompat-v7') {
details.useTarget 'net.xpece.android:support-appcompat-v7-fixed:26.1.0-1'
}
}
}

This code will replace appcompat-v7 dependency with the described modified artifact.

Currently the only supported version of the fix is 26.1.0.

Warning: Understand the code before copy-pasting, and always exercise caution when getting code from unknown sources!

This issue was resolved in support library 27.0.0:

Android Gradle Plugin 3.x:

implementation 'com.android.support:appcompat-v7:27.0.0'
implementation 'com.android.support:support-v4:27.0.0'

Android Gradle Plugin 2.x:

compile 'com.android.support:appcompat-v7:27.0.0'
compile 'com.android.support:support-v4:27.0.0'

Note that you will also need to compile against SDK level 27.