How to check if a Firebase App is already initialized on Android

72,499

Solution 1

On firebase web, you check if already initialized with:

if (firebase.apps.length === 0) {
    firebase.initializeApp({});
}

Solution 2

In v9, Firebase has been modularized for better tree shaking. So we can no longer import entire app and check the apps property AFAIK. The below approach can be used instead.

import { initializeApp, getApps, getApp } from "firebase/app";
getApps().length === 0 ? initializeApp(firebaseConfig) : getApp();

https://firebase.google.com/docs/reference/js/v9/app.md#getapps for documentation

Solution 3

For those wondering how to do the same as the accepted answer, in Android:

if (FirebaseApp.getApps(context).isEmpty()) {
    FirebaseApp.initializeApp(context);
}

and in an instrumented test environment, use this context:

InstrumentationRegistry.getContext()

Solution 4

Firebase Version 9

import { initializeApp, getApp } from "firebase/app";

const createFirebaseApp = (config = {}) => {
  try {
    return getApp();
  } catch () {
    return initializeApp(config);
  }
};

const firebaseApp = createFirebaseApp({/* your config */})

Solution 5

You can try to get the Firebase app instance, in it's code firebase checks if it's initialized, if not it throws an IllegalStateException.

    try{
        FirebaseApp.getInstance();
    }
    catch (IllegalStateException e)
    {
        //Firebase not initialized automatically, do it manually
        FirebaseApp.initializeApp(this);
    }
Share:
72,499
Relm
Author by

Relm

Updated on July 27, 2021

Comments

  • Relm
    Relm almost 3 years

    With the following, the first time it's called it works, but then fails on subsequent calls with "FirebaseApp name [DEFAULT] already exists!"

    public FirebaseDatabase conn(Context c) {
             FirebaseOptions options = new FirebaseOptions.Builder()
                    .setApiKey("key")
                    .setDatabaseUrl("url")
                    .setApplicationId("ID")
                    .build();
    
    
            /////I tried Try and Catch with no success//////
            FirebaseApp app = FirebaseApp.initializeApp(c, options);
    
            /// for this : FirebaseApp app = FirebaseApp.initializeApp(c, options, "some_app");
            //// will fail with "FirebaseApp name some_app already exists!"
            return FirebaseDatabase.getInstance(app);
    }
    

    All of the above is an attempt to connect to a second Firebase App.

  • Vlad
    Vlad over 6 years
    Cool but in android this impossible
  • Anand Rockzz
    Anand Rockzz almost 5 years
  • eoinmullan
    eoinmullan almost 4 years
    I had been using the App returned from initializeApp() but with the above approach this is no longer possible, so I'm using now firebase.app('[DEFAULT]'), but I'm not sure if that's recommended.
  • Pumuckelo
    Pumuckelo almost 3 years
  • cjmling
    cjmling over 2 years
    and in case if you want to get instance in else you can get by firebase.app();
  • Ayush Kumar
    Ayush Kumar over 2 years
    Can you update it to web 9?
  • Levi Wilkerson
    Levi Wilkerson about 2 years
    Thank you. One of the few explanations that make sense for being able to export the firebaseApp variable.
  • user3399180
    user3399180 about 2 years
    I tried this but I get this error - stackoverflow.com/questions/72028182/…