java.lang.NoClassDefFoundError: Failed resolution of: Landroid/support/v4/util/ArrayMap

java.lang.NoClassDefFoundError: Failed resolution of: Landroid/support/v4/util/ArrayMap

我的应用程序在 API 23 - 25 上 运行 时完美运行,但在 API 21 & 22 上它崩溃并出现以下错误:

12-12 15:01:18.436 27069-27069/com.platinum.hydro E/AndroidRuntime: FATAL EXCEPTION: main
   Process: com.platinum.hydro, PID: 27069
   java.lang.NoClassDefFoundError: Failed resolution of: Landroid/support/v4/util/ArrayMap;
       at com.google.firebase.FirebaseApp.<clinit>(Unknown Source)
       at com.google.firebase.provider.FirebaseInitProvider.onCreate(Unknown Source)
       at android.content.ContentProvider.attachInfo(ContentProvider.java:1751)
       at android.content.ContentProvider.attachInfo(ContentProvider.java:1726)
       at com.google.firebase.provider.FirebaseInitProvider.attachInfo(Unknown Source)
       at android.app.ActivityThread.installProvider(ActivityThread.java:5319)
       at android.app.ActivityThread.installContentProviders(ActivityThread.java:4893)
       at android.app.ActivityThread.handleBindApplication(ActivityThread.java:4833)
       at android.app.ActivityThread.access00(ActivityThread.java:178)
       at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1531)
       at android.os.Handler.dispatchMessage(Handler.java:111)
       at android.os.Looper.loop(Looper.java:194)
       at android.app.ActivityThread.main(ActivityThread.java:5631)
       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:959)
       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:754)
    Caused by: java.lang.ClassNotFoundException: Didn't find class "android.support.v4.util.ArrayMap" on path: DexPathList[[zip file "/data/app/com.platinum.hydro-2/base.apk"],nativeLibraryDirectories=[/data/app/com.platinum.hydro-2/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.FirebaseApp.<clinit>(Unknown Source) 
       at com.google.firebase.provider.FirebaseInitProvider.onCreate(Unknown Source) 
       at android.content.ContentProvider.attachInfo(ContentProvider.java:1751) 
       at android.content.ContentProvider.attachInfo(ContentProvider.java:1726) 
       at com.google.firebase.provider.FirebaseInitProvider.attachInfo(Unknown Source) 
       at android.app.ActivityThread.installProvider(ActivityThread.java:5319) 
       at android.app.ActivityThread.installContentProviders(ActivityThread.java:4893) 
       at android.app.ActivityThread.handleBindApplication(ActivityThread.java:4833) 
       at android.app.ActivityThread.access00(ActivityThread.java:178) 
       at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1531) 
       at android.os.Handler.dispatchMessage(Handler.java:111) 
       at android.os.Looper.loop(Looper.java:194) 
       at android.app.ActivityThread.main(ActivityThread.java:5631) 
       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:959) 
       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:754) 
    Suppressed: java.lang.ClassNotFoundException: android.support.v4.util.ArrayMap
       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)
            ... 18 more
    Caused by: java.lang.NoClassDefFoundError: Class not found using the boot class loader; no stack available

只是将评论移到答案中。

9.8.0 版本的 firebase 库已过时,可能无法与其他支持库一起正常工作。

尝试使用当前版本。 (10.0.1 截至此答案)。您还需要相应地更新其他 Play 服务依赖项

其实,这个问题还有一个原因: 当我们使用 gradle 构建时:

compile 'com.google.android.gms:play-services:xxxx'

我们不得不将整个 API 包编译到我们的 app.In 一些案例中,这样做使得我们更难保持应用程序中的方法数量(包括框架 API、库方法和您自己的方法)代码)在 65,536 的限制下。 所以解决方案:我们应该只使用我们使用的包来构建: 示例:

compile 'com.google.android.gms:play-services-analytics:x.x.x'
compile 'com.google.android.gms:play-services-gcm:x.x.x'

更多详情:https://developers.google.com/android/guides/setup

我遇到了同样的问题并通过将这些依赖项添加到 android/app/build.gradle 文件解决了它:

dependencies {
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.2'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
    implementation 'com.google.firebase:firebase-analytics:17.2.0'
    // I had to add the following to fix this error.
    implementation 'com.google.firebase:firebase-auth:19.0.0'
    implementation 'com.google.firebase:firebase-firestore:21.1.1'
}