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

江枫思渺然 提交于 2019-12-01 01:58:47

问题


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


回答1:


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.




回答2:


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




回答3:


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



来源:https://stackoverflow.com/questions/39354357/java-lang-noclassdeffounderror-when-running-app-with-android-5-1-with-android-st

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!