繁体   English   中英

警告:与依赖项“com.android.support:support-annotations”冲突

[英]Warning:Conflict with dependency 'com.android.support:support-annotations'

我几乎尝试了书中的每一个技巧。

  • ResolutionStrategy.force
  • 不包括模块

但似乎没有任何效果,下面是我的build.gradle。 我正在使用Gradle版本1.2.3。 有人可以说清楚我的代码还有什么问题。

我唯一没试过的是改变Gradle的版本。 这是一个非常基本的Espresso测试案例。 谢谢!

apply plugin: 'com.android.application'
android {
    configurations.all {
        resolutionStrategy.force 'com.android.support:support-annotations:22.1.0'
    }
    compileSdkVersion 22
    buildToolsVersion "21.1.2"

    defaultConfig {
        applicationId "com.example.rasika.job"
        minSdkVersion 16
        targetSdkVersion 22
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidUnitRunner"

    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}
repositories {
    mavenCentral()
}
dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.android.support:appcompat-v7:22.1.1'

    testCompile 'junit:junit:4.12'

    androidTestCompile 'com.android.support.test:runner:0.3'
    androidTestCompile 'com.android.support.test:rules:0.3'
    androidTestCompile 'com.android.support.test.espresso:espresso-core:2.2'
    androidTestCompile 'com.android.support.test.uiautomator:uiautomator-v18:2.1.1'
    androidTestCompile 'com.android.support.test.espresso:espresso-contrib:2.0'
}

我将android-topeka google示例和更新的appcompat版本分解为23.1.0,同样的消息:

警告:与依赖项“com.android.support:support-annotations”冲突。 app(23.1.0)和测试app(23.0.1)的已解决版本有所不同。

我补充说:

androidTestCompile 'com.android.support:support-annotations:23.1.0'

现在两个都解析为23.1.0,警告消失,应用程序和测试仍然有效。

我不确定这是更好的解决方案,所以我正在寻找另一个但是找到了你的问题。

更新:阅读PaulR的这个好解释

Update2:确认, android测试谷歌样本做到了。

// Testing-only dependencies
// Force usage of support annotations in the test app, since it is internally used by the runner module.
androidTestCompile 'com.android.support:support-annotations:23.0.1'

Update3CommonsWare的另一个很好的回应

使用以下方法检查特定版本/冲突/解决方案

./gradlew -q yourmodule:dependencies

在此输入图像描述

在你的情况下,Appcompat是22.1.1,但你强迫22.1.0。

Update4: Android构建系统(Android Dev Summit 2015)中解释的依赖冲突。

在此输入图像描述

解决主要和测试APK之间的冲突

运行检测测试时,主APK和测试APK共享相同的类路径。 如果主APK和测试APK使用相同的库(例如Guava)但是在不同版本中,Gradle构建将失败。 如果gradle没有捕获到,那么您的应用程序在测试期间和正常运行期间可能会有不同的行为(包括其中一个案例中的崩溃)。

要使构建成功,只需确保两个APK使用相同的版本。 如果错误是关于间接依赖项 (您在build.gradle中未提及的库),则只需将新版本的依赖项添加 到需要它 的配置 (“compile”或“androidTestCompile”) 中。 您还可以使用Gradle的解决策略机制。 您可以通过运行./gradlew:app:dependencies和./gradlew:app:androidDependencies来检查依赖关系树。

我通过添加依赖来解决冲突:

androidTestCompile 'com.android.support:support-annotations:23.2.0'

我遇到了同样的问题,由此解决了:

// build.gradle
...
android {
    ...
    defaultConfig {
        ...
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
}

dependencies {
    ...
    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2') {
        // Necessary if your app targets Marshmallow (since Espresso
        // hasn't moved to Marshmallow yet)
        exclude group: 'com.android.support', module: 'support-annotations'
    }
    androidTestCompile('com.android.support.test:runner:0.3') {
        // Necessary if your app targets Marshmallow (since the test runner
        // hasn't moved to Marshmallow yet)
        exclude group: 'com.android.support', module: 'support-annotations'
    }
}

解决方案在这里找到: https//github.com/codepath/android_guides/wiki/UI-Testing-with-Espresso

更新:我的build.gradle中的依赖块最终看起来像这样:

dependencies {
    ...        
    compile 'com.android.support:appcompat-v7:23.2.1'
    compile 'com.android.support:support-v4:23.2.1'
    compile 'com.android.support:design:23.2.1'
    ...     

    // Necessary if your app targets Marshmallow (since Espresso
    // hasn't moved to Marshmallow yet)
    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2') {
        exclude group: 'com.android.support'
    }
    androidTestCompile('com.android.support.test.espresso:espresso-intents:2.2.2') {
        exclude group: 'com.android.support'
    }
    androidTestCompile('com.android.support.test:runner:0.5') {
        exclude group: 'com.android.support'
    }
    androidTestCompile('com.android.support.test:rules:0.5') {
        exclude group: 'com.android.support'
    }
    androidTestCompile('com.android.support.test.espresso:espresso-contrib:2.2.2') {
        exclude group: 'com.android.support'
    }
    androidTestCompile('com.android.support:support-annotations:23.2.1') {
        exclude group: 'com.android.support'
    }
    androidTestCompile('com.android.support.test.uiautomator:uiautomator-v18:2.1.2') {
        exclude group: 'com.android.support'
    }
}

最近添加uiautomator时发生了这种情况。 要解决此问题,您需要确定使用过时模块的依赖项或依赖项。 您可以通过将每个androidTestCompile依赖项包装到一个块中来完成此操作,如下所示:

androidTestCompile ('com.android.support.test.espresso:espresso-core:2.2') {
    transitive = false;
}

可能会破坏其他一些东西,所以你需要小心。 我能够确切地确定哪两个依赖项对我造成了这个问题,并且只是将这种阻塞机制添加到那些。

我通过从runner和espresso-core依赖项中排除support-annotation库来解决冲突:

androidTestCompile 'com.android.support.test:runner:0.5',{
        exclude group: 'com.android.support', module: 'support-annotations'
}
androidTestCompile ('com.android.support.test.espresso:espresso-core:2.2.2'){
        exclude group: 'com.android.support', module: 'support-annotations'
}

将其添加到主build.gradle:

allprojects {
    ...
    configurations.all {
        resolutionStrategy.force 'com.android.support:support-annotations:23.1.1'
    }
    ...
}

将以下代码添加到build.gradle文件中的依赖关系块

compile 'com.android.support:support-annotations:23.2.1'
testCompile 'com.android.support:support-annotations:23.2.1'
androidTestCompile 'com.android.support:support-annotations:23.2.1'

androidTestCompile更改为测试编译。 并提醒不要将其更改为编译,只需将此依赖项编译到我们的调试APK或测试APK。

对我来说这很好

dependencies {

    androidTestCompile 'com.android.support:support-annotations:23.1.1'
}

我通过添加依赖来解决冲突:

compile 'com.android.support:appcompat-v7:23.2.1'
 compile 'com.android.support:design:23.2.1'
...
 androidTestCompile 'com.android.support:support-annotations:23.2.1'

在进入问题说

无法解决

com.android.support:support-annotations:23.1.0

并试图在其他服务器中找到,

但解决我问题的是添加:

google-service.json 

来自的文件

https://developers.google.com/mobile/add

并将其复制并粘贴到

YourAndroidProject/app 

然后重新编译它,我希望你的代码会飞

用它来解决冲突

androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
    exclude group: 'com.android.support', module: 'support-annotations'
})

我通过添加依赖来解决冲突:

androidTestCompile "com.android.support:support-annotations:26.0.0-beta1"

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM