Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

java.lang.NoClassDefFoundError when running app with Android 5.1 with Android Studio 2.2RC

My app is building and running ok when using devices with Android 6.0 or Android 7.0 but when running any device with Android 5.1 (Haven't tested lower) it fails with the following exception:

09-06 11:50:46.100 29601-29601/? E/AndroidRuntime: FATAL EXCEPTION: main
                                               Process: me.myapp.main, PID: 29601
                                               java.lang.NoClassDefFoundError: Failed resolution of: Lcom/google/android/gms/common/internal/zzab;
                                                   at com.google.firebase.provider.FirebaseInitProvider.zza(Unknown Source)
                                                   at com.google.firebase.provider.FirebaseInitProvider.attachInfo(Unknown Source)
                                                   at android.app.ActivityThread.installProvider(ActivityThread.java:5084)
                                                   at android.app.ActivityThread.installContentProviders(ActivityThread.java:4679)
                                                   at android.app.ActivityThread.handleBindApplication(ActivityThread.java:4619)
                                                   at android.app.ActivityThread.access$1500(ActivityThread.java:155)
                                                   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1378)
                                                   at android.os.Handler.dispatchMessage(Handler.java:102)
                                                   at android.os.Looper.loop(Looper.java:135)
                                                   at android.app.ActivityThread.main(ActivityThread.java:5343)
                                                   at java.lang.reflect.Method.invoke(Native Method)
                                                   at java.lang.reflect.Method.invoke(Method.java:372)
                                                   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:905)
                                                   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:700)
                                                Caused by: java.lang.ClassNotFoundException: Didn't find class "com.google.android.gms.common.internal.zzab" on path: DexPathList[[zip file "/data/app/me.myapp.main-1/base.apk"],nativeLibraryDirectories=[/data/app/me.myapp.main-1/lib/arm, /vendor/lib, /system/lib]]
                                                   at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)
                                                   at java.lang.ClassLoader.loadClass(ClassLoader.java:511)
                                                   at java.lang.ClassLoader.loadClass(ClassLoader.java:469)
                                                   at com.google.firebase.provider.FirebaseInitProvider.zza(Unknown Source) 
                                                   at com.google.firebase.provider.FirebaseInitProvider.attachInfo(Unknown Source) 
                                                   at android.app.ActivityThread.installProvider(ActivityThread.java:5084) 
                                                   at android.app.ActivityThread.installContentProviders(ActivityThread.java:4679) 
                                                   at android.app.ActivityThread.handleBindApplication(ActivityThread.java:4619) 
                                                   at android.app.ActivityThread.access$1500(ActivityThread.java:155) 
                                                   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1378) 
                                                   at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                   at android.os.Looper.loop(Looper.java:135) 
                                                   at android.app.ActivityThread.main(ActivityThread.java:5343) 
                                                   at java.lang.reflect.Method.invoke(Native Method) 
                                                   at java.lang.reflect.Method.invoke(Method.java:372) 
                                                   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:905) 
                                                   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:700) 
                                                Suppressed: java.lang.ClassNotFoundException: com.google.android.gms.common.internal.zzab
                                                   at java.lang.Class.classForName(Native Method)
                                                   at java.lang.BootClassLoader.findClass(ClassLoader.java:781)
                                                   at java.lang.BootClassLoader.loadClass(ClassLoader.java:841)
                                                   at java.lang.ClassLoader.loadClass(ClassLoader.java:504)
                                                        ... 15 more
                                                Caused by: java.lang.NoClassDefFoundError: Class not found using the boot class loader; no stack available

I've made sure that i have included all the necessary libraries in my gradle file (In this case, the log shows a google library but it has happened with others). I've also made sure that Multidex is enabled and tried cleaning and rebuilding the project as well as deleting the app and reinstalling.

I've isolated some conditions that may be causing the crash:

  • Instant Run is disabled
  • Building the app for multiple devices

I'm using Android Studio 2.2RC

Edit: Still happening with the stable 2.2

like image 427
Roberto Betancourt Avatar asked Sep 06 '16 17:09

Roberto Betancourt


2 Answers

Here what we can see in runtime/dex_file.cc

bool DexFile::OpenFromZip(...) {
    ...
    while (i < 100) {
        std::string name = StringPrintf("classes%zu.dex", i)
        ...
    }
    ...
}

So if you have more than 100 dex files you get this NoClassDefFoundError.

There are issues in the tracker for this behavior:

https://code.google.com/p/android/issues/detail?id=234367 https://code.google.com/p/android/issues/detail?id=170485

It is possible to avoid this error by disabling pre-dexing. So you can put something like

subprojects {
    project.plugins.whenPluginAdded { plugin ->
        if ("com.android.build.gradle.AppPlugin".equals(plugin.class.name)) {
            project.android.dexOptions.preDexLibraries = false
        } else if ("com.android.build.gradle.LibraryPlugin".equals(plugin.class.name)) {
            project.android.dexOptions.preDexLibraries = false
        }
    }
}

in the root build.gradle file.

like image 82
macros013 Avatar answered Sep 28 '22 00:09

macros013


Android 5.1 has an arbitrary limit of 100 dex files

One possible workaround is to disable preDexLibraries which reduces the number of classes.dex files included in an apk.

Add

android {
    dexOptions {
        preDexLibraries false
    }
}

to the app's build.gradle file

like image 42
alexbirkett Avatar answered Sep 28 '22 00:09

alexbirkett