Default FirebaseApp is not initialized

匿名 (未验证) 提交于 2019-12-03 07:50:05

问题:

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

回答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:

FirebaseApp.initializeApp(this); 


回答2:

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

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

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 {   // ...   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 said in documentation. I had exception as in a question above when forgot to add this in my gradle files.



回答3:

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



回答4:

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


回答5:

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



回答6:

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 )



回答7:

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:-

  


回答8:

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



回答9:

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.



易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!