在更改了一些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