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

Mab*_*bad 37 testing dependencies android android-support-library

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

  • 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'
}
Run Code Online (Sandbox Code Playgroud)

alb*_*elu 52

我将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'
Run Code Online (Sandbox Code Playgroud)

现在两个都解析为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'
Run Code Online (Sandbox Code Playgroud)

Update3:CommonsWare的另一个很好的回应.

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

./gradlew -q yourmodule:dependencies
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

在你的情况下,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来检查依赖关系树.


And*_*rey 26

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

androidTestCompile 'com.android.support:support-annotations:23.2.0'
Run Code Online (Sandbox Code Playgroud)


Dmi*_*try 15

我遇到了同样的问题,解决了这个问题:

// 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'
    }
}
Run Code Online (Sandbox Code Playgroud)

解决方案在这里找到: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'
    }
}
Run Code Online (Sandbox Code Playgroud)


Phi*_*hil 7

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

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

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