Android版本24.0.0和Espresso 2.0 Gradle异常

5

我正试图在我的应用中使用Espresso 2.0来测试UI元素。然而,gradle不允许我这样做。它给了我这个消息

Error:Conflict with dependency 'com.android.support:support-annotations'. Resolved versions for app (24.0.0) and test app (23.1.1) differ. See http://g.co/androidstudio/app-test-app-conflict for details.

这是我的 Gradle 文件
apply plugin: 'com.android.application'

android {
compileSdkVersion 24
buildToolsVersion "24.0.0"

defaultConfig {
    applicationId "theo.testing.espressotutorial"
    minSdkVersion 14
    targetSdkVersion 24
    versionCode 1
    versionName "1.0"


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

packagingOptions{
    exclude 'LICENSE.txt'
  }
}

dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:24.0.0'

androidTestCompile 'com.android.support.test.espresso:espresso-core:2.0'

// Android JUnit Runner
androidTestCompile 'com.android.support.test:runner:0.5'
// JUnit4 Rules
androidTestCompile 'com.android.support.test:rules:0.5'
}

我应该更新测试运行器吗?这让我头疼。

谢谢,

Theo。

1个回答

12

Espresso有一些会导致问题的传递依赖项。您可以从androidTest依赖项中排除所有来自组com.android.support的依赖项:

排除掉所有来自组com.android.support的依赖项就可以解决因传递依赖项引起的问题。

// Exclude Espresso's transitive dependencies to all packages in group com.android.support
configurations.androidTestCompile.dependencies.each { androidTestCompileDependency ->
    androidTestCompileDependency.exclude group: 'com.android.support'
}

你应该使用Espresso 2.2.2版本!一个完整的示例:

dependencies {

    def espressoVersion = '2.2.2'
    def testRunnerVersion = '0.5'

    androidTestCompile "com.android.support.test:runner:${testRunnerVersion}"
    androidTestCompile "com.android.support.test:rules:${testRunnerVersion}"
    androidTestCompile "com.android.support.test.espresso:espresso-core:${espressoVersion}"
    androidTestCompile "com.android.support.test.espresso:espresso-contrib:${espressoVersion}"
    androidTestCompile "com.android.support.test.espresso:espresso-intents:${espressoVersion}"

    configurations.androidTestCompile.dependencies.each { androidTestCompileDependency ->
        androidTestCompileDependency.exclude group: 'com.android.support'
    }
}

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