31

I recently needed to use Google GCM in my project. From its website it is said:

Firebase Cloud Messaging (FCM) is the new version of GCM. It inherits the reliable and scalable GCM infrastructure, plus new features! See the FAQ to learn more. If you are integrating messaging in a new app, start with FCM. GCM users are strongly recommended to upgrade to FCM, in order to benefit from new FCM features today and in the future.

In order to setup a new Firebase application, I've followed the instructions provided at the official website and did the following:

  1. Created a new Firebase app in the web console and got my json from there - after providing the correct package of my app;
  2. At the top level project's gradle file, I've added classpath 'com.google.gms:google-services:3.0.0' in the dependency section;
  3. At my module's gradle file, I've added: compile 'com.google.firebase:firebase-core:9.0.0' compile 'com.google.firebase:firebase-messaging:9.0.0' and in the last line of it, I've added: apply plugin: 'com.google.gms.google-services';

Full Gradle files are provided below:

Top level gradle file:

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
}

Module's gradle file:

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'

When I start my app I get this stacktrace:

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)

Any help solving this problem will be very appreciated! Regards

E. Fernandes
  • 3,889
  • 4
  • 30
  • 48
  • 1
    Which version of Google Play services do you have on the device this is crashing on? – Ian Barber May 23 '16 at 21:34
  • Using this code ```getPackageManager().getPackageInfo("com.google.android.gms", 0 ).versionCode;``` I've got this version: GOOGLE PLAY SERVICE DEVICE VERSION IS: 9083438. I've created a new project in Firebase and Android, just a hello world, and it worked. What makes me believe that some library I've added is messing with something – E. Fernandes May 23 '16 at 22:21
  • 1
    Yeah, its an odd one. Are you putting your app through proguard (and does the crash happen if you don't)? – Ian Barber May 23 '16 at 23:40
  • Yes it did, but I've found where the problem was. My answer is below – E. Fernandes May 24 '16 at 03:16

12 Answers12

27

Well, after struggle a lot I found what was causing my problem. Basically I had two libraries modules - that happened to be of my own - each one of them importing different versions of com.android.support:appcompat-v7.* and google play services. Made all of them import the same version and problem is gone.

E. Fernandes
  • 3,889
  • 4
  • 30
  • 48
  • 9
    Thanks. In my case, I was using `com.google.firebase:firebase-core:9.0.2` but `com.google.android.gms:play-services-gcm:9.2.0`. Downgrading play-services-gcm to 9.0.2 resolved this runtime error for me. – gnuf Jul 05 '16 at 20:55
  • how to check which libraries modules is importing different versions ? – Lyan Dwi Pangestu Aug 09 '16 at 03:56
  • If you are taking them from github you can check their gradle file, otherwise, if you own the library check it in the source code. – E. Fernandes Aug 09 '16 at 04:04
  • 1
    I had com.google.***:9.4.0 on 8 of 9 imports. Changing the last one that I had missed solved it. – riper Aug 15 '16 at 16:35
  • i am using all of same version, but still it gives the same error – Mansuu.... Nov 11 '16 at 05:12
20

I met the same error, solved by upgrade firebase-core:

dependencies {    
    compile 'com.google.firebase:firebase-core:9.0.2'
}
scoding
  • 209
  • 2
  • 3
9

Care to see that all the dependencies should have same version

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', 
    {
      exclude group: 'com.android.support', module: 'support-annotations'
    })
    implementation 'com.android.support:appcompat-v7:25.2.0'
    implementation 'com.android.support:design:25.2.0'
    implementation 'com.android.support:support-v4:25.2.0'
    implementation 'com.google.firebase:firebase-auth:10.0.1'
    implementation 'com.android.support:cardview-v7:25.2.0'
    implementation 'com.google.firebase:firebase-database:10.0.1'
    testCompile 'junit:junit:4.12'
}
geek
  • 51
  • 9
Harpreet Singh
  • 878
  • 1
  • 7
  • 5
3

I faced the Same Issue, but after some r & d and I came to known the problem was in Gradle i.e,

compile "com.android.support:support-v4:+"


then I just removed the + and replaced actual version ie,

compile "com.android.support:support-v4:23.1.0"


then it started working

Sunil KV
  • 763
  • 9
  • 13
1

For me a completely unrelated import caused this error compile 'com.aurelhubert:ahbottomnavigation:1.3.3' When I removed it everything was fine

Jaad Chacra
  • 549
  • 7
  • 12
1

In my case, the problem occurs because i had put "apply plugin: 'com.google.gms.google-services'" inside the dependencies of the modules build.grandle insteand of putting it at the end of the file.

TmCrafz
  • 184
  • 1
  • 12
1

I had the same issue, fixed by upgrading to play-services-auth:10.0.1:

dependencies {
    ......
    //implementation 'com.google.android.gms:play-services-auth:9.0.0'
    implementation 'com.google.android.gms:play-services-auth:10.0.1'
    ....
}
geek
  • 51
  • 9
DàChún
  • 4,751
  • 1
  • 36
  • 39
0

You can check the dependencies with gradle command

./gradlew app:dependencies

As Edgar said ensure that all the dependency libraries has same version. If not then you can exclude that dependency using

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

And add that dependency yourself.

Harish Rana
  • 484
  • 3
  • 8
0

In My case i forgot to add this with in App Level build

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

So i got the FireBase firebase-api-initialization-failure

geek
  • 51
  • 9
RAJESH KUMAR ARUMUGAM
  • 1,560
  • 21
  • 35
0

add compile 'com.google.android.gms:play-services:9.0.0' into your app level build.gradle file

Shaktisinh Jadeja
  • 1,427
  • 1
  • 17
  • 22
0

Me, after struggle all night because my dependencies' version are the same. After adding Firebase Analytics, when I edited the code and run to my device my app crashed with Rejecting re-init on previously-failed class com.google.android.gms... I have to clean project and run again, then it worked fine but will be crashed again if I edit the code.

My problem was caused by "useProguard false" from following this guide https://developer.android.com/studio/build/shrink-code.html

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

but I disabled Instant Run so remove "useProguard false" fixed my problem.

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

More complete answer for newbies. @Edgar is correct, Thanks!

Error: "Firebase API initialization failure" can be found in logcat of Android Monitor is due to incompatible library version. And it can be any libraries that you compile your app with in Project perspective: go to: Your-app-directory/app/build.gradle

In my case I have to match these 2 module version and that solved this error message:

implementation 'com.google.firebase:firebase-core:16.0.1'
implementation 'com.google.firebase:firebase-appindexing:16.0.1'

I attached here the screen shot so you can see all of it.

enter image description here

geek
  • 51
  • 9
Dung
  • 19,199
  • 9
  • 59
  • 54