355

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

Roy Solberg
  • 18,133
  • 12
  • 49
  • 76
  • 1
    Try searching SO for "Default FirebaseApp is not initialized". There are a lot of different circumstances that might cause this. – Doug Stevenson Oct 17 '16 at 18:22
  • Yeah, I have. Didn't find any with matching circumstances, so that's why I'm asking. Could've mentioned that in the Q. – Roy Solberg Oct 17 '16 at 19:18
  • Are you able to produce a minimal example the reproduces this problem, and show the manifest, build.gradle, and Activity? – Doug Stevenson Oct 17 '16 at 20:35
  • Do you initialise Firebase in a ContentProvider or in your Application class? – Andrew Kelly Oct 17 '16 at 22:15
  • 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. – Roy Solberg Oct 18 '16 at 12:14
  • 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 – Cheok Yan Cheng Jan 04 '17 at 15:47
  • 2
    I experience the same problem for a small percentage of players. Using the latest 10.0.1 – Dmitry Jan 06 '17 at 07:58
  • 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 Jan 06 '17 at 09:15
  • @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. – Roy Solberg Jan 06 '17 at 09:32
  • @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 :-( – Dmitry Jan 06 '17 at 10:24
  • @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. – Roy Solberg Jan 06 '17 at 13:31
  • Those who mentioned that they are using `FirebaseRemoteConfig.getInstance()` shouldn't worry because internally `.getInstance` does call `FirebaseApp var0 = FirebaseApp.getInstance();`. – Wahib Ul Haq Jul 21 '17 at 09:28
  • 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 ! – CoDe Jul 02 '18 at 08:28
  • We are also facing this issue just on line - "val firebaseRemoteConfig = FirebaseRemoteConfig.getInstance()" – Manmohan Soni Apr 03 '20 at 14:33
  • Only Firebase assistant can correct the dependencies, and connect the project with the Firebase developers console. It also provides the necessary code. – Abhinav Saxena May 01 '20 at 04:20

36 Answers36

478

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.

Roman Nazarevych
  • 7,513
  • 4
  • 62
  • 67
Leonid
  • 4,953
  • 2
  • 10
  • 13
  • 3
    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. – Roy Solberg Oct 17 '16 at 13:19
  • 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.](http://stackoverflow.com/questions/39763491/getting-default-firebaseapp-is-not-initialized-in-this-process-despite-initial) as explained – crakama Nov 22 '16 at 17:53
  • 7
    'apply plugin: 'com.google.gms.google-services' worked for me. – Rockyfish Dec 11 '16 at 08:15
  • 10
    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 :-( – Dmitry Jan 06 '17 at 08:00
  • 2
    thanks :) I forget to add this line in build.gradle : apply plugin: 'com.google.gms.google-services' – ultra.deep Mar 09 '17 at 20:30
  • 3
    why does the plugin have to be last? – dabluck Mar 10 '17 at 21:38
  • This was the only problem, no need to initialize either – Neri Aug 24 '18 at 15:36
  • 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. – 34m0 May 10 '20 at 09:22
  • apply plugin: 'com.google.gms.google-services' this worked for me as well – yadunath.narayanan Jul 14 '21 at 05:42
  • This worked for me as well after a frustrated search.. thank you – agega Aug 20 '21 at 04:10
  • There is an issue I am also using apply plugin: 'com.google.firebase.firebase-perf' apply plugin: 'com.google.firebase.crashlytics' These are needed below apply plugin: 'com.google.gms.google-services' If I keep these three plugins on top they do work, but I am left with the issue If I move them to bottom project build fails – nayan dhabarde Nov 16 '22 at 04:10
  • ````apply plugin: 'com.google.gms.google-services'```` solved my issue – Umang Jan 06 '23 at 09:58
171

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();
Filippos Zofakis
  • 561
  • 6
  • 12
Gabriel Lidenor
  • 2,905
  • 2
  • 25
  • 26
  • Did you add this to an extended Application class and onCreate or in your activity before using an Firebase API? – Henrik Mar 20 '17 at 15:54
  • 3
    @Henrik Only in ExtendedApplication.onCreate(). – Roy Solberg Apr 10 '17 at 07:11
  • @RoySolberg Was this enough to completely solve the issue in your case? – Sébastien Dec 11 '17 at 08:24
  • @Sébastien Yes. We have not seen the issue since we added that line. – Roy Solberg Dec 11 '17 at 11:57
  • 6
    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(). – Sébastien Dec 13 '17 at 08:18
  • @Sébastien using in main class with this instead of contexts will resolve the issue give it a try – Gopi.cs Feb 12 '18 at 10:23
  • 123
    It's important to note that besides this initialization, the google services must be applied as follows: `apply plugin: 'com.google.gms.google-services'` – pamobo0609 Mar 07 '18 at 20:29
  • 3
    in which file do i have to add this MainActivity.java? – abhit Jul 26 '18 at 07:31
  • 26
    For me version com.google.gms:google-services:4.1.0 was crashing but com.google.gms:google-services:4.0.1 worked – Igor Čordaš Oct 03 '18 at 09:05
  • 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). – Brian Crider Nov 16 '18 at 20:27
  • 2
    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' – Aanal Shah Mar 05 '19 at 18:31
  • 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 Jul 10 '19 at 13:02
  • I tried adding it in my SplashActivity but my app crashes when receiving the push notification. – natsumiyu Jul 24 '19 at 09:19
  • @natsumiyu you should add it to your main activity, your Splash Activity only runs when you open the app. – Gabriel Lidenor Jul 24 '19 at 11:16
  • @GabrielLidenor thank you it doesn't crash now but it still doesn't display in my emulator. – natsumiyu Jul 25 '19 at 07:43
  • Only Firebase assistant can correct the dependencies, and connect the project with the Firebase developers console. It also provides the necessary code. – Abhinav Saxena May 01 '20 at 04:19
  • 2
    Guys exactly in which file do we have to put `FirebaseApp.initializeApp(this);` ??? – Arsalan Ahmad Ishaq May 23 '20 at 18:28
  • `pamobo0609`'s solution about adding the google play services plugin solved it, has nothing to do with initializing anything – Odaym Nov 13 '20 at 11:14
