默认的FirebaseApp在此进程中未初始化。请确保首先调用FirebaseApp.initializeApp(Context)。

3

在更改了一些gradle依赖项后,我今天遇到了这个致命错误。尽管我尝试将gradle依赖项还原回原始状态,但我仍然在此行上遇到错误:

class MainActivity : AppCompatActivity() {

    val db = FirebaseFirestore.getInstance() // IllegalStateException

错误:

java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.app/com.app.MainActivity}: 

java.lang.IllegalStateException: Default FirebaseApp is not initialized in this process com.app. Make sure to call FirebaseApp.initializeApp(Context) first.

 Caused by: java.lang.IllegalStateException: Default FirebaseApp is not initialized in this process com.app. Make sure to call FirebaseApp.initializeApp(Context) first.
    at com.google.firebase.FirebaseApp.getInstance(com.google.firebase:firebase-common@@17.1.0:186)
    at com.google.firebase.firestore.FirebaseFirestore.getInstance(com.google.firebase:firebase-firestore@@20.1.0:70)
    at com.app.MainActivity.<init>(MainActivity.kt:43)

Gradle(应用程序)
apply plugin: 'com.google.gms.google-services'

dependencies {

// Firebase
implementation 'com.google.firebase:firebase-auth:18.0.0'
implementation 'com.firebaseui:firebase-ui-auth:4.3.1'
implementation 'com.google.firebase:firebase-firestore:20.1.0'
implementation 'com.google.firebase:firebase-storage:18.0.0'
implementation 'com.firebaseui:firebase-ui-storage:4.3.2'
implementation 'com.google.firebase:firebase-messaging:19.0.1'
implementation 'com.google.firebase:firebase-functions:17.0.0'

}

Gradle (项目)
buildscript {
    ext.kotlin_version = '1.3.21'
    repositories {
        google()
        jcenter()
        mavenCentral()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.4.1'
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
        classpath 'com.google.gms:google-services:4.2.0'
    }
}

根据多个答案1 2,问题与google-services依赖项有关 - 我已将其更改为推荐的版本4.2.0,但仍然出现相同的错误。有任何解决方法吗?编辑:这是重复的,因为所提出的答案FirebaseApp.initializeApp(this)没有解决问题。编辑2:我的模拟器上没有错误,只在我的设备上出现。

我也尝试在我的onCreate()和应用程序类中添加FirebaseApp.initializeApp(this),然后访问Firebase实例,但是我收到了相同的错误。@ManojPerumarath - Zorgan
2个回答

0

清单中的包名google-services.json中的"package_name"不同时,我遇到了这个错误。

在我修复了包名之后,我必须卸载应用程序并清理构建。

在Xamarin中:您不需要调用

FirebaseApp.initializeApp(Context);

你可以直接调用

await FirebaseMessaging.Instance.SubscribeToTopic(topic);

或者

Java.Lang.Object token = await FirebaseMessaging.Instance.GetToken();

它像魔法一样运行。:-)


0

我尝试使用Android Studio(3.5)助手设置Firebase。

这是他们向我展示要注入的推荐依赖项

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

在应用程序的 build.gradle 文件中。
apply plugin: 'com.google.gms.google-services'

implementation 'com.google.firebase:firebase-auth:16.0.4'
implementation 'com.google.firebase:firebase-core:16.0.4'
implementation 'com.google.firebase:firebase-messaging:16.0.4'
implementation 'com.google.firebase:firebase-database:16.0.4'
implementation 'com.google.firebase:firebase-storage:16.0.4'
implementation 'com.google.firebase:firebase-functions:16.0.4'

你不需要添加所有这些依赖项,只需添加所需的依赖项即可。否则,APK 文件将变得更大。

我已经尝试了4.2.0,但仍然无法正常工作。我的应用程序使用了你列出的所有Firebase服务,因此我需要包含所有这些依赖项。 - Zorgan
@Zorgan,你试过在所有依赖项中使用版本16.0吗? - Manoj Perumarath
是的,我尝试了你提供的依赖项,但它们无法工作,因为我遇到了以下错误:在项目'app'中,已解析的Google Play服务库依赖项依赖于另一个精确版本(例如"[17.0.4]"),但未被解析为该版本。库所表现出的行为将是未知的。依赖项失败:com.google.firebase:firebase-messaging:17.3.4 -> com.google.firebase:firebase-iid@[17.0.4],但firebase-iid版本为19.0.0。以下依赖项是直接或间接依赖于具有问题的构件的项目依赖项。 - Zorgan
ERROR: Failed to resolve: com.google.firebase:firebase-messaging:16.0.4 - Zorgan

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接