在这个进程com.example.app中,我们看到了一些异常消息Default FirebaseApp is not initialized。确保首先调用FirebaseApp.initializeApp(Context)。在我们的Android应用中,我们刚刚添加了Firebase远程配置。
堆栈跟踪如下:
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上的各种设备上。
编辑:
我发现这个问题引起了一些关注。我想这个解释对你们中的一些人来说可能很有趣:https://firebase.googleblog.com/2016/12/how-does-firebase-initialize-on-android.html
在我的例子中,我从我的一个项目中复制并粘贴了firebase依赖项。
所以,我的build.gradle(:app)有crashlytics依赖,但我没有在build.gradle(根)中添加crashlytics类路径。
验证你以这种方式添加了这些依赖项:
build.gradle(根):
// 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(应用):
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'
}
}
去应用类或第一个稳定类(MainActivity):
// initialize firebase:
FirebaseApp.initializeApp(context)
在我的例子中,我从我的一个项目中复制并粘贴了firebase依赖项。
所以,我的build.gradle(:app)有crashlytics依赖,但我没有在build.gradle(根)中添加crashlytics类路径。
验证你以这种方式添加了这些依赖项:
build.gradle(根):
// 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(应用):
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'
}
}
去应用类或第一个稳定类(MainActivity):
// initialize firebase:
FirebaseApp.initializeApp(context)
你需要在build中添加Firebase Gradle buildscript依赖。gradle(项目级)
classpath 'com.google.gms:google-services:3.1.0'
在app/build.gradle中添加Firebase插件
apply plugin: 'com.google.gms.google-services'
build.gradle will include these new dependencies:
compile 'com.google.firebase:firebase-database:11.0.4'
来源:Android Studio Assistant
在我的案例中,谷歌Services gradle插件没有从Google - Services生成所需的values.xml文件。json文件。Firebase库使用这个生成的值文件来初始化自身,如果找不到值文件,它似乎不会抛出错误。检查values文件是否存在于以下位置,并使用来自google- services的适当字符串进行填充。json文件:
app/build/generated/res/google-services/{build_type}/values/values.xml
和/或
app/build/generated/res/google-services/{flavor}/{build_type}/xml/global_tracker.xml
欲了解更多详情,请参阅:
https://developers.google.com/android/guides/google-services-plugin
我的特殊情况是由于使用gradle tools版本,对于我正在运行的Android Studio版本来说太高级了(即确保您运行grade tools v3.2)。X-YYY (Android Studio v3.2)。