【问题标题】:Firebase in Android: firestore is unresolved referenceAndroid中的Firebase:firestore是未解决的参考
【发布时间】:2022-01-09 17:12:47
【问题描述】:

我刚开始学习 Android,想连接数据库。第一个选择是 Firebase。所有依赖项都已按照点的说明安装。但是第一条指令失败了。指令是通过在活动中编写此代码来添加实例。就我而言,活动是 MainActivity.kt

val db = Firebase.firestore

在这里,Android 告诉我 Firestore 是一个已解析的引用。我不确定为什么会这样。有人可以帮我吗?

这是我的应用级 build.gradle 文件:

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'
}

这里是项目级别的 build.gradle 文件:

// 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
}

【问题讨论】:

    标签: android firebase kotlin google-cloud-firestore


    【解决方案1】:

    为了能够使用 Cloud Firestore,除了 BoM 依赖项:

    implementation platform('com.google.firebase:firebase-bom:29.0.1')
    

    您还需要为 Kotlin 添加 Firestore 依赖项:

    implementation 'com.google.firebase:firebase-firestore-ktx'
    

    或者Java对应的:

    implementation 'com.google.firebase:firebase-firestore'
    

    正如here 解释的那样。没有它,它将无法工作,因此无法解析参考。

    【讨论】:

    • 非常感谢!这实际上应该在说明中正确,而不是作为发行说明。还是我在说明中遗漏了它?
    • 据我所知,确实如此。但很高兴听到你解决了它。
    猜你喜欢
    • 2021-11-02
    • 1970-01-01
    • 2019-10-11
    • 2021-11-05
    • 1970-01-01
    • 2020-01-05
    • 1970-01-01
    • 1970-01-01
    • 2022-01-14
    相关资源
    最近更新 更多