FirebaseApp is not initialized by default

We see 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. 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 also use 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 Javadoc we do not need to perform manual initialization in our case.

An exception occurs on Android 4-6 on various devices.

Edit:

I see that this question gets a little attention. I think this explanation may be interesting for some of you: https://firebase.googleblog.com/2016/12/how-does-firebase-initialize-on-android.html

+193
android firebase-remote-config
Oct 17 '16 at 8:19
source share
27 answers

I had the same problem a while ago.

You are trying to get a Firebase instance without initializing it. Please add this line of code before trying to get a Firebase instance:

 FirebaseApp.initializeApp(this); 
+111
Jan 10 '17 at 13:11
source share

Be sure to add build.gradle to your root level

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

Then at your Gradle module level (usually the application /build.gradle) add the line "apply plugin" at the bottom of the file to enable the Gradle plugin:

 apply plugin: 'com.android.application' android { // ... } dependencies { // ... compile '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 stated in the documentation . I had an exception, as in the question above, when I forgot to add this to my Gradle files.

+265
Oct 17 '16 at 11:22
source share

Looks like google-services:4.1.0 has a problem. Either lower 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 help

+116
02 Sep '18 at 10:23
source share

I was missing the next line in the app / build.gradle file

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

and as soon as a clean project and run again. This fixed it for me.

+44
Jan 09 '18 at 17:22
source share
  classpath 'com.google.gms:google-services:4.1.0' 

There is a problem. use instead:

  classpath 'com.google.gms:google-services:4.2.0' 
+16
Jan 25 '19 at 14:11
source share

First of all you need to add com.google.gms: google-services: xxx at the 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 the plugin: "com.google.gms.google-services" in the application /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 you still get the problem you need to add

 FirebaseApp.initializeApp(this); 

before you call

 FirebaseInstanceId.getInstance().getToken(); 
+12
Apr 26 '17 at 13:46 on
source share

After updating various dependencies, I received a Crashlytics error during compilation: "Crashlytics detected an invalid API key: null. Check the Crashlytics plugin to make sure the application was successfully added! Contact support@fabric.io for help. 'The only non-automatic answer, which I received from repeated attempts to support@fabric.io, which the error directs you to, was that Fabric and Crashlytics are separate commands, so they could not help me. I avoided introducing an additional Fabric layer in Crashlytics and could not get new key from the Fabric site or even make the site recognize me. Trying to get around this by simply removing Crashlytics from my code, I got "By default, FirebaseApp does not initialize com.example.app in this process. Be sure to call FirebaseApp.initializeApp (Context) first 'runtime failure.

I never had to add the initialization string 'FirebaseApp.initializeApp (this)', and in fact it was commented out. The documentation even mentions that this is not necessary if only to use Firebase for one action. Adding didn't matter, still got a kill start error.

It turns out that the cause of the new obscure errors was the updated google-services dependency. At the moment, I don’t have time to spend more days trying to fix the gun errors caused by the new addiction, so until someone finds a solution, I will stick to the old version. In addition to the strange initialization failure, the new version may force Fabric users to crashlytics. To do this, users are also forced to revert to the old version of the dependency: Crashlytics detected an invalid API key: null. after updating 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 when trying to install with "xxx unexpectedly closed", such as when I tried to update the google-services dependency. Rummaging through the magazine, I found a link telling 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 not mentioned in the installation and implementation instructions here https://firebase.google.com/docs/android/setup and here https://developers.google.com/admob/android/interstitial .

Previously, I had to deal with only one identifier associated with advertising for my application, INTERSTITIAL_UNIT_ID. Now you need to deal with two. In addition to the above addition, the documentation instructs you to add here ADMOB_APP_ID (the same number that you associate with ads.APPLICATION_ID in the new manifest code)

 MobileAds.initialize(this, ADMOB_APP_ID); 

The identifiers INTERSTITIAL_UNIT_ID and ADMOB_APP_ID can be found in the Google AdMob console. My game application stopped showing ads when I first updated Firebase dependencies and still does not show ads, giving an error code 0 in

 public void onAdFailedToLoad(int errorCode){... 

Even after all this mess added, I still cannot update the google-services dependency without failing to start an initialized error. I expect to get stuck in google-services: 4.0.1 for a while.

EDIT 24/24/18: from mobileadssdk-advisor+support@google.com after several weeks of correspondence stating that the ad is not showing after the updates:

β€œThank you for exchanging device logs. From logs, this looks like an existing problem, and it is on our priority list, and our team is working on a fix, and this only happens on Android O and P devices.”

Only O and P devices? It was the last two versions of O that were released on September 25, 2017.

+11
04 Oct '18 at 18:31
source share

As mentioned in the @PSIXO comment, this may be a problem with the version of google dependency services. For me, a change

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

at

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

There might be some problem with version 4.1.0. Since I spent a lot of time on this, I decided to write this as an answer.

+8
Oct 25 '18 at 4:19
source share

If you are using FirebaseUI , there is no need for FirebaseApp.initializeApp(this); in your code as per sample .

Remember to add to the root level build.gradle:

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

Then at 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' 

It. Do not need anymore.

+7
Jan 03 '18 at 8:50
source share

Another possible solution is to try using a different Android studio if you are using some beta versions. Helped me. The new Android studio just didn't add Firebase properly. In my case 3.3preview

After some more research, I found that the problem is that the new Android studio is starting a project with a new version of Google Services, and it looks like this was the original problem. As @Ammar Bukhari said, this change helped:

classpath 'com.google.gms: google-services: 4.1.0' β†’ classpath 'com.google.gms: google-services: 4.0.0'

+5
Sep 15 '18 at 16:16
source share

I assume that there are compatibility issues with the version of google services and versions of firebase.

I changed the dependency in the Project build.gradle file

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

and then updated the build.gradle module dependencies:

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

implementation of "com.google.firebase: firebase-core: 16.0.7"

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

+4
Feb 05 '19 at 18:23
source share

You need to add the Firebase Gradle buildscript dependency to build.gradle (project level)

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

and add the 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

+3
Dec 13 '17 at 0:41
source share

In my case, the Google plugin for plugins did not generate the required values.xml file from google-services.json file. The Firebase library uses this generated value file to initialize, and it seems that it does not throw an error if the value file is not found. Make sure that the values ​​file exists in the following location and is populated with the corresponding lines from google-sevices.json :

 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 

Learn more at https://developers.google.com/android/guides/google-services-plugin.

My specific case was caused by using the version of gradle tools that was too advanced for the version of Android Studio that I was running (i.e. make sure you run the tools of class v3.2.X-YYY with Android Studio v3.2).

+3
Sep 06 '18 at 0:12
source share

If you recently upgraded Android Studio to version 3.3.1, which has a problem with the com.google.gms: google-services (below 4.2.0) dependencies, update com.google.gms: google-services to version 4.2.0.

 dependencies { classpath 'com.android.tools.build:gradle:3.3.1' classpath 'com.google.gms:google-services:4.2.0' } 
+3
Feb 12 '19 at 11:30
source share

We do not need to call FirebaseApp.initializeApp(this); anywhere by hand. and we should not either.

I just ran into the same problem and got an unexpected and strange solution.

From this answer:

I removed tools:node="replace" and it works like a charm.

+2
Jan 02 '19 at 9:04
source share

The reason for this is com.google.gms: version of google-services . When I used 4.1.0 , I ran into the same error. Then I downgrade. 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 this solves the error.

+2
Feb 04 '19 at 17:24
source share

for me it was a dependency update 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 } 
+2
Mar 10 '19 at 1:13
source share

Although manually initializing Firebase using FirebaseApp.initializeApp(this); , the error disappears, it does not eliminate the root cause, some odd problems come together, it seems to be unresolved, for example

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

Using the new Gradle plugin (e.g. Android 2.2.3 plugin and Gradle 2.14.1) fixed everything. (Of course, the setup should be correct according to the Firebase documentation )

0
Aug 16 '17 at 4:34 on
source share

We will need to initialize Firebase in the onCreate function of the 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"> 
0
Aug 17 '17 at 12:36 on
source share

One reason for this may be the forgetfulness of adding android.permission.INTERNET permissions to AndroidManifest.xml

 <uses-permission android:name="android.permission.INTERNET" /> 
0
Aug 24 '17 at 21:49
source share

My problem was not resolved using this procedure.

 FirebaseApp.initializeApp(this); 

So I tried something else, and now my firebase has been successfully initialized. Try adding the following to 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" ..} 
0
Aug 29 '18 at 20:28
source share

Click Tools> Firebase to open the Assistant window.

Click to expand one of the listed functions (for example, Google Analytics), then click on 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 code.

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

0
Sep 24 '18 at 15:19
source share

Installed Firebase via Android Studio Tools ... Firebase ...

I did the installation using the built-in tools from Android Studio (following the latest documents from Firebase). This fixed the main dependencies, but when I tried to connect to the database, it always gave me an error that I needed to call to initialize first, although I was:

FirebaseApp is not initialized by default in this process. Remember 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 this, I finally saw an error that helped me:

The file google-services.json is missing. A Google Services plugin cannot function without it. Search Location: C: \ Users \% username% \ AndroidStudioProjects \ TxtFwd \ app \ src \ nullnull \ debug \ google-services.json
C: \ Users \% username% \ AndroidStudioProjects \ TxtFwd \ application \ SRC \ Debug \ nullnull \ Google-services.json
C: \ Users \% username% \ AndroidStudioProjects \ TxtFwd \ application \ SRC \ nullnull \ Google-services.json
C: \ Users \% username% \ AndroidStudioProjects \ TxtFwd \ application \ SRC \ Debug \ Google-services.json
C: \ Users \% username% \ AndroidStudioProjects \ TxtFwd \ application \ SRC \ nullnullDebug \ Google-services.json
C: \ Users \% username% \ AndroidStudioProjects \ TxtFwd \ application \ Google-services.json

This is problem. Version 4.1.0 does not seem to give this build error for some reason - not to mention that you do not have google-services.json. There is no google-services.json file in my application, so I exited and added it.

But since it was an update that used the existing firsbase database in real time, I never had to create this file in the past. I went to firebase, generated and added, and this solved the problem.

Returned to 4.1.0

As soon as I discovered all this, I changed the classpath variable back (to 4.1.0) and rebuilt, and it again crashed with the error that it was not initialized.

Root problems

  • Build with 4.1.0 does not give you a permissible error when precompiling, so you may not know what is happening.
  • Running against 4.1.0 causes an initialization error.
0
Feb 21 '19 at 15:00
source share

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

0
Apr 19 '19 at 20:22
source share

changes

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

at

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

Works for me

0
Jun 06 '19 at 13:02
source share

Cause and solution: This is a common error that you will get in most cases. Reason: when you integrate your project with Firebase, it adds dependencies

 implementation 'com.google.firebase:firebase-auth:16.1.0' implementation 'com.google.android.gms:play-services-auth:16.0.1' 

and classpath

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

you just need to update them

Here is how you can upgrade

go to gradle sript and do it

-one
Apr 05 '19 at 10:12
source share

update version

 classpath 'com.google.gms:google-services:4.2.0' 
-one
Jun 06 '19 at 8:59
source share



All Articles