Android gradle错误:所有com.android.support库都必须使用完全相同的版本规范吗?

时间:2019-06-20 20:19:51

标签: android gradle dependencies

您会在stackoverflow上找到许多标题相同的问题。不幸的是,这些都没有解决我的问题。

我遇到以下错误:

所有com.android.support库必须使用完全相同的版本规范(混合版本可能导致运行时崩溃)。找到版本26.1.0,25.3.1。示例包括com.android.support:support-compat:26.1.0com.android.support:animated-vector-drawable:25.3.1

在此行(模块应用)上:

compile "com.android.support:appcompat-v7:$android_support_version"

但是我不明白为什么?在哪里可以使用26.1.0版?

这是我的文件(项目文件):

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

buildscript {
    ext {
        android_support_version ="25.3.1"
        archVersion = "1.0.0-alpha3"
    }
    repositories {
        jcenter()
        google()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.4.1'

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

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

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

模块应用:

apply plugin: 'com.android.application'

android {
    compileSdkVersion 25
    defaultConfig {
        applicationId "com.bsobat.github"
        minSdkVersion 16
        targetSdkVersion 25
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
    lintOptions {
        abortOnError false
    }
    dataBinding {
        enabled = true
    }
    testOptions {
        unitTests.returnDefaultValues = true
    }
}

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:$android_support_version"
    compile "com.android.support:support-v13:$android_support_version"
    compile "com.android.support:design:$android_support_version"
    compile "com.android.support:recyclerview-v7:$android_support_version"
    compile "com.android.support:cardview-v7:$android_support_version"

    compile 'com.android.support.constraint:constraint-layout:1.0.2'

    testCompile 'junit:junit:4.12'

    //dagger2
    compile "com.google.dagger:dagger:2.9"
    annotationProcessor "com.google.dagger:dagger-compiler:2.9"
    provided 'javax.annotation:jsr250-api:1.0'
    compile 'javax.inject:javax.inject:1'


    //android arch. component
    compile "android.arch.lifecycle:runtime:$archVersion"
    compile "android.arch.lifecycle:extensions:$archVersion"
    annotationProcessor "android.arch.lifecycle:compiler:$archVersion"
    compile "android.arch.persistence.room:runtime:$archVersion"
    annotationProcessor "android.arch.persistence.room:compiler:$archVersion"

    //Retrofit
    compile 'com.squareup.retrofit2:retrofit:2.2.0'

    //OkHttp
    compile 'com.squareup.okhttp3:okhttp:3.2.0'
    compile 'com.squareup.okio:okio:1.7.0'

    //Gson
    compile 'com.google.code.gson:gson:2.6.2'
    compile 'com.squareup.retrofit2:converter-gson:2.2.0'

    //Glide
    compile 'com.github.bumptech.glide:glide:4.0.0-RC0'


    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
        exclude group: 'com.android.support', module: 'support-annotations'
    })
    testCompile 'junit:junit:4.12'
    testCompile 'org.mockito:mockito-core:1.10.19'
    testCompile("android.arch.core:core-testing:$archVersion", {
        exclude group: 'com.android.support', module: 'support-compat'
        exclude group: 'com.android.support', module: 'support-annotations'
        exclude group: 'com.android.support', module: 'support-core-utils'
    })

}

1 个答案:

答案 0 :(得分:0)

您需要在应用程序级别gradle文件的底部添加以下代码块。

configurations.all {
    resolutionStrategy.eachDependency { DependencyResolveDetails details ->
        def requested = details.requested
        if (requested.group == 'com.android.support') {
            if (!requested.name.startsWith("multidex")) {
                details.useVersion '25.3.1'
            }
        }
    }
}