Roy Solberg
Roy Solberg

Reputation: 19643

Default FirebaseApp is not initialized

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

Upvotes: 396

Views: 435064

Answers (30)

BAIJU SHARMA
BAIJU SHARMA

Reputation: 296

If you are using the latest version catalog and directly connecting your Android app with Firebase using Android studio. Then below solution will work

Note: In this process we do not have to include "google-services.json" file inside the project.

Add below code in the Module.app build gradle

plugins {
    alias(libs.plugins.google.services)
}

Add below dependencies in the gradle

implementation(platform(libs.firebase.bom)) 
implementation(libs.firebase.auth)
implementation(libs.firebase.firestore)

Inside Version Catalog file .toml

[plugins]

google-services = { id = "com.google.gms.google-services", version.ref = "google-services" }

Upvotes: 2

Maxim Mazurok
Maxim Mazurok

Reputation: 4138

  1. In gradle/libs.versions.toml add this:
[versions]
googleServices = "4.4.2"

[plugins]
google-gms-google-services = { id = "com.google.gms.google-services", version.ref = "googleServices" }
  1. In build.gradle.kts add this:
plugins {
    alias(libs.plugins.google.gms.google.services) apply false
}
  1. In app/build.gradle.kts add this:
plugins {
    alias(libs.plugins.google.gms.google.services)
}

I'm very new to Android, but a fresh Kotlin project created in Android Studio is using this approach, I think you have your libraries in libs.versions.toml and then just reference aliases in other places, as opposed to putting string IDs and versions all over the place.


In another project, I had to upgrade Gradle to the latest (from 7.3.1 to 8.5 or 8.7 not sure)

Upvotes: 5

Kibrom Hs
Kibrom Hs

Reputation: 97

changing

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

To

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

Inside project level build.gradle

Upvotes: 3

J. Doe
J. Doe

Reputation: 13033

2024 solution

I created my Android project in 2024, 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 {

Upvotes: 24

Rumit Patel
Rumit Patel

Reputation: 12469


UPDATE (19th Feb. 2024):


We do not need to call FirebaseApp.initializeApp(this); anywhere manually. And we should not too.

Possibility 1: (An old approach):

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: More Info. here for both .kts and .gradle config files.

  • In your root-level (project-level) Gradle file (/build.gradle.kts or /build.gradle), add the Google services plugin as a dependency:
plugins {
  // Add the dependency for the Google services Gradle plugin
  id("com.google.gms.google-services") version "4.4.1" apply false
}
  • In your module (app-level) Gradle file (usually //build.gradle.kts or //build.gradle), add the Google services plugin:
plugins {
  // Add the Google services Gradle plugin
  id("com.google.gms.google-services")
}
  • Then clean project and run again. It worked for me.

Possibility 3:

I faced the same issue and got an unexpected and strange solution.

From this answer:

I removed tools:node="replace" from AndroidManifest.xml and it worked like a charm.

Upvotes: 96

Attee
Attee

Reputation: 23

I had same problem. This code is solved it.

FirebaseOptions options = new FirebaseOptions.Builder()
                .setProjectId("YOUR_PROJECT_ID")
                .setApplicationId("YOUR_APPLICATION_ID")
                .setApiKey("YOUR_API_KEY")
                .build();
FirebaseApp.initializeApp(this, options);

You can read the variables from the Firebase console / Project settings page.

I tried everything, none of the other suggestions helped me.

Upvotes: 1

Rakesh Ray
Rakesh Ray

Reputation: 72

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'
    }
}

Upvotes: 3

Abdullah Programmer
Abdullah Programmer

Reputation: 75

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.

Upvotes: 0

Muzammil Hussain
Muzammil Hussain

Reputation: 1272

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)

Upvotes: 0

abir-cse
abir-cse

Reputation: 546

For latest android studio and sdk

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

id 'com.google.gms.google-services'

Upvotes: 2

dgalluccio
dgalluccio

Reputation: 83

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.

Upvotes: 0

mehmoodnisar125
mehmoodnisar125

Reputation: 1529

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

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

Upvotes: 9

LethalMaus
LethalMaus

Reputation: 956

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.

Upvotes: 1

Androidcoder
Androidcoder

Reputation: 4679

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 [email protected] for assistance.' The one non-auto response I got from repeated attempts to [email protected] 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 [email protected] 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

Upvotes: 17

Muhammad Umair
Muhammad Umair

Reputation: 273

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

Upvotes: 0

Ashraf Sabry
Ashraf Sabry

Reputation: 3182

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).

Upvotes: 0

Gabriel Lidenor
Gabriel Lidenor

Reputation: 2985

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();

Upvotes: 177

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]");

Upvotes: 1

Leonid
Leonid

Reputation: 5223

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.

Upvotes: 505

Fawad Khan
Fawad Khan

Reputation: 103

changing

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

to

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

Works for me

Upvotes: 0

bianca
bianca

Reputation: 7214

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" />

Upvotes: 4

tony Macias
tony Macias

Reputation: 226

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

Upvotes: 0

Jmz
Jmz

Reputation: 169

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
}

Upvotes: 4

raddevus
raddevus

Reputation: 9077

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.

Upvotes: 0

Ammar Bukhari
Ammar Bukhari

Reputation: 2122

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

Upvotes: 135

Himanshu
Himanshu

Reputation: 151

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'
    }

Upvotes: 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);

Upvotes: 4

Md Nakibul Hassan
Md Nakibul Hassan

Reputation: 2858

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.

Upvotes: 4

Alp Altunel
Alp Altunel

Reputation: 3443

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

has a problem. instead use:

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

Upvotes: 19

Aayush Singla
Aayush Singla

Reputation: 1365

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.

Upvotes: 11

Related Questions