Default FirebaseApp is not initialized

301,108

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.

Share:
301,108

Related videos on Youtube

Roy Solberg
Author by

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, 2022

Comments

  • Roy Solberg
    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
      Doug Stevenson over 7 years
      Try searching SO for "Default FirebaseApp is not initialized". There are a lot of different circumstances that might cause this.
    • Roy Solberg
      Roy Solberg over 7 years
      Yeah, 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
      Doug Stevenson over 7 years
      Are you able to produce a minimal example the reproduces this problem, and show the manifest, build.gradle, and Activity?
    • Andrew Kelly
      Andrew Kelly over 7 years
      Do you initialise Firebase in a ContentProvider or in your Application class?
    • Roy Solberg
      Roy Solberg over 7 years
      We 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
      Lumii over 7 years
      Hi, 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 and FirebaseRemoteConfig.getInstance. We don't call FirebaseApp.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
      Dmitry over 7 years
      I experience the same problem for a small percentage of players. Using the latest 10.0.1
    • Roy Solberg
      Roy Solberg over 7 years
      We'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
      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
      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
      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
      Wahib Ul Haq almost 7 years
      Those who mentioned that they are using FirebaseRemoteConfig.getInstance() shouldn't worry because internally .getInstance does call FirebaseApp var0 = FirebaseApp.getInstance();.
    • CoDe
      CoDe almost 6 years
      btw, 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
      Manmohan Soni about 4 years
      We are also facing this issue just on line - "val firebaseRemoteConfig = FirebaseRemoteConfig.getInstance()"
    • Abhinav Saxena
      Abhinav Saxena about 4 years
      Only Firebase assistant can correct the dependencies, and connect the project with the Firebase developers console. It also provides the necessary code.
  • Roy Solberg
    Roy Solberg over 7 years
    Check, 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
    crakama over 7 years
    It 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 to compile 'com.google.firebase:firebase-core:9.6.0' to match my other libraries. Original answer You're mixing Firebase SDK versions. as explained
  • Rockyfish
    Rockyfish over 7 years
    'apply plugin: 'com.google.gms.google-services' worked for me.
  • Dmitry
    Dmitry over 7 years
    Did 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
    ultra.deep over 7 years
    thanks :) I forget to add this line in build.gradle : apply plugin: 'com.google.gms.google-services'
  • dabluck
    dabluck over 7 years
    why does the plugin have to be last?
  • Henrik
    Henrik over 7 years
    Did you add this to an extended Application class and onCreate or in your activity before using an Firebase API?
  • Roy Solberg
    Roy Solberg about 7 years
    @Henrik Only in ExtendedApplication.onCreate().
  • Roy Solberg
    Roy Solberg almost 7 years
    Whoa, 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
    Sébastien over 6 years
    @RoySolberg Was this enough to completely solve the issue in your case?
  • Roy Solberg
    Roy Solberg over 6 years
    @Sébastien Yes. We have not seen the issue since we added that line.
  • Sébastien
    Sébastien over 6 years
    Well, 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
    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
    pamobo0609 over 6 years
    It's important to note that besides this initialization, the google services must be applied as follows: apply plugin: 'com.google.gms.google-services'
  • abhit
    abhit almost 6 years
    in which file do i have to add this MainActivity.java?
  • Neri
    Neri almost 6 years
    This was the only problem, no need to initialize either
  • iuq
    iuq almost 6 years
    It worked for me as well. It seems there is some issue with 4.1.0
  • IainCunningham
    IainCunningham almost 6 years
    I 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
    Oliver Spryn almost 6 years
    omg, 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š
    Igor Čordaš over 5 years
    For me version com.google.gms:google-services:4.1.0 was crashing but com.google.gms:google-services:4.0.1 worked
  • Patty P
    Patty P over 5 years
    I experienced this exactly as well. Wish Google wouldn't break my app builds every time they push an update.
  • Nicolas Mauti
    Nicolas Mauti over 5 years
    Hi, I get it working by adding apply plugin: 'com.google.gms.google-services' in my build.gradle (with google-services:4.1.0)
  • kuelye
    kuelye over 5 years
    according with issue tracker will be fixed in 4.2.0
  • Brian Crider
    Brian Crider over 5 years
    PSIXO'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
    Abraham Mathew over 5 years
    yeah for me it worked by changing the build version to 4.0.1. It may be some dependency issue
  • david72
    david72 over 5 years
    After updating all versions to latest (4.2.0), that resolved the issue for me
  • Anil
    Anil over 5 years
    I 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
    Dasser Basyouni over 5 years
    4.0.2 is working well too according to issue tracker
  • Aanal Shah
    Aanal Shah over 5 years
    Downgrading 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
    Nanda Z about 5 years
    i solve my problem after downgrade 'com.google.firebase:firebase-core:16.0.8' to 'com.google.firebase:firebase-core:16.0.7', thanks
  • garnet
    garnet almost 5 years
    Using 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
    garnet almost 5 years
    Using 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
    natsumiyu almost 5 years
    I tried adding it in my SplashActivity but my app crashes when receiving the push notification.
  • Gabriel Lidenor
    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
    natsumiyu almost 5 years
    @GabrielLidenor thank you it doesn't crash now but it still doesn't display in my emulator.
  • Abhinav Saxena
    Abhinav Saxena about 4 years
    Only Firebase assistant can correct the dependencies, and connect the project with the Firebase developers console. It also provides the necessary code.
  • 34m0
    34m0 about 4 years
    If 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
    Sandip Wankhede about 4 years
    Superb .. great analysis
  • Arsalan Ahmad Ishaq
    Arsalan Ahmad Ishaq about 4 years
    Guys exactly in which file do we have to put FirebaseApp.initializeApp(this); ???
  • Odaym
    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
    yadunath.narayanan almost 3 years
    apply plugin: 'com.google.gms.google-services' this worked for me as well
  • Dan Abnormal
    Dan Abnormal almost 3 years
    Downgrading to classpath 'com.google.gms:google-services:4.3.8' fixed the issue for me. BIG thanks!
  • PM4
    PM4 almost 3 years
    Dropping 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
    Build3r almost 3 years
    Downgrading 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
    Devil10 almost 3 years
    Downgrading from 4.3.9 to 4.3.8 works for me too. +1
  • JerabekJakub
    JerabekJakub almost 3 years
    Yes, solved in 4.3.10 - but thanks to all!
  • agega
    agega almost 3 years
    This worked for me as well after a frustrated search.. thank you