133

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

Roshana Pitigala
  • 8,437
  • 8
  • 49
  • 80
Ammar Bukhari
  • 2,082
  • 2
  • 16
  • 16
  • 9
    It worked for me as well. It seems there is some issue with 4.1.0 – iuq Sep 07 '18 at 08:19
  • 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. – IainCunningham Sep 26 '18 at 20:58
  • 2
    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 – Oliver Spryn Sep 27 '18 at 16:04
  • 4
    according with [issue tracker](https://issuetracker.google.com/issues/112716914) will be fixed in 4.2.0 – kuelye Nov 06 '18 at 11:29
  • 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. – Anil Feb 13 '19 at 14:42
  • 4.0.2 is working well too according to [issue tracker](https://github.com/google/play-services-plugins/issues/22#issuecomment-455136562) – Dasser Basyouni Mar 04 '19 at 11:52
70

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.

Rumit Patel
  • 8,830
  • 18
  • 51
  • 70
  • 9
    Downgrading to classpath 'com.google.gms:google-services:4.3.8' fixed the issue for me. BIG thanks! – Dan Abnormal Aug 09 '21 at 09:13
  • 4
    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. – PM4 Aug 09 '21 at 10:52
  • 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. – Build3r Aug 09 '21 at 15:42
  • 1
    Downgrading from 4.3.9 to 4.3.8 works for me too. +1 – Devil10 Aug 10 '21 at 08:12
  • Yes, solved in 4.3.10 - but thanks to all! – JerabekJakub Aug 10 '21 at 13:06
61

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.

Mircea Nistor
  • 3,145
  • 1
  • 26
  • 32
varotariya vajsi
  • 3,965
  • 37
  • 39
18

First thing you need to add com.google.gms:google-services:x.x.x at root level build.gradle

buildscript {
repositories {
    jcenter()
}
dependencies {
    classpath 'com.android.tools.build:gradle:2.3.1'
    classpath 'com.google.gms:google-services:3.0.0'

    // NOTE: Do not place your application dependencies here; they belong
    // in the individual module build.gradle files
}

}

After that you need to apply plugin: 'com.google.gms.google-services' at app/build.gradle

dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
    exclude group: 'com.android.support', module: 'support-annotations'
})
compile 'com.android.support:appcompat-v7:25.3.1'
compile 'com.android.support:design:25.3.1'
compile 'com.android.support:cardview-v7:25.3.1'

