Default FirebaseApp is not initialized
Solution 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, in your main function or a FutureBuilder:
FirebaseApp.initializeApp();
Solution 2
Make sure to add to your root-level build.gradle
buildscript {
// ...
dependencies {
// ...
classpath 'com.google.gms:google-services:4.3.2'
}
}
Then, in your module level Gradle file (usually the app/build.gradle), add the 'apply plugin' line at the bottom of the file to enable the Gradle plugin:
apply plugin: 'com.android.application'
android {
// ...
}
dependencies {
// ...
implementation 'com.google.firebase:firebase-core:9.6.1'
// Getting a "Could not find" error? Make sure you have
// the latest Google Repository in the Android SDK manager
}
// ADD THIS AT THE BOTTOM
apply plugin: 'com.google.gms.google-services'
As said in documentation. I had exception as in a question above when forgot to add this in my gradle files.
Solution 3
It seems that google-services:4.1.0
has an issue. Either downgrade it to
classpath 'com.google.gms:google-services:4.0.0'
or upgrade it to
classpath 'com.google.gms:google-services:4.2.0'
dependencies {
classpath 'com.android.tools.build:gradle:3.3.0-alpha08'
classpath 'com.google.gms:google-services:4.2.0'
/*classpath 'com.google.gms:google-services:4.1.0' <-- this was the problem */
}
Hope it helps
Solution 4
UPDATE (11th Nov. 2021):
We do not need to call FirebaseApp.initializeApp(this);
anywhere manually. and we should not too.
Possibility 1:
It seems that com.google.gms:google-services:4.3.9
has an issue.
We can either UPGRADE it to
classpath 'com.google.gms:google-services:4.3.10'
or DOWNGRADE it to
classpath 'com.google.gms:google-services:4.3.8'
dependencies {
classpath 'com.android.tools.build:gradle:4.2.2'
classpath 'com.google.gms:google-services:4.3.9'// <-- this was the problem
}
Possibility 2:
Make sure to add the below line in app/build.gradle file
apply plugin: 'com.google.gms.google-services'
then clean project and run again. It worked for me.
Possibility 3:
I just faced the same issue about it and got an unexpected and strange solution.
From this answer:
I have removed tools:node="replace"
and it's working like charm.
Solution 5
I was missing the below line in my app/build.gradle file
apply plugin: 'com.google.gms.google-services'
and once clean project and run again. That fixed it for me.
Related videos on Youtube
Roy Solberg
Tech lead Android at FotMob. Often seen trading cryptocurrency at Binance. Check out my blog at https://blog.roysolberg.com. :)
Updated on April 02, 2022Comments
-
Roy Solberg over 2 years
We're seeing a few exceptions with the message
Default FirebaseApp is not initialized in this process com.example.app. Make sure to call FirebaseApp.initializeApp(Context) first.
in our Android app in which we just added Firebase Remote Config.The stack trace is as follows:
Fatal Exception: java.lang.IllegalStateException: Default FirebaseApp is not initialized in this process com.example.app. Make sure to call FirebaseApp.initializeApp(Context) first. at com.google.firebase.FirebaseApp.getInstance(Unknown Source) at com.google.firebase.remoteconfig.FirebaseRemoteConfig.getInstance(Unknown Source) at com.example.app.fragments.SomeFragment.updateFooter(SourceFile:295) at com.example.app.fragments.SomeFragment.onCreateView(SourceFile:205) at android.support.v4.app.Fragment.performCreateView(SourceFile:2080) at android.support.v4.app.FragmentManagerImpl.moveToState(SourceFile:1108) at android.support.v4.app.FragmentManagerImpl.moveToState(SourceFile:1290) at android.support.v4.app.BackStackRecord.run(SourceFile:801) at android.support.v4.app.FragmentManagerImpl.execSingleAction(SourceFile:1638) at android.support.v4.app.BackStackRecord.commitNowAllowingStateLoss(SourceFile:679) at android.support.v4.app.FragmentPagerAdapter.finishUpdate(SourceFile:143) at android.support.v4.view.ViewPager.populate(SourceFile:1240) at android.support.v4.view.ViewPager.populate(SourceFile:1088) at android.support.v4.view.ViewPager.setAdapter(SourceFile:542) at com.example.app.SomeActivity.onSomeAsyncCallback(SourceFile:908) at com.example.app.SomeDataRetriever.onAsyncHttpCompleted(SourceFile:72) at com.example.app.io.AsyncHttp.onPostExecute(SourceFile:141) at com.example.app.io.AsyncHttp.onPostExecute(SourceFile:19) at android.os.AsyncTask.finish(AsyncTask.java:679) at android.os.AsyncTask.access$500(AsyncTask.java:180) at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:696) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:150) at android.app.ActivityThread.main(ActivityThread.java:5665) at java.lang.reflect.Method.invoke(Method.java) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:799) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:689)
This is version 9.6.1 and we're also using other Firebase components:
compile 'com.google.firebase:firebase-ads:9.6.1' compile 'com.google.firebase:firebase-config:9.6.1' compile 'com.google.firebase:firebase-invites:9.6.1' compile "com.google.firebase:firebase-messaging:9.6.1"
As I can see from the documentation and the Javadoc we shouldn't have to do any manual initialization in our case.
The exception happens on Android 4-6 on a variety of devices.
Edit:
I see this question gets a little bit of attention. I think this explanation can be interesting for some of you: https://firebase.googleblog.com/2016/12/how-does-firebase-initialize-on-android.html
-
Doug Stevenson over 7 yearsTry searching SO for "Default FirebaseApp is not initialized". There are a lot of different circumstances that might cause this.
-
Roy Solberg over 7 yearsYeah, I have. Didn't find any with matching circumstances, so that's why I'm asking. Could've mentioned that in the Q.
-
Doug Stevenson over 7 yearsAre you able to produce a minimal example the reproduces this problem, and show the manifest, build.gradle, and Activity?
-
Andrew Kelly over 7 yearsDo you initialise Firebase in a ContentProvider or in your Application class?
-
Roy Solberg over 7 yearsWe initialize Firebase in our Application class. We'll move that. However, the app should in theory be a single process app. In that case nothing should be wrong, should it? Looking at the numbers now, the exception has happened to 26 of more than 1 million users that should've run the same callback as in the stack trace.
-
Lumii over 7 yearsHi, may I know do you solve your problem? We have the same problem here too. We are using
com.google.firebase:firebase-core:10.0.1
andFirebaseRemoteConfig.getInstance
. We don't callFirebaseApp.initializeApp
explicitly as it is not stated in documentation. Our code works for most users. Except, we experience similar crash log, for a very few users using Android 4.1 -
Dmitry over 7 yearsI experience the same problem for a small percentage of players. Using the latest 10.0.1
-
Roy Solberg over 7 yearsWe're seeing this in like 0.0025% of our users.. So it isn't a big issue, but it still is a few thousand users.
-
Roy Solberg over 7 years@DougStevenson Any thoughts on why me and dmitry would see this for just a very small part of our user base? You asked for a minimal example to reproduce the problem, but it seems like we need a big user base to reproduce it. Personally I haven't seen this issue on any our devices.
-
Dmitry over 7 years@RoySolberg I read that blog post, but my app is single-process. And I am pretty sure that the most of us would have preferred calling initialiser instead of getting a "magical" bug affecting some users but not happening on our test devices :-(
-
Roy Solberg over 7 years@Dmitry Agreed. I wonder if it could be Firebase (or maybe some other lib we're using) firing up a second process and then our apps wouldn't be single-process any longer.
-
Wahib Ul Haq almost 7 yearsThose who mentioned that they are using
FirebaseRemoteConfig.getInstance()
shouldn't worry because internally.getInstance
does callFirebaseApp var0 = FirebaseApp.getInstance();
. -
CoDe almost 6 yearsbtw, I'm facing same issue while executing test cases for one of FirebaseMessage class component. No issue in actual code functionality, it's working as expected. Any suggestion !
-
Manmohan Soni about 4 yearsWe are also facing this issue just on line - "val firebaseRemoteConfig = FirebaseRemoteConfig.getInstance()"
-
Abhinav Saxena about 4 yearsOnly Firebase assistant can correct the dependencies, and connect the project with the Firebase developers console. It also provides the necessary code.
-
-
Roy Solberg over 7 yearsCheck, and check. Just verified that we the Gradle files as described. I should mention that the code works okay for a lot of users.
-
crakama over 7 yearsIt could be that you are mixing your library versions. I had the same issue when I tried using compile
'com.google.firebase:firebase-core:9.8.0'
so I changed tocompile 'com.google.firebase:firebase-core:9.6.0'
to match my other libraries. Original answer You're mixing Firebase SDK versions. as explained -
Rockyfish over 7 years'apply plugin: 'com.google.gms.google-services' worked for me.
-
Dmitry over 7 yearsDid not help in my case. And the issue affects extremely small percentage of players. So, most likely it is not a problem with the code or settings, but with interoperability with play services installed n the user's devices :-(
-
ultra.deep over 7 yearsthanks :) I forget to add this line in build.gradle : apply plugin: 'com.google.gms.google-services'
-
dabluck over 7 yearswhy does the plugin have to be last?
-
Henrik over 7 yearsDid you add this to an extended Application class and onCreate or in your activity before using an Firebase API?
-
Roy Solberg about 7 years@Henrik Only in ExtendedApplication.onCreate().
-
Roy Solberg almost 7 yearsWhoa, that's some interesting info. With the problem happening to so few ppl it's hard to say if we've seen any of this. Maybe we should try removing it again and using the newer Gradle versions.
-
Sébastien over 6 years@RoySolberg Was this enough to completely solve the issue in your case?
-
Roy Solberg over 6 years@Sébastien Yes. We have not seen the issue since we added that line.
-
Sébastien over 6 yearsWell, this did not work for me (still a few crashes in production). I will try adding the init code to my first Activity onCreate().
-
Gopi.cs over 6 years@Sébastien using in main class with this instead of contexts will resolve the issue give it a try
-
pamobo0609 over 6 yearsIt's important to note that besides this initialization, the google services must be applied as follows:
apply plugin: 'com.google.gms.google-services'
-
abhit almost 6 yearsin which file do i have to add this MainActivity.java?
-
Neri almost 6 yearsThis was the only problem, no need to initialize either
-
iuq almost 6 yearsIt worked for me as well. It seems there is some issue with 4.1.0
-
IainCunningham almost 6 yearsI just found this problem too, and additionally found that there was an update available already in Android Studio 3.2 for the Android SDK Build-Tools (Tools --> SDK Manager --> SDK Tools), applying this update let me use
classpath 'com.google.gms:google-services:4.1.0'
again. YMMV. -
Oliver Spryn almost 6 yearsomg, literally spent time over 2 weeks to get this working, and all I had to do was downgrade from 4.1.0 to 4.0.0?!?!?! Thank you tons
-
Igor Čordaš over 5 yearsFor me version com.google.gms:google-services:4.1.0 was crashing but com.google.gms:google-services:4.0.1 worked
-
Patty P over 5 yearsI experienced this exactly as well. Wish Google wouldn't break my app builds every time they push an update.
-
Nicolas Mauti over 5 yearsHi, I get it working by adding
apply plugin: 'com.google.gms.google-services'
in my build.gradle (with google-services:4.1.0) -
kuelye over 5 yearsaccording with issue tracker will be fixed in 4.2.0
-
Brian Crider over 5 yearsPSIXO's solution worked for me as well. Change the project's build.gradle to use classpath 'com.google.gms:google-services:4.0.1' instead of 4.1.0 and the crash no longer happens in my app (which is just the sample MLKit starter app).
-
Abraham Mathew over 5 yearsyeah for me it worked by changing the build version to 4.0.1. It may be some dependency issue
-
david72 over 5 yearsAfter updating all versions to latest (4.2.0), that resolved the issue for me
-
Anil over 5 yearsI just downgraded com.google.gms:google-services:4.1.0 to om.google.gms:google-services:4.0.0 and it works. It saved my day.
-
Dasser Basyouni over 5 years4.0.2 is working well too according to issue tracker
-
Aanal Shah over 5 yearsDowngrading google service plugin from version 4.1.0 to version 4.0.1 in my project's build.gradle worked for me. classpath 'com.google.gms:google-services:4.0.1'
-
Nanda Z about 5 yearsi solve my problem after downgrade 'com.google.firebase:firebase-core:16.0.8' to 'com.google.firebase:firebase-core:16.0.7', thanks
-
garnet almost 5 yearsUsing following latest versions helped me resolve the issue. No need to downgrade :) classpath 'com.android.tools.build:gradle:3.4.2' classpath 'com.google.gms:google-services:4.3.0'
-
garnet almost 5 yearsUsing following latest versions helped me resolve the issue. No need to downgrade :) classpath 'com.android.tools.build:gradle:3.4.2' classpath 'com.google.gms:google-services:4.3.0'
-
natsumiyu almost 5 yearsI tried adding it in my SplashActivity but my app crashes when receiving the push notification.
-
Gabriel Lidenor almost 5 years@natsumiyu you should add it to your main activity, your Splash Activity only runs when you open the app.
-
natsumiyu almost 5 years@GabrielLidenor thank you it doesn't crash now but it still doesn't display in my emulator.
-
Abhinav Saxena about 4 yearsOnly Firebase assistant can correct the dependencies, and connect the project with the Firebase developers console. It also provides the necessary code.
-
34m0 about 4 yearsIf an answer has this many upvotes it should be the first answer in the list. In my case I made the mistake of putting the apply plugin in the project level gradle file.
-
Sandip Wankhede about 4 yearsSuperb .. great analysis
-
Arsalan Ahmad Ishaq about 4 yearsGuys exactly in which file do we have to put
FirebaseApp.initializeApp(this);
??? -
Odaym over 3 years
pamobo0609
's solution about adding the google play services plugin solved it, has nothing to do with initializing anything -
yadunath.narayanan almost 3 yearsapply plugin: 'com.google.gms.google-services' this worked for me as well
-
Dan Abnormal almost 3 yearsDowngrading to classpath 'com.google.gms:google-services:4.3.8' fixed the issue for me. BIG thanks!
-
PM4 almost 3 yearsDropping from 4.3.9 to 4.3.8 solved it for me as well, thanks! I recall upgrading from 4.0.x to 4.1.0 also had a very similar issue, it's baffling how often google-services versions break projects entirely.
-
Build3r almost 3 yearsDowngrading was a quick fix, had just updated all the libraries. Hope a day will arrive where we won't have anxiety when we update libraries.
-
Devil10 almost 3 yearsDowngrading from 4.3.9 to 4.3.8 works for me too. +1
-
JerabekJakub almost 3 yearsYes, solved in 4.3.10 - but thanks to all!
-
agega almost 3 yearsThis worked for me as well after a frustrated search.. thank you