默认FirebaseApp未初始化

我们在消息中看到一些例外情况, 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. 在我们的Android应用程序中,我们刚刚添加了Firebase Remote Config。

堆栈跟踪如下:

 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) 

这是版本9.6.1,我们也使用其他Firebase组件:

 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" 

从文档和Javadoc我可以看到,我们不应该在我们的情况下做任何手动初始化。

Android 4-6在各种设备上发生exception。

编辑:

我看到这个问题得到一点关注。 我认为这个解释对于你们中的一些人可能是有趣的: https : //firebase.googleblog.com/2016/12/how-does-firebase-initialize-on-android.html

前段时间我也有同样的问题。

您正在尝试获取Firebase的实例,而不进行初始化。 在尝试获取Firebase实例之前,请添加以下代码行:

 FirebaseApp.initializeApp(this); 

确保添加到您的根级build.gradle

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

然后,在您的模块级Gradle文件(通常是app / build.gradle)中,在文件底部添加'apply plugin'行以启用Gradle插件:

 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' 

如文件中所述。 在上面的问题中,我忘记在我的gradle文件中添加这个例外。

首先,您需要在根级别build.gradle中添加com.google.gms:google-services:xxx

 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 } 

}

之后,您需要在app / build.gradle上应用插件:“com.google.gms.google-services”

 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' 

如果仍然有问题,那么你需要添加

 FirebaseApp.initializeApp(this); 

就在你打电话之前

 FirebaseInstanceId.getInstance().getToken(); 

尽pipe使用FirebaseApp.initializeApp(this);手动初始化Firebase FirebaseApp.initializeApp(this); 使得错误消失,不解决根本原因,一些奇怪的问题走到一起似乎并没有解决,如

  • FCM需要仅用于GCM的com.google.android.c2dm.permission.RECEIVE权限
  • 令牌在发送第一个通知后变为未注册
  • 消息未收到/ onMessageReceived()永远不会被调用,

使用更新的Gradle插件(例如Android插件2.2.3和Gradle 2.14.1)修复了一切。 (当然,根据Firebase文档,设置必须正确)

我们将不得不在Application类的onCreate函数中初始化Firebase。

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

}

清单文件中的代码: –

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

发生这种情况的原因之一可能是忘记在AndroidManifest.xml添加android.permission.INTERNET权限

<uses-permission android:name="android.permission.INTERNET" />