使用 settings.gradle 文件将 Firebase 依赖项添加到 Android 项目

Adding Firebase dependencies to Android Project with settings.gradle file

提问人:Red Coder 提问时间:5/30/2023 更新时间:5/30/2023 访问量:621

问:

我在配置 Android Gradle 项目时遇到了很大的困难。我尝试了在互联网上找到的大量解决方案,但仍然无法成功构建和运行我的项目。我尝试将 Firebase 库添加到我的应用中。

该应用是通过最新的 Android Studio 导航模板生成的。

以下是我的 gradle 文件:

settings.gradle

pluginManagement {
    repositories {
        gradlePluginPortal()
        google()
        mavenCentral()
        maven { url "https://jitpack.io" }
    }
}

dependencyResolutionManagement {
    repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
    repositories {
        google()
        mavenCentral()
        maven { url "https://jitpack.io" }
        gradlePluginPortal()
//        jcenter()
    }
}
rootProject.name = "RowingBG Achievements"
include ':app'

build.gradle (项目)

buildscript {
    ext {
        gradle_version = "8.0.2"
        kotlin_version = '1.8.20'
        gms_version = '4.3.15'
    }
 //    repositories {
 //        google()
 //        mavenCentral()
 //        gradlePluginPortal()
 //        maven { url "https://maven.google.com" }
 //        maven { url "https://jitpack.io" }
 //    }

 //    dependencies {
 //        classpath "com.android.tools.build:gradle:$gradle_version"
 //        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
 //        classpath 'com.google.dagger:hilt-android-gradle-plugin:2.41'
 //        classpath "com.google.gms:google-services:$gms_version"
 //    }
 }
 plugins {
    id 'com.android.application' version "$gradle_version" apply false
    id 'com.android.library' version "$gradle_version" apply false
    id 'org.jetbrains.kotlin.android' version "$kotlin_version" apply false
    id "com.google.gms.google-services" version "$gms_version" apply false
 }

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

build.gradle (模块:App)

apply plugin: 'com.android.application'
apply plugin: 'org.jetbrains.kotlin.android'
apply plugin: 'com.google.gms.google-services'
//apply plugin: 'kotlin-android'
//apply plugin: 'kotlin-kapt'

android {
    namespace 'com.bzahov.rowingbg'
    compileSdk 33

    defaultConfig {
        applicationId "com.bzahov.rowingbg"
        minSdk 28
        targetSdk 33
        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'
    }
    buildFeatures {
        viewBinding true
    }
}
dependencies { 
     // Import the BoM for the Firebase platform
    implementation platform('com.google.firebase:firebase-bom:32.1.0')
    implementation 'com.google.firebase:firebase-auth-ktx'
    implementation 'com.google.firebase:firebase-analytics-ktx'
    implementation 'com.google.firebase:firebase-core'
    implementation "com.google.firebase:firebase-firestore-ktx"
    implementation "com.google.firebase:firebase-database-ktx"
    implementation "com.google.firebase:firebase-storage-ktx"
    implementation "com.google.android.gms:play-services-auth-ktx"


...}

我遇到了如此奇怪的错误:

未找到“:app”的变体。检查构建文件以确保至少存在一个变体。在: com.android.tools.idea.gradle.project.sync.AndroidModule.prepare(GradleModules.kt:266)

在我解决了这个问题之后,我得到了以下一个:

无法找到 com.google.firebase:firebase-core:。 要求者: 项目名称:app 无法解析:com.google.android.gms:play-services-auth-ktx 添加 Google Maven 存储库并同步项目 受影响的模块:app

原因:org.gradle.api.internal.artifacts.ivyservice.DefaultLenientConfiguration$ArtifactResolveException:无法解析配置“:app:debugRuntimeClasspath”的所有文件。

enter image description here

当我同步项目时,我收到这些警告,例如我的项目无法访问 Google Maven 存储库enter image description here

我尝试了很多不同的方法,但我对新设置没有足够的了解。Gradle 方法。你能帮我解决问题或给我一些建议和指导吗?

如果您能帮助我解决它,我将不胜感激,因为我已经尝试过独自完成它,但没有成功,已经很长时间了

提前感谢您的帮助。:)

Android Firebase android-gradle-plugin build.gradle

评论


答:

1赞 Silviya 5/30/2023 #1

在build.gradle(Module:App)中,您应该从以下位置删除:-ktx"com.google.android.gms:play-services-auth-ktx"

implementation "com.google.android.gms:play-services-auth"

评论

0赞 Red Coder 5/30/2023
我删除了firebase-core并按照您的建议进行操作,删除了-ktx,这解决了我的问题。谢谢
1赞 Alex Mamo 5/30/2023 #2

导致您出现问题的代码行是:

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

目前也已弃用。除此之外,您还删除了 和 插件。为了解决这个问题,请在配置文件中添加以下依赖项:kotlin-androidkotlin-kapt

settings.gradle:

pluginManagement {
    repositories {
        gradlePluginPortal()
        google()
        mavenCentral()
    }
}
dependencyResolutionManagement {
    repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
    repositories {
        google()
        mavenCentral()
    }
}
rootProject.name = "RowingBG Achievements"
include ':app'

build.gradle (项目):

buildscript {
    ext {
        gradle_version = '7.4.0'
        kotlin_version = '1.8.20'
        google_services_version = '4.3.15'
        firebase_bom_version = '32.0.0'
        play_services_auth_version = '20.5.0'
    }
    dependencies {
        classpath "com.google.gms:google-services:$google_services_version"
        classpath "com.google.dagger:hilt-android-gradle-plugin:$hilt_version"
    }
}

plugins {
    id 'com.android.application' version "${gradle_version}" apply false
    id 'com.android.library' version "${gradle_version}" apply false
    id 'org.jetbrains.kotlin.android' version "${kotlin_version}" apply false
}

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

build.gradle (模块:App):

plugins {
    id "com.android.application"
    id "kotlin-android"
    id "kotlin-kapt"
    id "com.google.gms.google-services"
}

android {
    namespace "com.bzahov.rowingbg"
    compileSdk 33

    defaultConfig {
        applicationId "com.bzahov.rowingbg"
        minSdk 21
        targetSdk 33
        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 = JavaVersion.VERSION_1_8
    }
    buildFeatures {
        viewBinding true
    }
}

dependencies {
    //Firebase
    implementation platform("com.google.firebase:firebase-bom:$firebase_bom_version")
    implementation "com.google.firebase:firebase-analytics-ktx"
    implementation "com.google.firebase:firebase-auth-ktx"
    implementation "com.google.firebase:firebase-firestore-ktx"
    implementation "com.google.firebase:firebase-database-ktx"
    implementation "com.google.firebase:firebase-storage-ktx"
    //Play Services Auth
    implementation "com.google.android.gms:play-services-auth:$play_services_auth_version"
}

请注意,这些依赖项将用于实现 Firebase 产品。但是,您还需要为 Android 添加相应的依赖项。

评论

1赞 Red Coder 5/30/2023
谢谢!!昨天我删除了 firebase-core,然后按照@Silviya的建议发现我应该从 gms:play-services-auth 中删除 -ktx。两个答案都解决了问题