Android espresso-contrib gradle 构建失败

Android espresso-contrib gradle build failing

我正在尝试学习 android espresso.. 我遵循了一些基本教程并且效果很好。但现在我想对 android 导航抽屉做一些测试。为此,我需要使用 gradle dependency androidTestCompile 'com.android.support.test.espresso:espresso-contrib:2.2.2' 但它会导致与其他依赖项发生冲突。我的 gradle 文件:

apply plugin: 'com.android.application'

android {
compileSdkVersion 23
buildToolsVersion "23.0.3"

defaultConfig {
    applicationId "my.com.myapp_android"
    minSdkVersion 18
    targetSdkVersion 23
    versionCode 1
    versionName "1.0"
    testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
    release {
        minifyEnabled false
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
    }
}
}
repositories {
jcenter()
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
//material design
compile 'com.android.support:appcompat-v7:23.3.0'
compile 'com.android.support:support-v4:23.3.0'

//zxing
compile 'com.journeyapps:zxing-android-embedded:3.2.0@aar'
compile 'com.google.zxing:core:3.2.1'

//Testing
// Optional -- Mockito framework
testCompile 'org.mockito:mockito-core:1.10.19'
androidTestCompile 'com.android.support:support-annotations:23.3.0'
androidTestCompile 'com.android.support.test:runner:0.5'
androidTestCompile 'com.android.support.test:rules:0.4.1'
// Optional -- Hamcrest library
androidTestCompile 'org.hamcrest:hamcrest-library:1.3'
// Optional -- UI testing with Espresso
androidTestCompile 'com.android.support.test.espresso:espresso-core:2.2.2'
androidTestCompile 'com.android.support.test.espresso:espresso-contrib:2.2.2'
// Optional -- UI testing with UI Automator
androidTestCompile 'com.android.support.test.uiautomator:uiautomator-v18:2.1.1'




//inMarketSDK
//compile group: 'com.inmarket', name: 'm2msdk', version: '2.29', ext: 'aar'

}

错误是这样的:

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

关注此:link 用于浓缩咖啡安装

我也尝试排除注释依赖:

 androidTestCompile ('com.android.support.test.espresso:espresso-core:2.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.espresso:espresso-contrib:2.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.espresso:espresso-contrib:2.2.1'){
    exclude module: 'support-annotations'
    exclude module: 'support-v4'
    exclude module: 'recyclerview-v7'
}

TL;DR;

espresso-contrib 2.2.2 库的新版本现在依赖于 com.android.support:appcompat-v7:23.1.1,导致在 [=] 中使用不同版本的 appcompat-v7 时发生冲突24=] 如下所示的时间依赖性:

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

     androidTestCompile 'com.android.support.test.espresso:espresso-contrib:2.2.2'
}

为了避免冲突,当我们从 espresso-contrib 中排除 appcompat-v7 依赖时,如下所示,由于对 design support lib.

的某些值依赖,它再次中断
androidTestCompile ('com.android.support.test.espresso:espresso-contrib:2.2.2'){
    exclude module: 'support-annotations'
    exclude module: 'support-v4'
    exclude module: 'support-v13'
    exclude module: 'recyclerview-v7'
    exclude module: 'appcompat-v7'
}

错误:

Error:(69) Error retrieving parent for item: No resource found that matches the given name 'TextAppearance.AppCompat.Display1'.

根本原因:

This is because the design support lib has dependency on appcompat-v7.
So,when we exclude 'appcompat-v7' module from espresso-contrib dependencies(like above) , the design support lib downloaded as part of transitive dependency of espresso-contrib lib couldn't find the compatible version of appcompat-v7 lib(23.1.1) it is using internally in its resources files and thus gives out the above error.

因此,上述问题的解决方案是从 espresso-contrib 中排除 'design-support' lib 依赖项,如下所示:

androidTestCompile ('com.android.support.test.espresso:espresso-contrib:2.2.2'){
    exclude module: 'support-annotations'
    exclude module: 'support-v4'
    exclude module: 'support-v13'
    exclude module: 'recyclerview-v7'
    exclude module: 'design'
}

冲突问题解决了!

加长版(以防有人感兴趣):

为了找出我们在使用“espresso-contrib”库时遇到的各种冲突问题的原因,我创建了示例应用程序来找出根本原因。

Step 1:Using Espresso-Contrib Lib version 2.2.1

创建应用程序以使用 'espresso-contrib' lib 版本 2.2.1 通过在 app/build.gradle 文件中添加以下行:

dependencies {
     compile fileTree(dir: 'libs', include: ['*.jar'])
     testCompile 'junit:junit:4.12'

     androidTestCompile 'com.android.support.test.espresso:espresso-contrib:2.2.1'

}

注意:在这种情况下,我没有导入任何其他支持库组件,例如
appcompat-v7,recyclerview-v7,etc.

上述设置的依赖关系图如下所示:

可以看出espresso-contrib 2.2.1lib对
的23.0.1版本有传递依赖 support-v4recyclerview-v7support-annotations 等。

因为我没有在我的项目中为 recyclerview-v7support-annotations 定义依赖项,所以上面的设置可以正常工作。

但是当我们在我们的项目中将它们定义为编译依赖项时 [如下所示],我们会遇到您问题中所述的版本冲突问题。

compile 'com.android.support:appcompat-v7:23.3.0'
compile 'com.android.support:support-v4:23.3.0'

为了避免这些冲突,我们将以下行添加到我们的 espresso-contrib 库中:

    androidTestCompile ('com.android.support.test.espresso:espresso-contrib:2.2.1'){
    exclude module: 'support-annotations'
    exclude module: 'support-v4'
    exclude module: 'support-v13'
    exclude module: 'recyclerview-v7'
}

这确保这些依赖项不会作为 espresso-contrib 传递依赖项的一部分下载。
以上 setup.No 问题一切正常!

Step 2: Using Espresso-Contrib lib version 2.2.2

通过更改之前的 build.gradle 文件:

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

androidTestCompile ('com.android.support.test.espresso:espresso-contrib:2.2.2'){
    exclude module: 'support-annotations'
    exclude module: 'support-v4'
    exclude module: 'support-v13'
    exclude module: 'recyclerview-v7'
  }
}

但是当我使用上面的设置构建项目时..构建失败并出现有问题的错误..

错误:

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

因此,查看错误我在上面添加了一行 build.gradle:

exclude module: 'appcompat-v7' (inside androidTestCompile block of espresso-contrib)

但这并没有解决冲突问题,我在评论中发现值依赖错误。
所以我再次检查我的应用程序的依赖关系图:

现在可以看出,espresso-contrib 2.2.2 lib 现在对 com.android.support:design:23.1.1 具有传递依赖性,导致上述冲突。

因此,我们需要在 androidTestCompile ('com.android.support.test.espresso:espresso-contrib:2.2.2') 块中添加以下行:

exclude module: 'design'

这解决了 lib 版本 2.2.2 中的冲突问题!

出于某种原因,它也开始突然发生在我身上。

唯一解决的方法是使缓存无效并重新启动