compile 'com.google.android.gms:play-services-gcm:9.8.0'
compile 'com.google.android.gms:play-services-maps:9.8.0'
compile 'com.google.android.gms:play-services-location:9.8.0'
compile 'com.google.firebase:firebase-messaging:9.8.0'
testCompile 'junit:junit:4.12'
}


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

and if still you are getting problem then you need to add

FirebaseApp.initializeApp(this);

just before you are calling

FirebaseInstanceId.getInstance().getToken();
ankurdayalsingh
  • 260
  • 2
  • 9
18
    classpath 'com.google.gms:google-services:4.1.0'

has a problem. instead use:

    classpath 'com.google.gms:google-services:4.2.0'
Alp Altunel
  • 3,324
  • 1
  • 26
  • 27
16

After updating various dependencies I got a Crashlytics error in the compile, 'Crashlytics found an invalid API key: null. Check the Crashlytics plugin to make sure that the application has been added successfully! Contact support@fabric.io for assistance.' The one non-auto response I got from repeated attempts to support@fabric.io the error directs you to was that Fabric and Crashlytics are separate teams so they couldn't help me. I've avoided implementing the extra Fabric layer to Crashlytics, and was unable to get a new key from the Fabric site, or even get the site to recognize me. On attempting to work around this by just removing Crashlytics from my code, I got the 'Default FirebaseApp is not initialized in this process com.example.app. Make sure to call FirebaseApp.initializeApp(Context) first' crash in the run.

I've never had to add the initialization line of 'FirebaseApp.initializeApp(this)', and in fact had it commented out. The documentation even mentions not needing this if only using Firebase for one activity. Adding it made no difference, still got the run killing error.

Turns out what was causing the new obscure errors was the updated google-services dependency. For now, I don't have time to spend more days trying to fix the shotgun errors the new dependency is causing, so until someone comes up with solutions I'll stick to the old version. Besides the odd initialization crash, the new version may be forcing Fabric on Crashlytics users. Users are being forced back to the old dependency version for this too: Crashlytics found an invalid API key: null. after updated com.google.gms:google-services:4.1.0

//com.google.gms:google-services:4.1.0 BAD
com.google.gms:google-services:4.0.1//GOOD

EDIT 10/17/18: After updating the following dependencies again

implementation 'com.google.firebase:firebase-ads:17.0.0'
implementation 'com.google.firebase:firebase-auth:16.0.4'
implementation 'com.google.firebase:firebase-database:16.0.3'
implementation 'com.google.firebase:firebase-core:16.0.4

I got an immediate crash on the install attempt with 'xxx has unexpectedly closed', like when I attempted the google-services dependency update. Digging into the log I found a link directing me to add this to the manifest

<meta-data
        android:name="com.google.android.gms.ads.APPLICATION_ID"
        android:value="ca-app-pub-xxxxxx~xxxxxx"/>

This is new, and is not mentioned in the setup and interstitial instructions here https://firebase.google.com/docs/android/setup and here https://developers.google.com/admob/android/interstitial.

I used to only have to deal with one ad-related ID for my app, the INTERSTITIAL_UNIT_ID. Now two need to be dealt with. Besides the above addition, documentation directs adding ADMOB_APP_ID here (the same number you tie with ads.APPLICATION_ID in the new manifest code)

MobileAds.initialize(this, ADMOB_APP_ID);

The INTERSTITIAL_UNIT_ID and ADMOB_APP_ID ids can be dug up in your Google AdMob console. My game app stopped serving ads on my first update of the firebase dependencies and still does not serve ads, giving error code 0 in the

