【问题标题】:Android Kotlin - cannot resolve firebaseAndroid Kotlin - 无法解析 Firebase
【发布时间】:2020-02-12 21:07:18
【问题描述】:

我正在尝试将 Firebase(特别是 Firebase 身份验证)包含到我的项目中。我是一名正在尝试学习 Kotlin 的 iOS 开发人员,所以距离我上一次接触 Android/Android Studio 已经有 4 年了(而且之前绝对从未接触过 Kotlin)。

所以我对 Android 的了解有点落伍了。

我还尝试实施 Clean Architecture 来让我的应用有一个良好的开端。所以我添加了一个新模块,如下所述:https://www.raywenderlich.com/3595916-clean-architecture-tutorial-for-android-getting-started。我添加了一个core 模块,然后在该模块中我添加了包:domaindatainteractors

我将此添加到我的应用程序的build.gradle dependencies 块中:

implementation project(':core')

所以我的应用知道我的模块core

我的core 模块的build.gradle 文件如下所示:

apply plugin: 'java-library'
apply plugin: 'kotlin'
apply plugin: 'kotlin-android-extensions'
apply plugin: 'kotlin-kapt'

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version"
    implementation "org.jetbrains.kotlinx:kotlinx-coroutines-android:1.2.1"
    implementation 'com.google.dagger:dagger:2.26'
    kapt 'com.google.dagger:dagger-compiler:2.26'
    implementation 'com.google.firebase:firebase-auth:19.2.0'
    implementation("io.reactivex.rxjava2:rxkotlin:2.4.0")
}

sourceCompatibility = "7"
targetCompatibility = "7"

你可以看到我还添加了Dagger 用于依赖注入。这很好解决。

这是根 Gradle 文件:

// Top-level build file where you can add configuration options common to all sub-projects/modules.

buildscript {
    ext.kotlin_version = '1.3.61'
    repositories {
        google()
        jcenter()

    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.5.3'
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
        classpath 'com.google.gms:google-services:4.3.3'
        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

allprojects {
    repositories {
        google()
        jcenter()

    }
}

task clean(type: Delete) {
    delete rootProject.buildDir
}

这是应用程序 Gradle 文件:

apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
apply plugin: 'com.google.gms.google-services'
apply plugin: 'kotlin-kapt'

android {
    compileSdkVersion 29
    buildToolsVersion "29.0.3"
    defaultConfig {
        applicationId "com.charlotteerpels.travellyn"
        minSdkVersion 23
        targetSdkVersion 29
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
    implementation 'androidx.appcompat:appcompat:1.1.0'
    implementation 'androidx.core:core-ktx:1.2.0'
    implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
    implementation 'com.google.dagger:dagger:2.26'
    implementation 'com.google.android.gms:play-services-gcm:17.0.0'

    implementation project(':core')

    kapt 'com.google.dagger:dagger-compiler:2.26'
    testImplementation 'junit:junit:4.13'
    androidTestImplementation 'androidx.test.ext:junit:1.1.1'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
}

Gradle 构建/同步完成且没有错误。

但是当我尝试在具有简单函数的简单类中执行以下操作时,出现错误。

val auth = FirebaseAuth.getInstance();

错误是:Unresolved reference: FirebaseAuth。 Android Studio 也没有提供导入的东西。所以我尝试自己在文件顶部添加导入,就在package 行的下方。

import com.google.firebase.auth.FirebaseAuth,但这也不能解决。在我的文件中,导入中的firebase 和函数中的FirebaseAuth 保持红色。

我做错了什么?

【问题讨论】:

  • 您是否按照此处的步骤进行操作,重要的是 google() 存储库并应用了 google 服务:firebase.google.com/docs/android/setup
  • 是的,我做到了:(我将在帖子中发布我的项目 Gradle 文件和应用程序 Gradle 文件

标签: android firebase gradle kotlin firebase-authentication


【解决方案1】:

您可能需要确保包含 google repos 才能找到 firebase。

来自here

a) 在项目级别的 gradle 中

buildscript {

  repositories {
    // Check that you have the following line (if not, add it):
    google()  // Google's Maven repository
  }

  dependencies {
    // ...

    // Add the following line:
    classpath 'com.google.gms:google-services:4.3.3'  // Google Services plugin
  }
}

allprojects {
  // ...

  repositories {
    // Check that you have the following line (if not, add it):
    google()  // Google's Maven repository
    // ...
  }
}

b) 在应用级别的 gradle 中

apply plugin: 'com.android.application'
// Add the following line:
apply plugin: 'com.google.gms.google-services'  // Google Services plugin

android {
  // ...
}

【讨论】:

  • @Charlotte1993 不幸的是,我无法重现您的问题 :( 我只使用我提供的代码创建了一个空项目,并且可以毫无问题地使用 firebase。如果您可以在您的问题发生的地方提供一个 repo也许我可以帮助找到问题
猜你喜欢
  • 2021-07-30
  • 2017-07-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-10-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多