It's not as difficult as it may seem.
This explains all the proccess.
Your choose is the Option 1. After step 4 your project-level build.gradle should look like (of course sdk, java, libreries versions can be different)
all sub-projects/modules.
buildscript {
repositories {
google()
}
dependencies {
classpath 'com.google.gms:google-services:4.3.12'
}
}
plugins {
id 'com.android.application' version '7.2.1' apply false
id 'com.android.library' version '7.2.1' apply false
}
task clean(type: Delete) {
delete rootProject.buildDir
}
allprojects {
repositories {
google()
}
}
and app-level build.gradle(app/build.gradle) like
plugins {
id 'com.android.application'
}
android {
compileSdk 32
defaultConfig {
applicationId "io.devmartynov.bulletinboard"
minSdk 30
targetSdk 32
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_11
targetCompatibility JavaVersion.VERSION_11
}
}
dependencies {
implementation 'androidx.appcompat:appcompat:1.4.2'
implementation 'com.google.android.material:material:1.6.1'
implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
testImplementation 'junit:junit:4.13.2'
androidTestImplementation 'androidx.test.ext:junit:1.1.3'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
implementation platform('com.google.firebase:firebase-bom:30.2.0')
implementation 'com.google.firebase:firebase-database'
}
apply plugin: 'com.google.gms.google-services'
Don't foget to add implementation 'com.google.firebase:firebase-database'
in you app-level build.gradle(app/build.gradle), because steps 1-4 don't mention it.
After that sync gradle files changes. While syncing you may face with error Build was configured to prefer settings repositories over project repositories but repository 'Google' was added by build file 'build.gradle'
. This explains and solves the problem.
Next you should create Realtime Database
. It's pretty simple!
You also must place google-services.json
file to your app directory. You can download this file in settings->project settings->your apps->google-services.json
After that you can try to write to you DB.
Add this test code to onCreate
method and start app.
FirebaseDatabase database = FirebaseDatabase.getInstance();
DatabaseReference myRef = database.getReference("messages");
myRef.setValue("Hello, World!");