public void onAdFailedToLoad(int errorCode){...

Even after all this added clutter, I still can't update the google-services dependency without the initialize error run crash. I expect to be stuck at google-services:4.0.1 for some time.

EDIT 10/24/18: From mobileadssdk-advisor+support@google.com after weeks of correspondence on not getting ad serves after updates:

'Thanks for sharing the device logs. From the logs, it looks like an existing issue and this is on our priority list and our team is working on the fix and this is only happening on Android O and P devices.'

Only O and P devices? That's the last two versions, O came out on September 25, 2017. Yikes.

UPDATE 8/8/21: After updating google-services from 4.3.8 to 4.3.9 I'm suddenly getting this issue from almost 3 years ago again, despite having indeed called the initialization (although allegedly not needed). Will again have to delay update implementation:

//com.google.gms:google-services:4.3.9 BAD
com.google.gms:google-services:4.3.8//GOOD
Androidcoder
  • 4,389
  • 5
  • 35
  • 50
  • 3
    I experienced this exactly as well. Wish Google wouldn't break my app builds every time they push an update. – Patty P Oct 04 '18 at 20:27
  • Hi, I get it working by adding `apply plugin: 'com.google.gms.google-services'` in my build.gradle (with google-services:4.1.0) – Nicolas Mauti Oct 15 '18 at 22:39
10

As mentioned by @PSIXO in a comment, this might be the problem with the dependency version of google-services. For me changing,

buildscript {
    // ...
    dependencies {
        // ...
        classpath 'com.google.gms:google-services:4.1.0'
    }
}

to

buildscript {
    // ...
    dependencies {
        // ...
        classpath 'com.google.gms:google-services:4.0.1'
    }
}

worked.There might be some problem with 4.1.0 version. Because I wasted many hours on this, I thought to write this as an answer.

Aayush Singla
  • 1,305
  • 1
  • 13
  • 20
8

If you are using FirebaseUI, no need of FirebaseApp.initializeApp(this); in your code according the sample.

Make sure to add to your root-level build.gradle :

buildscript {

    repositories {
        google()
        jcenter()
    }
    dependencies {
        ...
        classpath 'com.google.gms:google-services:3.1.1'
        ...
    }
}

Then, in your module level Gradle file :

dependencies {

    ...

    // 1 - Required to init Firebase automatically (THE MAGIC LINE)
    implementation "com.google.firebase:firebase-core:11.6.2"

    // 2 - FirebaseUI for Firebase Auth (Or whatever you need...)
    implementation 'com.firebaseui:firebase-ui-auth:3.1.2'
    ...
}

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

That's it. No need more.

Phil
  • 4,730
  • 1
  • 41
  • 39
7

For me the problem was that I did not add this line in app/build.gradle file.

apply plugin: 'com.google.gms.google-services'
Suraj Rao
  • 29,388
  • 11
  • 94
  • 103
mehmoodnisar125
  • 1,469
  • 18
  • 14
6

You need add Firebase Gradle buildscript dependency in build.gradle (project-level)

classpath 'com.google.gms:google-services:3.1.0'

and add Firebase plugin for Gradle in app/build.gradle

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

build.gradle will include these new dependencies:
    compile 'com.google.firebase:firebase-database:11.0.4'

Source: Android Studio Assistant

Artificioo
  • 704
  • 1
  • 9
  • 19
5

In my case, the Google Services gradle plugin wasn't generating the required values.xml file from the google-services.json file. The Firebase library uses this generated values file to initialize itself and it appears that it doesn't throw an error if the values file can't be found. Check that the values file exists at the following location and is populated with the appropriate strings from your google-sevices.json file:

app/build/generated/res/google-services/{build_type}/values/values.xml

and/or

app/build/generated/res/google-services/{flavor}/{build_type}/xml/global_tracker.xml

For more detail see: https://developers.google.com/android/guides/google-services-plugin

My particular case was caused by using a gradle tools version that was too advanced for the version of Android Studio that I was running (ie ensure you run grade tools v3.2.X-YYY with Android Studio v3.2).

TheIT
  • 11,919
  • 4
  • 64
  • 56
5

Another possible solution - try different Android Studio if you are using some betas. Helped for me. New Android Studio simply didn't add Firebase properly. In my case 3.3preview

After some more investigation I found the problem was that new Android studio starts project with newer Google Services version and it looks it was the original problem. As @Ammar Bukhari suggested this change helped:

classpath 'com.google.gms:google-services:4.1.0' -> classpath 'com.google.gms:google-services:4.0.0'

4

One of the reason of this happening could be to forgetting adding android.permission.INTERNET permissions in AndroidManifest.xml

<uses-permission android:name="android.permission.INTERNET" />
adiga
  • 34,372
  • 9
  • 61
  • 83
bianca
  • 7,004
  • 12
  • 43
  • 58
4

I'm guessing there are compatibility problems with the version of google-services and firebase versions.

I changed in the Project's build.gradle file, the dependency

classpath 'com.google.gms:google-services:4.1.0' to 4.2.0

and then updated the module's build.gradle dependencies to:

implementation 'com.google.firebase:firebase-database:16.0.6'

implementation 'com.google.firebase:firebase-core:16.0.7'

Everything works like a charm, no need to type FirebaseApp.initializeApp(this);

  • i solve my problem after downgrade 'com.google.firebase:firebase-core:16.0.8' to 'com.google.firebase:firebase-core:16.0.7', thanks – Nanda Z Apr 26 '19 at 17:17
4

I created my Android project in 2023, it is just a little different than the other answers.

  1. You do NOT need to call FirebaseApp.initializeApp(this) yourself
  2. You do NOT need <uses-permission android:name="android.permission.INTERNET" />
  3. Go to your Project's build.gradle and add id 'com.google.gms.google-services' version '4.3.15' apply false under plugins {
  4. Go to your Module's build.gradle and add id 'com.google.gms.google-services' under plugins {
J. Doe
  • 12,159
  • 9
  • 60
  • 114
3

Reason for happening this is com.google.gms:google-services version.When I was using 4.1.0, I faced the same error. Then I downgrade the version. Before

classpath 'com.android.tools.build:gradle:3.3.0'
classpath 'com.google.gms:google-services:4.1.0'

After

classpath 'com.android.tools.build:gradle:3.3.0'
classpath 'com.google.gms:google-services:3.2.0'

Hope, it will solve the error.

Md Nakibul Hassan
  • 2,716
  • 1
  • 15
  • 19
3

If you recently update your Android Studio to 3.3.1 that have a problem with com.google.gms:google-services (Below 4.2.0) dependencies So please update com.google.gms:google-services to 4.2.0.

dependencies {
    classpath 'com.android.tools.build:gradle:3.3.1'
    classpath 'com.google.gms:google-services:4.2.0'
    }
Himanshu
  • 141
  • 1
  • 3
3

to me it was upgrading dependencies of com.google.gms:google-services inside build.gradle to

buildscript {
repositories {
    jcenter()
    mavenCentral()
    maven {
        url 'https://maven.google.com/'
        name 'Google'
    }
    google()
}
dependencies {
    classpath 'com.android.tools.build:gradle:3.3.2'
    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
}
Jmz
  • 159
  • 7
2

For latest android studio and sdk

----> add this line inside plugins{} of build:gradle(:app)

id 'com.google.gms.google-services'
abir-cse
  • 507
  • 3
  • 12
2

in gradle module file add

plugins {
...
id: 'com.google.gms.google-services'
}

verify the latest version from firebase and add in gradle project file.

for example current version is

 buildscript {
    dependencies {
        classpath 'com.google.gms:google-services:4.3.15'
    }
}
Rakesh Ray
  • 62
  • 1
  • 9
1

By following @Gabriel Lidenor answer, initializing app with context is not work in my case. What if you are trying to create firebase-app without google-service.json ? So before initializing any number of firebase app, first need to initialize as;

FirebaseOptions options = new FirebaseOptions.Builder().setApplicationId("APP_ID")
                    .setGcmSenderId("SENDER_ID").build();
FirebaseApp.initializeApp(context, options, "[DEFAULT]");
1

In my case I was deactivating the service in the build gradle (app) as follows:

android.applicationVariants.all { variant ->
    def googleTask = tasks.findByName("process${variant.name.capitalize()}GoogleServices")
    googleTask.enabled = "mock" != variant.flavorName
}

As this variant was only used for testing and mocking services it was deactivated. This didn't cause any issues for me for nearly 2 years. Once I noticed it within the gradle file it was a bit of a facepalm moment...

If you also do this, simply delete it.

LethalMaus
  • 798
  • 1
  • 8
  • 20
0

Although manually initialize Firebase with FirebaseApp.initializeApp(this); makes the error disappear, it doesn't fix the root cause, some odd issues come together doesn't seem to be solved, such as

  • FCM requires com.google.android.c2dm.permission.RECEIVE permission which is only for GCM
  • token becomes unregistered after first notification sent
  • message not received/ onMessageReceived() never get called,

Use newer Gradle plugin (e.g. Android plugin 2.2.3 and Gradle 2.14.1) fixed everything. (Of course setup has to be correct as per Firebase documentation )

Beeing Jk
  • 3,796
  • 1
  • 18
  • 29
  • 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. – Roy Solberg Aug 16 '17 at 07:18
0

My problem was not resolved with this procedure

FirebaseApp.initializeApp(this); 

So I tried something else and now my firebase has been successfully initialized. Try adding following in app module.gradle

BuildScript{
dependencies {..
classpath : "com.google.firebase:firebase-plugins:1.1.5"
    ..}
}

dependencies {...
implementation : "com.google.firebase:firebase-perf:16.1.0"
implementation : "com.google.firebase:firebase-core:16.0.3"
..}
Fahad
  • 118
  • 1
  • 3
  • 13
0

Installed Firebase Via Android Studio Tools...Firebase...

I did the installation via the built-in tools from Android Studio (following the latest docs from Firebase). This installed the basic dependencies but when I attempted to connect to the database it always gave me the error that I needed to call initialize first, even though I was:

Default FirebaseApp is not initialized in this process . Make sure to call FirebaseApp.initializeApp(Context) first.

I was getting this error no matter what I did.

Finally, after seeing a comment in one of the other answers I changed the following in my gradle from version 4.1.0 to :

classpath 'com.google.gms:google-services:4.0.1'

When I did that I finally saw an error that helped me:

File google-services.json is missing. The Google Services Plugin cannot function without it. Searched Location: C:\Users\%username%\AndroidStudioProjects\TxtFwd\app\src\nullnull\debug\google-services.json
C:\Users\%username%\AndroidStudioProjects\TxtFwd\app\src\debug\nullnull\google-services.json
C:\Users\%username%\AndroidStudioProjects\TxtFwd\app\src\nullnull\google-services.json
C:\Users\%username%\AndroidStudioProjects\TxtFwd\app\src\debug\google-services.json
C:\Users\%username%\AndroidStudioProjects\TxtFwd\app\src\nullnullDebug\google-services.json
C:\Users\%username%\AndroidStudioProjects\TxtFwd\app\google-services.json

That's the problem. It seems that the 4.1.0 version doesn't give that build error for some reason -- doesn't mention that you have a missing google-services.json file. I don't have the google-services.json file in my app so I went out and added it.

But since this was an upgrade which used an existing realtime firsbase database I had never had to generate that file in the past. I went to firebase and generated it and added it and it fixed the problem.

Changed Back to 4.1.0

Once I discovered all of this then I changed the classpath variable back (to 4.1.0) and rebuilt and it crashed again with the error that it hasn't been initalized.

Root Issues

  • Building with 4.1.0 doesn't provide you with a valid error upon precompile so you may not know what is going on.
  • Running against 4.1.0 causes the initialization error.
raddevus
  • 8,142
  • 7
  • 66
  • 87
0

use com.google.gms:google-services:4.0.1' instead of 4.1.0

tony Macias
  • 226
  • 3
  • 9
0

changing

classpath 'com.google.gms:google-services:4.1.0'

to

classpath 'com.google.gms:google-services:4.0.1'

Works for me

Fawad Khan
  • 103
  • 2
  • 12
  • 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 Jul 10 '19 at 13:03
0

If you're using Xamarin and came here searching for a solution for this problem, here it's from Microsoft:

In some cases, you may see this error message: Java.Lang.IllegalStateException: Default FirebaseApp is not initialized in this process Make sure to call FirebaseApp.initializeApp(Context) first.

This is a known problem that you can work around by cleaning the solution and rebuilding the project (Build > Clean Solution, Build > Rebuild Solution).

Ashraf Sabry
  • 3,081
  • 3
  • 32
  • 29
0

I downgrade project gms:google_services to classpath 'com.google.gms:google-services:4.0.1' and it work for me.

Muhammad Umair
  • 274
  • 3
  • 13
0

I fix the problem with : apply plugin: 'com.google.gms.google-services' in build.gradle file and move the file google-service.json under app folder.

dgalluccio
  • 85
  • 5
0

In my case, I copied and pasted firebase dependencies from one of my project. So, my build.gradle(:app) have crashlytics dependency but I didn't add crashlytics classpath in build.gradle(root).

Verify that you added these dependencies in this way:

build.gradle(root):

// Top-level build file where you can add configuration options 
//  common to all sub-projects/modules.

buildscript {
    repositories {
        mavenCentral()
        google()
    }

    dependencies {
        classpath "com.android.tools.build:gradle:${project.GRADLE_VERSION}"
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:${project.KOTLIN_VERSION}"
        classpath "com.google.gms:google-services:4.3.14"
        classpath 'com.google.firebase:firebase-crashlytics-gradle:2.9.2'
        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

allprojects {
    repositories {
        mavenCentral()
        google()
    }
}

build.gradle(:app):

plugins {
    id 'com.android.application'
    id 'org.jetbrains.kotlin.android'
    id 'com.google.gms.google-services'
    id 'com.google.firebase.crashlytics'

}

android {
  .......
  depencies {
      ........
    //firebase
    implementation platform("com.google.firebase:firebase-bom:31.1.1")
    implementation "com.google.firebase:firebase-crashlytics-ktx:18.3.2"
    implementation "com.google.firebase:firebase-analytics-ktx:21.2.0"
    implementation 'com.google.firebase:firebase-messaging-ktx:23.1.1'
    implementation 'com.google.firebase:firebase-config-ktx:21.2.0'
  }
}

Goto ApplicationClass or First Stable Class (MainActivity):

// initialize firebase:
FirebaseApp.initializeApp(context)
0

In my case, I updated my firestore dependency to the updated version :

implementation 'com.google.firebase:firebase-firestore:24.4.4'

and rebuild the project. Now it's working well.

-1

We will have to initialize Firebase in onCreate function of Application Class.

 package com.rocks.music.videoplayer;

 import android.app.Application;
 import android.content.Context;

 import com.google.firebase.FirebaseApp;


/**
* Created by ashish123 on 22/8/15.
  */
 public class MyApplication extends Application {

private static MyApplication mInstance;

@Override
public void onCreate() {
    super.onCreate();
    mInstance = this;
    try {
        FirebaseApp.initializeApp(this);
    }
    catch (Exception e) {
    }
}

public static Context getInstance() {
    return mInstance;
}

}

Code in manifest file:-

  <application
    android:name="com.rocks.music.videoplayer.MyApplication"
    android:allowBackup="true"
    android:icon="@drawable/app_icon"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
Ashish Saini
  • 2,328
  • 25
  • 21
-2

Click Tools > Firebase to open the Assistant window.

Click to expand one of the listed features (for example, Analytics), then click the provided tutorial link (for example, Log an Analytics event).

Click the Connect to Firebase button to connect to Firebase and add the necessary code to your app.

https://firebase.google.com/docs/android/setup

k4dima
  • 6,070
  • 5
  • 41
  • 39