5

I added firebase to my android project to use firebase cloud messaging. I followed the documentation and I didn't find any instruction to call FirebaseApp.initializeApp().

My app works fine, except for once it crashed with following error.

Caused by: java.lang.IllegalStateException: Default FirebaseApp is not initialized in this process com.my.app. Make sure to call FirebaseApp.initializeApp(Context) first.
at com.google.firebase.FirebaseApp.getInstance(Unknown Source)
at com.google.firebase.iid.FirebaseInstanceId.getInstance(Unknown Source)
at com.my.app.core.ApplicationEx.onCreate(ApplicationEx.java:79)

When I searched for the error, the resolution given is to call FirebaseApp.initializeApp() at the startup.

I am wondering whether this is really necessary, since documentation didn't mention it and my app worked (mostly) fine without it.

Does anyone know whether calling FirebaseApp.initializeApp() is really necessary, and what else could have caused the error I mentioned above?

Following is my build.gradle

apply plugin: 'com.android.application'

android {
    compileSdkVersion 26
    defaultConfig {
        applicationId "com.my.app"
        minSdkVersion 17
        targetSdkVersion 26
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
        multiDexEnabled true
    }
    flavorDimensions "appType"
    productFlavors {
        passenger {
            dimension "appType"
            applicationId "com.my.app.passenger"
            versionCode 1
            versionName "1"
        }
        driver {
            dimension "appType"
            applicationId "com.my.app.driver"
            versionCode 1
            versionName "1"
        }
        admin {
            dimension "appType"
            applicationId "com.my.app.admin"
            versionCode 1
            versionName "1"
        }
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
        debug {
            testCoverageEnabled true
        }
        packagingOptions {
            exclude 'META-INF/ASL2.0'
            exclude 'META-INF/LICENSE'
            exclude 'META-INF/NOTICE'
            exclude 'META-INF/NOTICE.txt'
            exclude 'META-INF/LICENSE.txt'
            exclude 'META-INF/MANIFEST.MF'
        }
    }
}

repositories {
    maven { url 'https://oss.sonatype.org/content/repositories/snapshots' }
}

dependencies {
    implementation project(path: ':cards')
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation "com.android.support:design:${supportVersion}"
    implementation "com.android.support:support-v4:${supportVersion}"
    implementation "com.android.support:appcompat-v7:${supportVersion}"
    implementation "com.android.support:cardview-v7:${supportVersion}"
    implementation "com.android.support:gridlayout-v7:${supportVersion}"
    implementation "com.google.android.gms:play-services-maps:${googlePlayServicesVersion}"
    implementation "com.google.android.gms:play-services-location:${googlePlayServicesVersion}"
    implementation "com.google.android.gms:play-services-places:${googlePlayServicesVersion}"
    implementation "com.google.android.gms:play-services-gcm:${googlePlayServicesVersion}"
    implementation "com.google.android.gms:play-services-ads:${googlePlayServicesVersion}"
    implementation "com.google.android.gms:play-services-auth:${googlePlayServicesVersion}"
    implementation 'com.google.maps:google-maps-services:0.2.5'
    implementation "com.google.firebase:firebase-messaging:${googlePlayServicesVersion}"
    implementation "com.loopj.android:android-async-http:${asyncHttpVersion}"
    implementation "com.android.support.test.espresso:espresso-idling-resource:${espressoVersion}"
    implementation 'com.android.support:multidex:1.0.2'
    implementation 'com.android.support.constraint:constraint-layout:1.0.2'
    implementation 'org.slf4j:slf4j-api:1.7.25'
    implementation 'com.github.tony19:logback-android-core:1.1.1-6'
    implementation 'ch.acra:acra:4.9.2'
    implementation('com.github.tony19:logback-android-classic:1.1.1-6') {
        exclude group: 'com.google.android', module: 'android'      // workaround issue #73
    }
    testImplementation 'org.testng:testng:6.9.6'
    testImplementation 'org.mockito:mockito-core:1.10.19'
    testImplementation 'org.powermock:powermock-api-mockito:1.6.5'
    testImplementation 'org.powermock:powermock-module-junit4-rule-agent:1.6.5'
    testImplementation 'org.powermock:powermock-module-junit4-rule:1.6.5'
    testImplementation 'org.powermock:powermock-module-junit4:1.6.5'
    androidTestImplementation "com.android.support:support-annotations:${supportVersion}"
    androidTestImplementation 'com.android.support.test:runner:1.0.1'
    androidTestImplementation 'com.android.support.test:rules:1.0.1'
    androidTestImplementation 'org.testng:testng:6.9.6'
    androidTestImplementation 'org.mockito:mockito-core:1.10.19'
    androidTestImplementation 'com.google.dexmaker:dexmaker:1.2'
    androidTestImplementation 'com.google.dexmaker:dexmaker-mockito:1.2'
    androidTestImplementation 'com.android.support.test.uiautomator:uiautomator-v18:2.1.3'
    androidTestImplementation("com.android.support.test.espresso:espresso-core:${espressoVersion}", {
        exclude group: 'com.android.support', module: 'support-annotations'
    })
}

