Search code examples
androidandroid-multidex

Android 4.x NoClassDefFoundError with Multidex fully implemented


I recently added another dependency to my app which pushed it over the 64k limit and required Multidex. When running on Android 4.x the app now crashes on startup with a NoClassDefFoundError:

FATAL EXCEPTION: main
Process: org.naturenet.debug, PID: 3136
java.lang.NoClassDefFoundError: org.naturenet.util.ForestFire
at org.naturenet.NatureNetApplication.onCreate(NatureNetApplication.java:60)

Other cases of this error suggest that it is because Multidex is not fully implemented, but this still happens with all of the following changes:

app build file

dependencies {
    compile 'com.android.support:multidex:1.0.1'
    compile 'com.android.support:appcompat-v7:25.1.0'
    compile 'com.android.support:support-v4:25.1.0'
    ...
}

android {
    compileSdkVersion 25
    buildToolsVersion "25.0.2"
    defaultConfig {
        applicationId "org.naturenet"
        minSdkVersion 16
        targetSdkVersion 23
        multiDexEnabled true
        jackOptions {
            enabled true
            additionalParameters('jack.incremental': 'true')
        }
    }
}

Application class

public class NatureNetApplication extends MultiDexApplication {

The ForestFire class in question has not changed. I originally had different implementations under the debug and release source trees, but replacing those with a single file in the main source tree made no difference.


Solution

  • I also had the same issue on Android 4.* devices and it somehow was connected with using Jack toolchain.

    But Jack was deprecated and Android Studio 2.4 Preview 6 was released:

    Java 8 language features are now supported by the Android build system in the javac/dx compilation path. Android Studio's Gradle plugin now desugars Java 8 class files to Java 7-compatible class files, so you can use lambdas, method references and other features of Java 8.

    See this for more info.

    Now you can install Android Studio Preview and remove jackOptions from build.gradle.