I’m trying to connect firebase realtime database to my app. I watched many youtube about this and tried the same way of videos but it doesn’t work for me. Below is the step I followed.
Make empty project >> add ‘android:exported = "true"’ on manifest file (without this my app gets an error while building) >> go tools and select firebase >> select Realtime Database >> click ‘get started with realtime database [KOTLIN]’ >> Following the guideline, I’ve connected my app to Firebase and Added the Realtime Database to my app like below image
image
After that, at firebase console, the firebase project showed that my app is connected and then I created a realtime database. Below is how it looks.
image
The security rules are test mode. And then I tried to insert a data to the database. Below is the code.
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()
}
}
}
My purpose was simply saving the value of testData on the database and toasting a message when I press the button (btn). But if I do it, Nothing happens to my database and also the app doesn’t toast any message. What can I do for it?
project level gradle
file
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
}
app level gradle
file
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'
}
*When I Debug app, there is an warning that says ‘w: Runtime JAR files in the classpath should have the same version. These files were found in the classpath:‘. Could this be the cause of the problem?
2
Answers
If none of the onSuccess() or onFailure() methods are triggered, and your database location is us-central1, then most likely the reason is the lack of an internet connection. Establish a connection on your emulator and it will work perfectly fine.
I think it will solve your problem