如何在 Android 的 gradle 文件中为 Firebase for database 和 auth 添加依赖项?

新手上路,请多包涵

我对 android 非常陌生,正在尝试 firebase,我正在尝试 firebase auth 和 firebase 数据库。

我可以成功完成身份验证功能,下面是紧随其后的应用程序模块的 gradle 文件内容。

 apply plugin: 'com.android.application'

android {
    compileSdkVersion 24
    buildToolsVersion "24.0.3"
    defaultConfig {
        applicationId "com.cirvi.laxmimobileshopee"
        minSdkVersion 15
        targetSdkVersion 24
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
        exclude group: 'com.android.support', module: 'support-annotations'
    })
    compile 'com.android.support:appcompat-v7:24.2.1'
    compile 'com.android.support:design:24.2.1'
    compile 'com.android.support:support-v4:24.2.1'
    compile 'com.android.support:recyclerview-v7:24.2.1'

    compile "com.google.firebase:firebase-auth:9.0.2"

    testCompile 'junit:junit:4.12'
}

apply plugin: 'com.google.gms.google-services'

现在下一步我想在同一个项目中使用 firebase 数据库,我通过添加依赖项更新 gradle 文件

compile 'com.google.firebase:firebase-database:9.6.1'

一旦我这样做并开始同步,我就会收到以下错误

Information:Gradle tasks [:app:generateDebugSources, :app:generateDebugAndroidTestSources, :app:mockableAndroidJar, :app:prepareDebugUnitTestDependencies]
Error:Execution failed for task ':app:processDebugGoogleServices'.
> Please fix the version conflict either by updating the version of the google-services plugin (information about the latest version is available at https://bintray.com/android/android-tools/com.google.gms.google-services/) or updating the version of com.google.android.gms to 9.6.1.
Information:BUILD FAILED
Information:Total time: 2.86 secs
Information:1 error
Information:0 warnings
Information:See complete output in console

下面是我的项目 gradle 文件的代码。

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

buildscript {
    repositories {
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:2.2.0'
        classpath 'com.google.gms:google-services:3.1.0'

        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

allprojects {
    repositories {
        jcenter()
    }
}

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

我试图更新项目 gradle 文件中的谷歌服务版本,但随后遇到了其他问题。由于我是 android 的新手并正在尝试尝试,所以我被卡住了,目前无法弄清楚如何解决这个问题。谁能指导我或帮助我?

项目结构

原文由 Chetan 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 289
2 个回答

试试这个

转到项目级别 build.gradle 并检查它是否看起来完全像这样:

 allprojects {
    repositories {
        jcenter()
        maven {
            url "https://maven.google.com"
        }
    }
}

为两者设置相同的依赖关系

implementation "com.google.firebase:firebase-auth:9.6.1"
implementation 'com.google.firebase:firebase-database:9.6.1'

原文由 Ninja 发布,翻译遵循 CC BY-SA 4.0 许可协议

试试这个: Build > Edit Libraries and Dependencies…

在此处输入图像描述在此处输入图像描述


现在,搜索 可用 模块:

在此处输入图像描述

原文由 ILYAS_Kerbal 发布,翻译遵循 CC BY-SA 3.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题