Firebase Realtime Database接入Android应用失败求助
Firebase Realtime Database 接入Android应用失败排查
问题背景
我尝试将Firebase Realtime Database接入Android应用,操作流程如下:
- 创建空白Android项目
- 在清单文件中添加
android:exported="true"(不添加会编译报错) - 通过Android Studio的Tools菜单选择Firebase,进入Realtime Database模块,点击「get started with realtime database [KOTLIN]」
- 按照指引完成应用与Firebase的绑定,以及Realtime Database实例的创建
Firebase控制台显示应用已成功连接,且数据库已设置为测试模式安全规则。我编写了Kotlin代码,实现点击按钮时将测试数据写入数据库,并通过Toast反馈操作结果,但点击按钮后数据库无任何变化,也没有弹出任何Toast提示。
相关代码与配置
Activity代码
val testData = 110 private lateinit var database : DatabaseReference override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) val btn = findViewById<Button>(R.id.btn1) btn.setOnClickListener { database = FirebaseDatabase.getInstance().getReference("testing") database.setValue(testData).addOnSuccessListener { Toast.makeText(this,"success",Toast.LENGTH_SHORT).show() }.addOnFailureListener{ Toast.makeText(this,"Fail",Toast.LENGTH_SHORT).show() } } }
项目级build.gradle文件
buildscript { ext.kotlin_version = "1.5.31" repositories { google() mavenCentral() } dependencies { classpath "com.android.tools.build:gradle:4.2.1" classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" classpath 'com.google.gms:google-services:4.3.13' // NOTE: Do not place your application dependencies here; they belong // in the individual module build.gradle files } } allprojects { repositories { google() mavenCentral() jcenter() } } task clean(type: Delete) { delete rootProject.buildDir }
应用级build.gradle文件
plugins { id 'com.android.application' id 'kotlin-android' id 'com.google.gms.google-services' } android { compileSdkVersion 31 buildToolsVersion "30.0.3" defaultConfig { applicationId "com.lotterynumber.firebase" minSdkVersion 16 targetSdkVersion 31 versionCode 1 versionName "1.0" testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" } buildTypes { release { minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro' } } compileOptions { sourceCompatibility JavaVersion.VERSION_1_8 targetCompatibility JavaVersion.VERSION_1_8 } kotlinOptions { jvmTarget = '1.8' } } dependencies { implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version" implementation 'androidx.core:core-ktx:1.8.0' implementation 'androidx.appcompat:appcompat:1.4.0' implementation 'com.google.android.material:material:1.6.1' implementation 'androidx.constraintlayout:constraintlayout:2.1.4' implementation 'com.google.firebase:firebase-database-ktx:20.0.5' testImplementation 'junit:junit:4.+' androidTestImplementation 'androidx.test.ext:junit:1.1.3' androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0' }
疑问
调试过程中出现以下警告:
w: Runtime JAR files in the classpath should have the same version. These files were found in the classpath:
请问这个警告是否是导致数据库写入失败、Toast不弹出的原因?该如何解决这个问题?
内容的提问来源于stack exchange,提问作者josephyumss
相关产品推荐
相关产品推荐

