SimpleJ
SimpleJ

Reputation: 14758

Firebase throwing uninitialized error despite being initialized

I'm trying to setup firebase messaging in an Android app, and from what I understand I need to run FirebaseApp.initializeApp(context) before I do anything else with the firebase classes. The problem I'm having is I'm running initializeApp before trying to get the device token with FirebaseInstanceId.getInstance().getInstanceId(), but firebase is still throwing this error:

09-05 11:16:20.314 24640-24640/com.company.AppName E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.company.AppName, PID: 24640
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.company.AppName/com.company.AppName.MainActivity}: java.lang.IllegalStateException: Default FirebaseApp is not initialized in this process com.company.AppName. Make sure to call FirebaseApp.initializeApp(Context) first.
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2778)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2856)
        at android.app.ActivityThread.-wrap11(Unknown Source:0)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1589)
        at android.os.Handler.dispatchMessage(Handler.java:106)
        at android.os.Looper.loop(Looper.java:164)
        at android.app.ActivityThread.main(ActivityThread.java:6494)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
     Caused by: java.lang.IllegalStateException: Default FirebaseApp is not initialized in this process com.company.AppName. Make sure to call FirebaseApp.initializeApp(Context) first.
        at com.google.firebase.FirebaseApp.getInstance(com.google.firebase:firebase-common@@16.0.1:219)
        at com.google.firebase.iid.FirebaseInstanceId.getInstance(Unknown Source:1)
        at com.company.AppName.MainActivity.onCreate(MainActivity.java:48)
        at android.app.Activity.performCreate(Activity.java:7009)
        at android.app.Activity.performCreate(Activity.java:7000)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1214)

Here is the code that initializes firebase and throws the error (in MainActivity.java onCreate):

FirebaseApp.initializeApp(this);
NativeApp
    .shared()
    .setContext(this)
    .setDeviceToken(FirebaseInstanceId.getInstance().getInstanceId().toString());

If I comment out the setDeviceToken line, no error is thrown. Is initializeApp asynchronous? The Firebase Android setup guide manages to not even mention initializeApp.

Upvotes: 1

Views: 379

Answers (1)

Doug Stevenson
Doug Stevenson

Reputation: 317322

You do not need to call initializeApp if you performed an integration with Firebase using the documented procedure. If you do a standard integration, Firebase will be initialized automatically at app launch. Please read more about this in the API docs for FirebaseApp.

If you didn't perform a standard integration, then it's up to you to call initializeApp at the right time for your use case.

Upvotes: 1

Related Questions