Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

FirebaseApp name [DEFAULT] already exists

Tags:

I'm trying to manually initialize FirebaseApp on Application but getting this error.

public class BaseApplication extends Application {      @Override     public void onCreate() {         super.onCreate();         FirebaseOptions firebaseOptions = new FirebaseOptions.Builder()               .setDatabaseUrl("[DATABASE_URL]")               .setApiKey("API_KEY")               .setApplicationId("PROJECT_ID").build();         FirebaseApp.initializeApp(getApplicationContext(),firebaseOptions);          if (!FirebaseApp.getApps(this).isEmpty()) {             FirebaseDatabase.getInstance().setPersistenceEnabled(true);         }     } 

Assume that I set the firebaseOptions value accordingly. I am expecting that this will set values for FirebaseApp.

Did I missed something? FirebaseApp Documentation

The default app instance is initialized on app startup by FirebaseInitProvider. This is added to the app's manifest by Gradle manifest merging. If the app is using a different build system the provider needs to be manually added to the app's manifest.

Alternatively initializeApp(Context, FirebaseOptions) initializes the default app instance. This method should be invoked from Application. This is also necessary if it is used outside of the application's main process.

   FATAL EXCEPTION: main     Process: com.sample.android, PID: 5490     java.lang.RuntimeException: Unable to create application com.android.tools.fd.runtime.BootstrapApplication: java.lang.IllegalStateException: FirebaseApp name [DEFAULT] already exists!     at android.app.ActivityThread.handleBindApplication(ActivityThread.java:4331)     at android.app.ActivityThread.access$1500(ActivityThread.java:135)     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)     at android.os.Handler.dispatchMessage(Handler.java:102)     at android.os.Looper.loop(Looper.java:136)     at android.app.ActivityThread.main(ActivityThread.java:5001)     at java.lang.reflect.Method.invokeNative(Native Method)     at java.lang.reflect.Method.invoke(Method.java:515)     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:801)     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:617)     at dalvik.system.NativeStart.main(Native Method)     Caused by: java.lang.IllegalStateException: FirebaseApp name [DEFAULT] already exists!     at com.google.android.gms.common.internal.zzab.zza(Unknown Source)     at com.google.firebase.FirebaseApp.initializeApp(Unknown Source)     at com.sample.android.activities.BcodeApplication.onCreate(BcodeApplication.java:21)     at com.android.tools.fd.runtime.BootstrapApplication.onCreate(BootstrapApplication.java:369)     at android.app.Instrumentation.callApplicationOnCreate(Instrumentation.java:1007)     at android.app.ActivityThread.handleBindApplication(ActivityThread.java:4328)     at android.app.ActivityThread.access$1500(ActivityThread.java:135)      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)      at android.os.Handler.dispatchMessage(Handler.java:102)      at android.os.Looper.loop(Looper.java:136)      at android.app.ActivityThread.main(ActivityThread.java:5001)      at java.lang.reflect.Method.invokeNative(Native Method)      at java.lang.reflect.Method.invoke(Method.java:515)      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:801)      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:617)      at dalvik.system.NativeStart.main(Native Method)  
like image 259
yesterdaysfoe Avatar asked Aug 27 '16 22:08

yesterdaysfoe


People also ask

What is firebase initializeApp?

initializeApp. Creates and initializes a Firebase app instance. See Add Firebase to your app and Initialize multiple projects for detailed documentation.


2 Answers

Use this

FirebaseOptions options = new FirebaseOptions.Builder()     .setApiKey(apiKey)     .setApplicationId(appId)     .setDatabaseUrl(firebaseBaseUrl)     .build();  boolean hasBeenInitialized=false; List<FirebaseApp> firebaseApps = FirebaseApp.getApps(mContext); for(FirebaseApp app : firebaseApps){     if(app.getName().equals(FirebaseApp.DEFAULT_APP_NAME)){         hasBeenInitialized=true;         finestayApp = app;     } }  if(!hasBeenInitialized) {     finestayApp = FirebaseApp.initializeApp(mContext, options); } 
like image 117
Riyas PK Avatar answered Oct 15 '22 22:10

Riyas PK


It's not clear what your goal is. If you simply want to modify ("overwrite") the default FirebaseApp that is created by FirebaseInitProvider, I don't think that is possible. The documentation you cited is a little misleading in that it suggests it is possible. I think the documentation is intended to describe how the default app can be initialized when it has not already been created, for example in a secondary process.

You can use FirebaseApp.initializeApp() to create another app object. You need to use the method that accepts a parameter for the app name:

    FirebaseOptions firebaseOptions = new FirebaseOptions.Builder()           .setDatabaseUrl("[DATABASE_URL]")           .setApiKey("API_KEY")           .setApplicationId("PROJECT_ID").build();      FirebaseApp myApp = FirebaseApp.initializeApp(getApplicationContext(),firebaseOptions,         "MyAppName"); 

You can then use the created FirebaseApp to get an instance of FirebaseDatabase, FirebaseStorage, FirebaseAuth, FirebaseCrash, or FirebaseInstanceId. For example:

FirebaseDatabase database = FirebaseDatabase.getInstance(myApp); 

In an app's main process, I don't think there is a simple way to disable the initialization processing done by FirebaseInitProvider. If you want to override the configuration parameters that normally come from google-services.json, you can probably create your own XML file for the parameters using the information in the documentation for the Google Services Plugin. It states:

If your Android project has some configuration that prevents you from using the google-services plugin, you can safely recreate the XML files manually using these values

I don't know how simple or maintainable that is.

like image 32
Bob Snyder Avatar answered Oct 15 '22 21:10

Bob Snyder