7

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

Roberto Betancourt
  • 2,375
  • 3
  • 27
  • 35
  • Make sure all of your Google Play Services are using the same version number – OneCricketeer Sep 06 '16 at 17:12
  • 2
    Same for me on 5.0.0, but does work on 6.0.0. No gradle files changed (Google Play services are all same version) except for 'com.android.tools.build:gradle:2.2.0'. – Pnemonic Sep 20 '16 at 14:32
  • Facing same issue, It is working with proguard on 5.0.0, but without it crashing. – Puneet Gupta Sep 28 '16 at 11:13
  • Similar issue here http://stackoverflow.com/questions/33161671/java-lang-noclassdeffounderror-com-google-android-gms-internal-zzmp and here http://stackoverflow.com/questions/34592849/java-lang-noclassdeffounderror-com-google-android-gms-common-internal-zzd – S.A.Norton Stanley Sep 28 '16 at 15:51
  • Not really related since my project and app was already configured for Multidex @S.A.NortonStanley – Roberto Betancourt Sep 28 '16 at 16:39

3 Answers3

6

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.

macros013
  • 739
  • 9
  • 10
4

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

IntelliJ Amiya
  • 74,896
  • 15
  • 165
  • 198
alexbirkett
  • 2,604
  • 2
  • 26
  • 30
  • This is based on macros013 answer but I found his solution of overriding dexOptions from the root build.gradle unnecessarily opaque. Other developers are not going look for dex options in the root.gradle! – alexbirkett Feb 24 '17 at 10:55
0

Make sure you're using the updated versions of google.android.gms

Invalidate Caches and then Make changes in your app : gradle file :

implementation 'com.android.support.constraint:constraint-layout:1.1.2'
//noinspection GradleCompatible
//noinspection GradleCompatible
implementation 'com.android.support:appcompat-v7:23.4.0'
implementation 'com.android.support:design:23.4.0'
implementation 'com.google.android.gms:play-services-maps:15.0.1'
implementation 'com.parse.bolts:bolts-tasks:1.4.0'
implementation 'com.parse:parse-android:1.13.0'
implementation 'com.android.support:multidex:1.0.3'

This worked for me

shellym
  • 546
  • 1
  • 5
  • 11