FirebaseApp name [DEFAULT] already exists

I try to manually initialize FirebaseAppin the application, but I get 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);
        }
    }

Suppose I set the value firebaseOptionsaccordingly. I expect this to set values ​​for FirebaseApp.

Did I miss something? FirebaseApp Documentation

The default instance of the application is initialized when the FirebaseInitProvider application starts. This is added to the application manifest by merging the Gradle manifest. If the application uses a different build system, the provider must be manually added to the application manifest.

initializeApp (Context, FirebaseOptions) . . , .

   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:135at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256at android.os.Handler.dispatchMessage(Handler.java:102at android.os.Looper.loop(Looper.java:136at android.app.ActivityThread.main(ActivityThread.java:5001at java.lang.reflect.Method.invokeNative(Native Method) 
    at java.lang.reflect.Method.invoke(Method.java:515at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:801at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:617at dalvik.system.NativeStart.main(Native Method) 
+9
5

, . ( "" ) FirebaseApp, FirebaseInitProvider, , . , , . , , , , , .

FirebaseApp.initializeApp() . , :

    FirebaseOptions firebaseOptions = new FirebaseOptions.Builder()
          .setDatabaseUrl("[DATABASE_URL]")
          .setApiKey("API_KEY")
          .setApplicationId("PROJECT_ID").build();

    FirebaseApp myApp = FirebaseApp.initializeApp(getApplicationContext(),firebaseOptions,
        "MyAppName");

FirebaseApp, FirebaseDatabase, FirebaseStorage, FirebaseAuth, FirebaseCrash, FirebaseInstanceId. :

FirebaseDatabase database = FirebaseDatabase.getInstance(myApp);

, , FirebaseInitProvider. , google-services.json, , XML , documentation Google. :

Android , google-services, XML ,

, .

+9

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);
}
+12

FirebaseApp .

: 1. FirebaseInitProvider Android.

<provider
    android:name="com.google.firebase.provider.FirebaseInitProvider"
    android:authorities="${applicationId}.firebaseinitprovider"
    tools:node="remove"
    />

2. ContentProvider, AndroidManifest. FirebaseInitProvider. Firebase , FirebaseOptions.

FirebaseOptions.Builder builder = new FirebaseOptions.Builder()
    .setApplicationId("1:0123456789012:android:0123456789abcdef")
    .setApiKey("your_api_key")
    .setDatabaseUrl("https://your-app.firebaseio.com")
    .setStorageBucket("your-app.appspot.com");
FirebaseApp.initializeApp(this, builder.build());
+3

onCreate():

    ...                                                                    
      if (!checkInternet()) {
        showMessageDialog("Enable Internet connection please.", true);
        finish();

    } else {

        boolean hasBeenInitialized = false;
        List<FirebaseApp> fbsLcl = FirebaseApp.getApps(this);
        for (FirebaseApp app : fbsLcl) {
            if (app.getName().equals("SpeachGAPIMyTest")) {
                hasBeenInitialized = true;
            }
        }

        FirebaseOptions options = new FirebaseOptions.Builder()
                .setApiKey("AIzaSyA6172grz_K_jkhfvXsrUUiug9LwImK3sg26bE")//https://console.developers.google.com/apis/credentials?project=speachgapimytest-72e23
                .setApplicationId("uz.my.speach.gapi")
                .setDatabaseUrl("https://speachgapimytest-72e23.firebaseio.com/")
                .build();
        if (!hasBeenInitialized) 
            fbApp = FirebaseApp.initializeApp(getApplicationContext(), options, "SpeachGAPIMaximTest"/*""*/);
        else
            fbApp = FirebaseApp.getInstance("SpeachGAPIMyTest");
        fbDB = FirebaseDatabase.getInstance(fbApp); 

        startAuthDialog();                                                
        ...


    }
+1

, [DEFAULT] FirebaseApp, , , , :

if(FirebaseApp.getInstance(FirebaseApp.DEFAULT_APP_NAME) != null) {
    return;
}
0

Source: https://habr.com/ru/post/1652726/


All Articles