将Firebase添加到项目中会导致关于混合版本警告的提示,可能会导致运行时崩溃。

5
在一个空项目上,添加firebase后会出现以下错误:
图像版本:

mixed version can lead to runtime crashes

文本版本:

所有的com.android.support库必须使用完全相同的版本规范(混合版本可能会导致运行时崩溃)。发现了版本27.1.1、25.2.0。例如,com.android.support:animated-vector-drawable:27.1.1和com.android.support:support-media-compat:25.2.0。

另一个错误:

警告:配置'compile'已经过时,已被'replace'替换为'implementation'和'api'。

是否有可能的解决方案来消除这些警告?

我的项目build.gradle是:

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

buildscript {

    repositories {
        google()
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.1.1'


        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
        classpath 'com.google.gms:google-services:3.1.1'
    }
}

allprojects {
    repositories {
        google()
        jcenter()
    }
}

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

我的应用程序 build.gradle 是:

apply plugin: 'com.android.application'

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

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'com.android.support:appcompat-v7:27.1.1'
    implementation 'com.android.support.constraint:constraint-layout:1.0.2'
    implementation 'com.google.firebase:firebase-auth:11.6.0'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.1'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1'
}

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

在点踩之前,了解一下原因会更好。 - Pulkit
1个回答

6
错误发生的原因是因为 google-services 实现了一些库,这些库没有使用 27.1.1 版本。因此我们需要覆盖版本。
为了做到这一点,请尝试在 Android Studio 的终端中运行此命令:
./gradlew -q dependencies app:dependencies --configuration debugAndroidTestCompileClasspath

这将显示一个依赖项列表,其中显示了版本号,类似于以下内容:

list of dependencies

在这里,我们需要找到那些未被覆盖且版本号为25或26且未被覆盖至27的依赖项。

就像上面的例子中,我们有com.android.support:support-v4:25.2.0

现在我们只需要将其添加到依赖块中,并更新版本号即可。

最后,依赖块如下所示:

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'com.android.support:appcompat-v7:27.1.1'
    implementation 'com.android.support:support-v4:27.1.1'
    implementation 'com.android.support:support-media-compat:27.1.1'

    implementation 'com.android.support.constraint:constraint-layout:1.0.2'
    implementation 'com.google.firebase:firebase-auth:12.0.1'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.1'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1'
}

这里我们正在添加

implementation 'com.android.support:support-v4:27.1.1'
implementation 'com.android.support:support-media-compat:27.1.1'

为了修复第二个错误

警告:配置项'compile'已过时,应使用'implementation'和'api'。

在项目的build.gradle中更新

classpath 'com.google.gms:google-services:3.1.1'

classpath 'com.google.gms:google-services:3.2.0'

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接