簡體   English   中英

IllegalStateException:默認 FirebaseApp 未在此過程中初始化。 確保先調用 FirebaseApp.initializeApp(Context)

[英]IllegalStateException: Default FirebaseApp is not initialized in this process. Make sure to call FirebaseApp.initializeApp(Context) first

在更改一些 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:我的模擬器沒有錯誤,只有我的設備

我嘗試使用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將變大。

清單中的Package 名稱google-services.json中的“package_name”不同時,我遇到了這個錯誤。

在修復 package 名稱后,我不得不卸載應用程序並清理構建。

在 Xamarine 中:您不需要調用

FirebaseApp.initializeApp(Context);

你可以直接調用

await FirebaseMessaging.Instance.SubscribeToTopic(topic);

或者

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

它就像一個魅力。 :-)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM