我正在尝试连接到 firebase,但应用程序不断崩溃。我知道我一定有一些未对齐的依赖项,但我相信我根据以下内容采用了正确的版本: https://firebase.google.com/support/release-notes/android
Build.gradle.kts 包含:
plugins {
id("com.android.application") version "8.6.0"
id("org.jetbrains.kotlin.android") version "1.9.25"
id("com.google.gms.google-services") version "4.4.2"
}
dependencies {
// Firebase dependencies
implementation(platform("com.google.firebase:firebase-bom:33.3.0"))
implementation("com.google.firebase:firebase-auth:23.0.0")
implementation("com.google.firebase:firebase-firestore:25.1.0")
}
关于项目级别:
plugins {
alias(libs.plugins.android.application) apply false
alias(libs.plugins.jetbrains.kotlin.android) apply false
id("com.google.gms.google-services") version "4.4.2" apply false
}
我使用的代码如下,作为概念证明..:
fun checkWhitelistByEmail(
email: String,
onAccessGranted: () -> Unit,
onAccessDenied: () -> Unit,
scope: CoroutineScope // Pass the coroutine scope from outside
) {
val db = Firebase.firestore
// val db = FirebaseFirestore.getInstance()
Log.d("FirestoreCheck", "Firestore instance initialized")
db.collection("whitelisted_users")
.get()
.addOnSuccessListener { result ->
for (document in result) {
Log.d("FirestoreCheck", "${document.id} => ${document.data}")
}
}
.addOnFailureListener { exception ->
Log.w("FirestoreCheck", "Error getting documents.", exception)
}
}
但是它一直崩溃:
FATAL EXCEPTION: main (Ask Gemini)
Process: com.flashc_ai.FlahsC_AI, PID: 5439
java.lang.RuntimeException: Internal error in Cloud Firestore (25.1.0).
at com.google.firebase.firestore.util.AsyncQueue.lambda$panic$3(AsyncQueue.java:546)
at com.google.firebase.firestore.util.AsyncQueue$$ExternalSyntheticLambda3.run(D8$$SyntheticClass:0)
at android.os.Handler.handleCallback(Handler.java:958)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loopOnce(Looper.java:205)
at android.os.Looper.loop(Looper.java:294)
at android.app.ActivityThread.main(ActivityThread.java:8177)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:552)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:971)
Caused by: com.google.android.gms.tasks.RuntimeExecutionException: java.lang.RuntimeException: java.lang.NoClassDefFoundError: Failed resolution of: Lio/grpc/InternalGlobalInterceptors;
at com.google.android.gms.tasks.zzw.getResult(com.google.android.gms:play-services-tasks@@18.1.0:3)
at com.google.firebase.firestore.remote.FirestoreChannel.lambda$runBidiStreamingRpc$0$com-google-firebase-firestore-remote-FirestoreChannel(FirestoreChannel.java:146)
等等....
知道我该如何安排吗,我已经被这个错误困扰了好几天了,无论我做什么都没有效果。请注意,firebaseAuth 工作正常,因此连接设置正常。 谢谢!
我尝试指定导入模块的版本,尝试简化代码以进行概念证明...
项目级别的gradle文件就可以了。
build.gradle(应用程序) a)插件块内部->粘贴
id(“com.google.gms.google-services”)
b)依赖项块内部 -> 粘贴
implementation("com.google.firebase:firebase-analytics")
implementation(platform("com.google.firebase:firebase-bom:33.3.0"))
只需使用这两个依赖项,auth依赖项您可以直接导入到您的活动文件中。
至于代码,我为您提供了我的示例测试应用程序中的代码块
val currentUser = auth.currentUser
val userId = currentUser?.uid
try {
val userId = auth.currentUser?.uid
//val currentTotal = total.text.toString().toIntOrNull() ?: 0
val addedAmount = amount.text.toString().toInt()
// Update total and save to SharedPrefs
val newTotal = total.text.toString().toInt() + addedAmount
total.text = newTotal.toString()
SharedPrefs.getInstance(context = this).save("Amount", newTotal.toString())
val expense = hashMapOf(
"description" to desc.text.toString(),
"amount" to amount.text.toString().toInt(),
"timestamp" to System.currentTimeMillis(),
"total" to newTotal,
"date" to TxtDate.text.toString()
)
userId?.let {
FirebaseFirestore.getInstance().collection("users").document(it)
.collection("expenses")
.add(expense)
.addOnSuccessListener {
clearEditTexts()
MessageUtilDialog.errorMessagedialog(this,"Expense Added")
}.addOnFailureListener {
MessageUtilDialog.errorMessagedialog(this,"Expense Not Added")
}
}
} catch (e: NumberFormatException) {
// Handle invalid number input
MessageUtilDialog.errorMessagedialog(this, "Invalid number format")
//Toast.makeText(this, "Invalid number format", Toast.LENGTH_SHORT).show()
}
}
检查此代码并尝试建立连接(Firebase.Firestore.getInstance().collection("your firestore db name")