Skip to content Skip to sidebar Skip to footer

Make Sure To Call Firebaseapp.initializeapp(context) First In Android

I am facing this issue and seen some answers on this site but did not get any proper solution. I have used previous version of Firebase which works fine but when I try to upgrade u

Solution 1:

In your SimpleBlog application class, initialize FirebaseApp in onCreate() method and remove it from RegisterActivity in order to have Firebase initialize into entire application, not just one Activity.

@OverridepublicvoidonCreate() {
    super.onCreate();
    FirebaseApp.initializeApp(this);
}

Also add apply plugin: 'com.google.gms.google-services' at the end of app gradle:

dependencies {
    ....
}

apply plugin: 'com.google.gms.google-services'

Plugin is required to process your json config from firebase and to avoid dependency collisions. You can read here for more details.

Solution 2:

Spent literally a whole day. Finally, this was the solution.

Freaking 0 instead of 1 and you will not have to use the initialize firebaseapp or anything like that.

In Project gradle, use Google services: 4.0.0 and not 4.1.0.

Plus, apply plugin statement at the end is also not necessary in my experience.

(Hoping you have added firebase database from the tools => firebase assistant. Step 1 and step 2 are right and green. )

Solution 3:

According to FirebaseApp documentation, you do not need to invoke this initialization, except that your app requires access to another Firebase project.

I invoked this initialization and sometime, user gets crash time to time when user opens my app, so I remove this line of code then everything works well. Be careful to invoke this initialization.

The capture of FirebaseApp documentation:

enter image description here

Update: below Exception will occur if you try to add this line [Some ads network requires this line, they also add some process in their AndroidManifest]

Failed to gain exclusive lock to the Firestore client's offline persistence. This generally means you are using Firestore from multiple processes in your app. Keep in mind that multi-process Android apps execute the code in your Application class in all processes, so you may need to avoid initializing Firestore in your Application class. If you are intentionally using Firestore from multiple processes, you can only enable offline persistence (i.e. call setPersistenceEnabled(true)) in one of them.

To fix it:

1) add below method in your Application class:

privatebooleanisMainProcess(Context context) {
    if (null == context) {
        returntrue;
    }
    ActivityManagermanager= (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);

    intpid= android.os.Process.myPid();
    for (ActivityManager.RunningAppProcessInfo processInfo : manager.getRunningAppProcesses()) {
        if (APPLICATION_ID.equals(processInfo.processName) && pid == processInfo.pid) {
            returntrue;
        }
    }
    returnfalse;
}

2) Wrap onCreate of your Application

@OverridepublicvoidonCreate() {
    super.onCreate();
    if (!isMainProcess(this)) {
        FirebaseApp.initializeApp(this);
        FirebaseFirestoreSettingssettings=newFirebaseFirestoreSettings.Builder()
                .setPersistenceEnabled(false)
                .build();
        FirebaseFirestore.getInstance().setFirestoreSettings(settings);
        // other thingsreturn;
    }
    // other things
}

UPDATE: Sometime, my app throws below exception

Unable to create application My Application Class : java.lang.IllegalStateException: Default FirebaseApp is not initialized in this process MY_APPLICATION_ID. Make sure to call FirebaseApp.initializeApp(Context) first.

To Fix It: let update onCreate method:

@OverridepublicvoidonCreate() {
    super.onCreate();
    FirebaseApp.initializeApp(this);
    booleanisMain= isMainProcess(this);
    FirebaseFirestoreSettingssettings=newFirebaseFirestoreSettings.Builder().setPersistenceEnabled(isMain).build();
    FirebaseFirestore.getInstance().setFirestoreSettings(settings);
    if (!isMain) {
        // other thingsreturn;
    }
    // other things
}

Solution 4:

Got same problem and solved this way:

in activity:

@OverridepublicvoidonCreate() {
    super.onCreate();
    FirebaseApp.initializeApp(this);
}

in app's gradle (at the end of file):

dependencies {
    ....
}

apply plugin: 'com.google.gms.google-services'

is project's gradle:

   dependencies {
        classpath 'com.android.tools.build:gradle:3.0.1'
        classpath 'com.google.gms:google-services:3.0.0'
}

Solution 5:

  1. I had this same issue some time ago.

    You're trying to get an instance of Firebase without initialize it. Please add this line of code before you try to get an instance of Firebase:

    Just import latest version firebase and google play services.

    Recent update version (example):

add to these lines build.gradle

dependencies {

    implementation 'com.google.firebase:firebase-messaging:17.3.4'
    implementation 'com.google.firebase:firebase-core:16.0.7'
    implementation 'com.firebase:firebase-jobdispatcher:0.8.5'


}
apply plugin: 'com.google.gms.google-services'



dependencies {
    classpath 'com.android.tools.build:gradle:3.3.1'
    classpath 'com.google.gms:google-services:4.2.0'// NOTE: Do not place your application dependencies here; they belong// in the individual module build.gradle files
}

thats all. Enjoy your coding.

Post a Comment for "Make Sure To Call Firebaseapp.initializeapp(context) First In Android"