I have just started to learn Android and want to connect to a database. The first choice was Firebase. All the dependencies have been installed by following the instructions to the dot. But the very first instruction fails. The instruction is to add an instance by writing this code in the activity. In my case, the activity is MainActivity.kt
val db = Firebase.firestore
Here, Android tells me that Firestore is an resolved reference. I am not sure why is this happening. Can someone help me?
Here is my app level build.gradle file:
plugins {
id 'com.android.application'
id 'kotlin-android'
id 'com.google.gms.google-services'
}
android {
compileSdk 31
defaultConfig {
applicationId "com.example.happybirthday"
minSdk 19
targetSdk 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 'androidx.core:core-ktx:1.3.2'
implementation 'androidx.appcompat:appcompat:1.2.0'
implementation 'com.google.android.material:material:1.3.0'
implementation 'androidx.constraintlayout:constraintlayout:2.0.4'
testImplementation 'junit:junit:4. '
androidTestImplementation 'androidx.test.ext:junit:1.1.2'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
implementation platform('com.google.firebase:firebase-bom:29.0.1')
implementation 'com.google.firebase:firebase-analytics-ktx'
}
Here is the project level build.gradle file:
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
repositories {
google()
mavenCentral()
}
dependencies {
classpath "com.android.tools.build:gradle:7.0.3"
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.5.20"
classpath 'com.google.gms:google-services:4.3.10'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
CodePudding user response:
To be able to use Cloud Firestore, besides the BoM dependency:
implementation platform('com.google.firebase:firebase-bom:29.0.1')
You also need to add Firestore dependency for Kotlin:
implementation 'com.google.firebase:firebase-firestore-ktx'
Or the corresponding one for Java:
implementation 'com.google.firebase:firebase-firestore'
As explained here. Without it, it won't work, hence the unresolved reference.