java.lang.NoClassDefFoundError: android/graphics/drawable/Icon

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!

Leave a Comment