FirebaseApp name [DEFAULT] already exists
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.
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);
}