apply plugin: 'com.google.gms.google-services'
Lahiru Chandima
  • 22,324
  • 22
  • 103
  • 179
  • Please show your build.gradle. – Doug Stevenson Dec 12 '17 at 01:34
  • @DougStevenson, edited question – Lahiru Chandima Dec 12 '17 at 01:38
  • Do you have different processes defined for different components in your manifest? What's in `com.my.app.core.ApplicationEx`? – Doug Stevenson Dec 12 '17 at 01:42
  • @DougStevenson, The app has three flavors. `ApplicationEx` is a subclass of `android.app.Application`, which is specified as the application class in manifest. – Lahiru Chandima Dec 12 '17 at 02:33
  • I gathered that much, but I'm asking about how you've defined some of your app components in the manifest. Are you using any processes other than the main process? What's the contents of ApplicationEx? Your app is crashing in ApplicationEx, so I think it's wise to take a look in there. – Doug Stevenson Dec 12 '17 at 02:36
  • In `ApplicationEx.onCreate()`, I try to get the messaging token (`FirebaseInstanceId.getInstance().getToken()`) and send it to the backend, so that the backend can register the token to a messaging topic. I do not start any other processes myself, but the crash reporting library I use `ACRA` uses another process. – Lahiru Chandima Dec 12 '17 at 02:43

2 Answers2

1

The Firebase SDKs generally don't support the use of processes other than the main process. If and when ACRA kicks in and starts another process, its own process will create a new Application subclass for that process. This is because every app process must have at exactly one Application object instantiated.

What this means for your app is that this other process should never use Firebase APIs. This means you'll need to find another place to get that IID token.

(Note that the Firebase SDKs are automatically initialized by a ContentProvider that's merged into your app by default - you should never have to call FirebaseApp.initializeApp() unless you've removed this ContentProvider or you aren't using the google-services plugin.)

Typically, when apps need to get the IID token, they create a subclass of FirebaseInstanceIdService, as described in the documentation. This service is notified every time a new token is known. That's the place where you should be retrieving it and sending it to your server.

Doug Stevenson
  • 297,357
  • 32
  • 422
  • 441
  • Thanks for the answer. Can you please clarify following: 1) Shouldn't I call `FirebaseInstanceId.getInstance().getToken()` first, for the `FirebaseInstanceIdService` to get notified about the token refresh? If so, where should I put it instead of `ApplicationEx`? 2) What could be the reason the app doesn't always crash? 3) Calling `FirebaseApp.initializeApp()` is not necessary according to your answer? – Lahiru Chandima Dec 12 '17 at 13:31
  • 1) No. As I said, the documentation says how and where you should get the token. 2) The app doesn't always crash because I suspect ACRA doesn't always kick in. 3) It's not necessary unless you have the circumstances that I said, which are rare. – Doug Stevenson Dec 12 '17 at 16:11
0

Step 1:

Download google-services.json from firebase project where you will see your firebase project keys and values

"project_info": {
    "project_number": "XXXXXXXXXXXX",
    "firebase_url": "https://xxxxxxx-XXXXXX.firebaseio.com",
    "project_id": "xxxxxx-XXXXX",
    "storage_bucket": "xxxxxx-XXXXX.appspot.com"
 }

Step 2: In your application class your can make a entry of Firebase

private FirebaseDatabase database;

public void onCreate() {
        super.onCreate();
        FirebaseApp.initializeApp(this);
        database = FirebaseDatabase.getInstance();
}

// by calling this method wherever you can use firebase database object for further operations.

public FirebaseDatabase getDataBase(){
        return database;
    }

Step 3: In your project level build.gradle

dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
        exclude group: 'com.android.support', module: 'support-annotations'
    }) 
compile 'com.google.firebase:firebase-messaging:11.4.0'
    compile 'com.google.firebase:firebase-database:11.4.0' 
}

apply plugin: 'com.google.gms.google-services'

Step 4:
In your project level build.gradle

dependencies {
    classpath 'com.android.tools.build:gradle:2.3.3'
        classpath 'com.google.gms:google-services:3.1.0'
        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }

allprojects {
    repositories {
        jcenter()
        maven { url "https://maven.google.com" }// Google's Maven repository
    }
}

and for further issues please read DOCUMENTATION

yashkal
  • 713
  • 5
  • 20