Android dependency has different version for the compile and runtime

时间:2017-06-20 12:36:27

标签: android android-gradle build.gradle

After updating Android Studio from Canary 3 to Canary 4, the following error is thrown at the build time.

Android dependency 'com.android.support:support-support-v4' has different version for the compile (25.2.0) and runtime (26.0.0-beta2) classpath. You should manually set the same version via DependencyResolution.

I ran a complete search throughout the project and the version 25.1.0 is no where used.

App-build.gradle

android {
compileSdkVersion 26
buildToolsVersion '26.0.0'


defaultConfig {
    applicationId "com.xxx.xxxx"
    minSdkVersion 14
    targetSdkVersion
    versionCode 1
    versionName "1.0"
    multiDexEnabled true

}


buildTypes {
    debug {
        debuggable true
    }
    release {
        debuggable false
        minifyEnabled true
        shrinkResources true
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
    }

    lintOptions {
        abortOnError false
    }

}}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
testImplementation 'junit:junit:4.12'
implementation project(':core')
implementation com.google.android.gms:play-services-gcm:9.0.0'

implementation('com.crashlytics.sdk.android:crashlytics:2.6.5@aar') {
    transitive = true
}
implementation 'com.android.support:multidex:1.0.1'
implementation 'com.flurry.android:analytics:7.0.0'
annotationProcessor 'com.jakewharton:butterknife-compiler:8.6.0'
implementation 'com.jakewharton:butterknife:8.6.0'
implementation 'com.android.support.constraint:constraint-layout:1.0.2'
}

Library-build.gradle:

apply plugin: 'com.android.library'
android {
compileSdkVersion 26
buildToolsVersion '26.0.0'

defaultConfig {
    minSdkVersion 14
    targetSdkVersion
    versionCode 1
    versionName "1.0"
}

}

dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation files('libs/model.jar')
testImplementation 'junit:junit:4.12'
implementation 'com.android.support:percent:26.0.0-beta2'
implementation 'com.android.support:appcompat-v7:26.0.0-beta2'
implementation 'com.android.support:support-core-utils:26.0.0-beta2'

implementation 'com.squareup.retrofit2:retrofit:2.0.2'
implementation 'com.squareup.picasso:picasso:2.4.0'
implementation 'com.squareup.retrofit2:converter-gson:2.0.2'
implementation 'com.squareup.okhttp3:logging-interceptor:3.2.0'
implementation 'uk.co.chrisjenx:calligraphy:2.2.0'
implementation 'com.google.code.gson:gson:2.2.4'
implementation 'com.android.support:design:26.0.0-beta2'
implementation 'com.github.PhilJay:MPAndroidChart:v3.0.1'

}

Note: Project was building fine in Canary 3

17 个答案:

答案 0 :(得分:114)

在您的buildscript(build.gradle root)中使用此代码:

subprojects {
  project.configurations.all {
     resolutionStrategy.eachDependency { details ->
        if (details.requested.group == 'com.android.support'
              && !details.requested.name.contains('multidex') ) {
           details.useVersion "version which should be used - in your case 26.0.0-beta2"
        }
     }
  }
}

答案 1 :(得分:78)

我有同样的错误,解决了我的问题是什么。在我的中,而不是使用编译或实现,我使用“api”。所以最后我的依赖:

dependencies {
api fileTree(dir: 'libs', include: ['*.jar'])
api files('libs/model.jar')
testApi 'junit:junit:4.12'
api 'com.android.support:percent:26.0.0-beta2'
api 'com.android.support:appcompat-v7:26.0.0-beta2'
api 'com.android.support:support-core-utils:26.0.0-beta2'

api 'com.squareup.retrofit2:retrofit:2.0.2'
api 'com.squareup.picasso:picasso:2.4.0'
api 'com.squareup.retrofit2:converter-gson:2.0.2'
api 'com.squareup.okhttp3:logging-interceptor:3.2.0'
api 'uk.co.chrisjenx:calligraphy:2.2.0'
api 'com.google.code.gson:gson:2.2.4'
api 'com.android.support:design:26.0.0-beta2'
api 'com.github.PhilJay:MPAndroidChart:v3.0.1'
}

您可以在此链接https://stackoverflow.com/a/44493379/3479489

中找到有关“api”,“实施”的更多信息

答案 2 :(得分:22)

通过为项目运行正确的gradle -q dependencies命令,您应该能够确切地看到奇数版本中的哪个依赖项作为传递依赖项:

https://docs.gradle.org/current/userguide/userguide_single.html#sec:listing_dependencies

一旦你追踪到它的内容,你可以在gradle文件中添加一个排除到特定的依赖项,例如:

implementation("XXXXX") {
    exclude group: 'com.android.support', module: 'support-compat'
}

答案 3 :(得分:15)

经过大量的时间并从一位比我更了解android的朋友那里获得帮助: 应用程序/的build.gradle

