android 中的 InvocationTargetException 和 NoSuchMethodError

InvocationTargetException and NoSuchMethodError in android

当我在 Android 中使用 StorageReference 对象的 putFile 方法时,我得到一个 java.lang.NoSuchMethodError。

我在应用级别 gradle 文件('com.google.firebase:firebase-storage:10.0.1' 和 'com.google.firebase:firebase-database:10.0.1')中有所需的依赖项。

这是错误消息:

06-05 02:28:29.530 15493-15493/packagename A/FirebaseApp: Firebase API initialization failure.
                                                                        java.lang.reflect.InvocationTargetException
                                                                            at java.lang.reflect.Method.invoke(Native Method)
                                                                            at java.lang.reflect.Method.invoke(Method.java:372)
                                                                            at com.google.firebase.FirebaseApp.zza(Unknown Source)
                                                                            at com.google.firebase.FirebaseApp.initializeApp(Unknown Source)
                                                                            at com.google.firebase.FirebaseApp.initializeApp(Unknown Source)
                                                                            at com.google.firebase.FirebaseApp.initializeApp(Unknown Source)
                                                                            at com.google.firebase.provider.FirebaseInitProvider.onCreate(Unknown Source)
                                                                            at android.content.ContentProvider.attachInfo(ContentProvider.java:1737)
                                                                            at android.content.ContentProvider.attachInfo(ContentProvider.java:1712)
                                                                            at com.google.firebase.provider.FirebaseInitProvider.attachInfo(Unknown Source)
                                                                            at android.app.ActivityThread.installProvider(ActivityThread.java:6475)
                                                                            at android.app.ActivityThread.installContentProviders(ActivityThread.java:6070)
                                                                            at android.app.ActivityThread.handleBindApplication(ActivityThread.java:6010)
                                                                            at android.app.ActivityThread.access00(ActivityThread.java:218)
                                                                            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1795)
                                                                            at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                            at android.os.Looper.loop(Looper.java:145)
                                                                            at android.app.ActivityThread.main(ActivityThread.java:6934)
                                                                            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:1404)
                                                                            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1199)
                                                                         Caused by: java.lang.NoSuchMethodError: No static method getNoBackupFilesDir(Landroid/content/Context;)Ljava/io/File; in class Lcom/google/android/gms/common/util/zzw; or its super classes (declaration of 'com.google.android.gms.common.util.zzw' appears in /data/app/packagename-2/split_lib_dependencies_apk.apk:classes19.dex)
                                                                            at com.google.firebase.iid.zzh.zzeK(Unknown Source)
                                                                            at com.google.firebase.iid.zzh.<init>(Unknown Source)
                                                                            at com.google.firebase.iid.zzh.<init>(Unknown Source)
                                                                            at com.google.firebase.iid.zzd.zzb(Unknown Source)
                                                                            at com.google.firebase.iid.FirebaseInstanceId.getInstance(Unknown Source)
                                                                            at java.lang.reflect.Method.invoke(Native Method) 
                                                                            at java.lang.reflect.Method.invoke(Method.java:372) 
                                                                            at com.google.firebase.FirebaseApp.zza(Unknown Source) 
                                                                            at com.google.firebase.FirebaseApp.initializeApp(Unknown Source) 
                                                                            at com.google.firebase.FirebaseApp.initializeApp(Unknown Source) 
                                                                            at com.google.firebase.FirebaseApp.initializeApp(Unknown Source) 
                                                                            at com.google.firebase.provider.FirebaseInitProvider.onCreate(Unknown Source) 
                                                                            at android.content.ContentProvider.attachInfo(ContentProvider.java:1737) 
                                                                            at android.content.ContentProvider.attachInfo(ContentProvider.java:1712) 
                                                                            at com.google.firebase.provider.FirebaseInitProvider.attachInfo(Unknown Source) 
                                                                            at android.app.ActivityThread.installProvider(ActivityThread.java:6475) 
                                                                            at android.app.ActivityThread.installContentProviders(ActivityThread.java:6070) 
                                                                            at android.app.ActivityThread.handleBindApplication(ActivityThread.java:6010) 
                                                                            at android.app.ActivityThread.access00(ActivityThread.java:218) 
                                                                            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1795) 
                                                                            at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                            at android.os.Looper.loop(Looper.java:145) 
                                                                            at android.app.ActivityThread.main(ActivityThread.java:6934) 
                                                                            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:1404) 
                                                                            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1199) 

有时我会收到此错误消息:

06-05 02:42:13.403 15493-15493/packagename E/AndroidRuntime: FATAL EXCEPTION: main
                                                                           Process: packagename, PID: 15493
                                                                           java.lang.NoSuchMethodError: No static method zzyv()Lcom/google/android/gms/common/util/zze; in class Lcom/google/android/gms/common/util/zzh; or its super classes (declaration of 'com.google.android.gms.common.util.zzh' appears in /data/app/packagename-2/split_lib_dependencies_apk.apk:classes19.dex)
                                                                               at com.google.android.gms.internal.zzbqw.<clinit>(Unknown Source)
                                                                               at com.google.firebase.storage.UploadTask.<init>(Unknown Source)
                                                                               at com.google.firebase.storage.StorageReference.putFile(Unknown Source)
                                                                               at packagename.PostActivity.onClick(PostActivity.java:80)
                                                                               at android.view.View.performClick(View.java:5246)
                                                                               at android.widget.TextView.performClick(TextView.java:10620)
                                                                               at android.view.View$PerformClick.run(View.java:21256)
                                                                               at android.os.Handler.handleCallback(Handler.java:739)
                                                                               at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                               at android.os.Looper.loop(Looper.java:145)
                                                                               at android.app.ActivityThread.main(ActivityThread.java:6934)
                                                                               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:1404)
                                                                               at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1199)

错误发生在代码中(当我尝试使用 putFile 时):

StorageReference filepath = mStorage.child("Pictures").child(mImageUri.getLastPathSegment());
filepath.putFile(mImageUri);

经过大量搜索和测试,我发现并非所有 Firebase UI 库都与任何 Firebase 库兼容。

这是列表和问题的解决方案: https://github.com/firebase/FirebaseUI-Android#compatibility-with-firebase--google-play-services-libraries

向下滚动链接页面,答案就在那里。