如何解决Firebase API初始化失败(Android + Firebase)

我最近需要在我的项目中使用Google GCM。 从它的网站上说:

Firebase云消息传递(FCM)是GCM的新版本。 它inheritance了可靠和可扩展的GCM基础架构,以及新的function! 查看常见问题以了解更多信息。 如果您要将消息集成到新应用程序中,请从FCM开始。 强烈build议GCM用户升级到FCM,以便从当前和未来的新FCMfunction中受益。

为了设置一个新的Firebase应用程序,我遵循官方网站提供的说明,并执行以下操作:

  1. 在Web控制台中创build一个新的Firebase应用程序,并从那里获得我的JSON – 提供了我的应用程序的正确包;
  2. 在顶层项目的gradle文件中,我在依赖项部分添加了classpath 'com.google.gms:google-services:3.0.0' ;
  3. 在我的模块的gradle文件中,我添加了:compile'c​​om.google.firebase compile 'com.google.firebase:firebase-core:9.0.0' compile 'com.google.firebase:firebase-messaging:9.0.0'并在最后一行,我已经添加了: apply plugin: 'com.google.gms.google-services' ;

下面提供了完整的Gradle文件:

顶级gradle文件:

     buildscript { repositories { jcenter() } dependencies { classpath 'com.android.tools.build:gradle:2.1.0' classpath 'com.neenbedankt.gradle.plugins:android-apt:1.7' classpath 'com.google.gms:google-services:3.0.0' } } allprojects { repositories { jcenter() maven { url "https://jitpack.io" } } } task clean(type: Delete) { delete rootProject.buildDir } 

    模块的gradle文件:

     apply plugin: 'com.android.application' apply plugin: 'com.neenbedankt.android-apt' android { compileSdkVersion 23 buildToolsVersion "23.0.2" dexOptions { javaMaxHeapSize "4g" } defaultConfig { applicationId "br.com.gogame" minSdkVersion 21 targetSdkVersion 23 versionCode 1 versionName "1.0" multiDexEnabled true } buildTypes { release { minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' } } useLibrary 'org.apache.http.legacy' } dependencies { ... compile 'com.google.firebase:firebase-core:9.0.0' compile 'com.google.firebase:firebase-messaging:9.0.0' ... } apply plugin: 'com.google.gms.google-services' 

    当我开始我的应用程序,我得到这个堆栈跟踪:

     05-23 14:04:55.109 13368-13368/br.com.gogame I/art: Rejecting re-init on previously-failed class java.lang.Class<com.google.android.gms.internal.zzqq$zzd> 05-23 14:04:55.109 13368-13368/br.com.gogame I/art: Rejecting re-init on previously-failed class java.lang.Class<com.google.android.gms.internal.zzqq$zzd> 05-23 14:04:55.110 13368-13368/br.com.gogame I/art: Rejecting re-init on previously-failed class java.lang.Class<com.google.android.gms.internal.zzqq$zzc> 05-23 14:04:55.110 13368-13368/br.com.gogame I/art: Rejecting re-init on previously-failed class java.lang.Class<com.google.android.gms.internal.zzqq$zzc> 05-23 14:04:55.126 13368-13368/br.com.gogame E/FirebaseApp: Firebase API initialization failure. java.lang.reflect.InvocationTargetException at java.lang.reflect.Method.invoke(Native Method) 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.zzbu(Unknown Source) at com.google.firebase.provider.FirebaseInitProvider.onCreate(Unknown Source) at android.content.ContentProvider.attachInfo(ContentProvider.java:1748) at android.content.ContentProvider.attachInfo(ContentProvider.java:1723) at com.google.firebase.provider.FirebaseInitProvider.attachInfo(Unknown Source) at android.app.ActivityThread.installProvider(ActivityThread.java:5174) at android.app.ActivityThread.installContentProviders(ActivityThread.java:4769) at android.app.ActivityThread.handleBindApplication(ActivityThread.java:4709) at android.app.ActivityThread.-wrap1(ActivityThread.java) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1415) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:148) at android.app.ActivityThread.main(ActivityThread.java:5443) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:728) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618) Caused by: java.lang.NoSuchMethodError: No static method zzy(Ljava/lang/Object;)Ljava/lang/Object; in class Lcom/google/android/gms/common/internal/zzx; or its super classes (declaration of 'com.google.android.gms.common.internal.zzx' appears in /data/app/br.com.gogame-2/base.apk:classes26.dex) at com.google.android.gms.measurement.internal.zzt.zzaU(Unknown Source) at com.google.android.gms.measurement.AppMeasurement.getInstance(Unknown Source) at java.lang.reflect.Method.invoke(Native Method) 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.zzbu(Unknown Source) at com.google.firebase.provider.FirebaseInitProvider.onCreate(Unknown Source) at android.content.ContentProvider.attachInfo(ContentProvider.java:1748) at android.content.ContentProvider.attachInfo(ContentProvider.java:1723) at com.google.firebase.provider.FirebaseInitProvider.attachInfo(Unknown Source) at android.app.ActivityThread.installProvider(ActivityThread.java:5174) at android.app.ActivityThread.installContentProviders(ActivityThread.java:4769) at android.app.ActivityThread.handleBindApplication(ActivityThread.java:4709) at android.app.ActivityThread.-wrap1(ActivityThread.java) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1415) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:148) at android.app.ActivityThread.main(ActivityThread.java:5443) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:728) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618) 05-23 14:04:55.131 13368-13368/br.com.gogame I/FirebaseInitProvider: FirebaseApp initialization successful 05-23 14:04:55.133 13368-13368/br.com.gogame E/AndroidRuntime: FATAL EXCEPTION: main Process: br.com.gogame, PID: 13368 java.lang.NoSuchMethodError: No static method zzy(Ljava/lang/Object;)Ljava/lang/Object; in class Lcom/google/android/gms/common/internal/zzx; or its super classes (declaration of 'com.google.android.gms.common.internal.zzx' appears in /data/app/br.com.gogame-2/base.apk:classes26.dex) at com.google.android.gms.measurement.internal.zzt.zzaU(Unknown Source) at com.google.android.gms.measurement.AppMeasurementContentProvider.onCreate(Unknown Source) at android.content.ContentProvider.attachInfo(ContentProvider.java:1748) at android.content.ContentProvider.attachInfo(ContentProvider.java:1723) at android.app.ActivityThread.installProvider(ActivityThread.java:5174) at android.app.ActivityThread.installContentProviders(ActivityThread.java:4769) at android.app.ActivityThread.handleBindApplication(ActivityThread.java:4709) at android.app.ActivityThread.-wrap1(ActivityThread.java) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1415) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:148) at android.app.ActivityThread.main(ActivityThread.java:5443) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:728) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618) 

    任何帮助解决这个问题将非常感激! 问候

    Related of "如何解决Firebase API初始化失败(Android + Firebase)"

    那么经过艰苦的努力,我发现了什么导致了我的问题。 基本上我有两个库模块 – 碰巧是我自己的 – 每个人都导入不同版本的com.android.support:appcompat-v7.*和谷歌播放服务。 使他们都import相同的版本,问题不复存在。

    我遇到了同样的错误,通过升级firebase-core解决:

     dependencies { compile 'com.google.firebase:firebase-core:9.0.2' } 

    小心看到所有的依赖应该有相同的版本

     dependencies { compile fileTree(dir: 'libs', include: ['*.jar']) androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', { exclude group: 'com.android.support', module: 'support-annotations' }) compile 'com.android.support:appcompat-v7:25.2.0' compile 'com.android.support:design:25.2.0' compile 'com.android.support:support-v4:25.2.0' compile 'com.google.firebase:firebase-auth:10.0.1' compile 'com.android.support:cardview-v7:25.2.0' compile 'com.google.firebase:firebase-database:10.0.1' testCompile 'junit:junit:4.12' } 

    我面对同样的问题,但经过一些研发和我来了解这个问题是在Gradle即编译“com.android.support:support-v4:+”然后我刚刚删除了+和实际版本,即,编译“com.android.support:support-v4:23.1.0”然后开始工作

    对我来说,一个完全不相关的导入导致这个错误编译“com.aurelhubert:ahbottomnavigation:1.3.3”当我删除它一切都很好

    在我的情况下,出现这个问题的原因是我已经把“apply plugin:'com.google.gms.google-services'”放入了模块build.grandle的依赖关系中,并且把它放在文件末尾。

    你可以用gradle命令检查依赖关系

     ./gradlew app:dependencies 

    正如Edgar所说的,确保所有的依赖库具有相同的版本。 如果没有,那么你可以使用排除依赖关系

     compile('your dependency') { exclude group: 'lib to be removed' } 

    并且自己添加这个依赖关系。

    我遇到了同样的问题,通过升级到play-services-auth来解决:10.0.1:

     dependencies { ...... //compile 'com.google.android.gms:play-services-auth:9.0.0' compile 'com.google.android.gms:play-services-auth:10.0.1' .... } 

    在我的情况下,我忘了添加这个应用程序级别构build

     compile 'com.google.firebase:firebase-messaging:9.6.0' 

    所以我得到了FireBase fi rebase-api-initialization-failure

    compile 'com.google.android.gms:play-services:9.0.0'到应用程序级别的build.gradle文件中

    我整晚奋斗后,因为我的依赖版本是一样的。 添加Firebase Analytics之后,当我编辑代码并运行到我的设备时,我的应用程序崩溃, 在以前失败的类com.google.android.gms上拒绝重新初始化…我必须清理项目并重新运行,然后才能运行好,但如果我编辑代码将再次坠毁。

    我的问题是由以下指南中的“useProguard false”造成的https://developer.android.com/studio/build/shrink-code.html

      debug { minifyEnabled true useProguard false proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' } 

    但我禁用即时运行,所以删除“useProguard false”解决了我的问题。

      debug { minifyEnabled true proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' }