android {
    compileSdkVersion 27

    // org.gradle.caching = true

    defaultConfig {
        applicationId "com.cryptoviewer"
        minSdkVersion 16
        targetSdkVersion 23
        versionCode 196
        versionName "16.83"
        // ndk {
        //     abiFilters "armeabi-v7a", "x86"
        // }
    }

和依赖

dependencies {
    implementation project(':react-native-camera')
   //...
    implementation "com.android.support:appcompat-v7:26.1.0" // <= YOU CARE ABOUT THIS
    implementation "com.facebook.react:react-native:+"  // From node_modules
}
build.gradle中的

allprojects {
   //...
    configurations.all {
        resolutionStrategy.force "com.android.support:support-v4:26.1.0"
    }
gradle.properties中的

android.useDeprecatedNdk=true
android.enableAapt2=false
org.gradle.jvmargs=-Xmx4608M

答案 4 :(得分:5)

对我来说,答案是也将其添加到我的SELECT * FROM books where id= 3 and status like '%public%' 文件中:

build.gradle

就我而言,必须将解决方案策略放在configurations.all { resolutionStrategy.eachDependency { details -> if (details.requested.group == 'com.android.support' && !details.requested.name.contains('multidex') ) { details.useVersion "26.1.0" } } } 块中。我将configurations.all { .. }块直接放入了configurations.all文件中(即app/build.gradle没有嵌套在其他任何文件中)

答案 5 :(得分:4)

这对我有用:

在依赖项部分的app/build.gradle中添加以下行:

implementation "com.android.support:appcompat-v7:27.1.0"

:27.1.1

答案 6 :(得分:3)

将此代码添加到项目级别的build.gradle文件中。

subprojects {
    project.configurations.all {
        resolutionStrategy.eachDependency { details ->
            if (details.requested.group == 'com.android.support'
                    && !details.requested.name.contains('multidex') ) {
                details.useVersion "version which should be used - in your case 28.0.0-beta2"
            }
        }
    }
}

示例代码:

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

buildscript {

    repositories {
        google()
        jcenter()
        maven { url 'https://maven.fabric.io/public' }
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.2.0'
        classpath 'io.fabric.tools:gradle:1.31.0'

        // 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
}


subprojects {
    project.configurations.all {
        resolutionStrategy.eachDependency { details ->
            if (details.requested.group == 'com.android.support'
                    && !details.requested.name.contains('multidex') ) {
                details.useVersion "28.0.0"
            }
        }
    }
}

答案 7 :(得分:2)

我按照Eddi上面提到的那样解决了这个问题,

 resolutionStrategy.eachDependency { details ->
            if (details.requested.group == 'com.android.support'
                    && !details.requested.name.contains('multidex') ) {
                details.useVersion "26.1.0"
            }
        }

答案 8 :(得分:2)

将冲突的实现从实现切换到api可以解决问题。这是minderks撰写的一篇很好的文章,解释了它们之间的区别。

https://medium.com/mindorks/implementation-vs-api-in-gradle-3-0-494c817a6fa

编辑:

这也是我的依赖解决方案

 subprojects {
        project.configurations.all {
            resolutionStrategy.eachDependency { details ->
                if (details.requested.group == 'com.android.support'
                        && !details.requested.name.contains('multidex')) {
                    details.useVersion "28.0.0"
                }
                if (details.requested.group == 'com.google.android.gms'
                        && details.requested.name.contains('play-services-base')) {
                    details.useVersion "15.0.1"
                }
                if (details.requested.group == 'com.google.android.gms'
                        && details.requested.name.contains('play-services-tasks')) {
                    details.useVersion "15.0.1"
                }
            }
        }
    }

答案 9 :(得分:2)

在您的图书馆项目中查看,使 compileSdkVersion targetSdkVersion 版本与您的应用程序相同

android {
    compileSdkVersion 28

    defaultConfig {
        consumerProguardFiles 'proguard-rules.txt'
        minSdkVersion 14
        targetSdkVersion 28
    }
}

还将所有依赖项设置为同一级别

答案 10 :(得分:1)

我注释了//api 'com.google.android.gms:play-services-ads:15.0.1',它在同步后对我有用

答案 11 :(得分:1)

只需将这些行添加到build.gradle文件中

resolutionStrategy.force“ com.android.support:support-media-compat:26.0.0-beta2”

resolutionStrategy.force“ com.android.support:support-v4:26.0.0-beta2”

答案 12 :(得分:1)

我通过升级android / build.gradle文件中的gradle依赖关系来解决该问题:classpath'com.android.tools.build:gradle:3.3.1'(我以前使用的是3.2版。

答案 13 :(得分:0)

如果有人在2019年遇到此依赖关系问题,请将Android Studio更新到3.4或更高版本

答案 14 :(得分:0)

在我的情况下,我在两个不同的模块中拥有以下实现的两个不同版本,因此我将两个实现都更改为版本:即6.0.2,并且可以正常工作。您可能还需要编写依赖性解析,请参见接受的答案。

应用程序模块

implementation 'com.karumi:dexter:5.0.0'

通用模块

implementation 'com.karumi:dexter:6.0.2'

答案 15 :(得分:0)

    configurations.all {
        resolutionStrategy.force
        //"com.android.support:appcompat-v7:25.3.1"
        //here put the library that made the error with the version you want to use
    }

将此添加到allprojects内的gradle(项目)中

答案 16 :(得分:-6)

将硬编码版本替换为+ 例如:

implementation 'com.google.android.gms:play-services-base:+'
implementation 'com.google.android.gms:play-services-maps:+'