Android espresso-contrib gradle构建失败

Hir*_*el_ 7 android-testing android-gradle-plugin android-espresso

我正在尝试学习android espresso ..我遵循了一些基本的教程,它工作正常.但现在我想在android导航抽屉上做一些测试.为此我需要使用gradle依赖androidTestCompile'c​​om.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'

}
Run Code Online (Sandbox Code Playgroud)

错误是这样的:

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.
Run Code Online (Sandbox Code Playgroud)

紧随其后:espresso安装链接

我还试图排除注释依赖:

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

Pun*_*itD 31

TL; DR;

新版本的espresso-contrib 2.2.2库现在依赖于在我们的时间依赖项中com.android.support:appcompat-v7:23.1.1使用不同版本时产生冲突,如下所示:appcompat-v7compile

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

为了避免在我们appcompat-v7espresso-contrib下面排除依赖性时发生冲突,由于对design supportlib的某些值依赖性,它会再次中断.

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

错误:

错误:(69)检索项目的父项时出错:找不到与给定名称"TextAppearance.AppCompat.Display1"匹配的资源.

根本原因:

这是因为design支持lib依赖于 appcompat-v7.
因此,当我们从espresso-contrib 依赖项(如上所述)中排除'appcompat-v7'模块时 ,作为designlib的传递依赖项的一部分下载的支持lib espresso-contrib无法找到它的兼容版本appcompat-v7 lib(23.1.1)在其资源文件中内部使用,从而给出了以上错误.

因此,上述问题的解决方案是从espresso-contrib下面排除"设计支持"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'
}
Run Code Online (Sandbox Code Playgroud)

这解决了冲突问题!

更长版本(如果有人有兴趣):

要找出我们在使用`espresso-contrib'库时遇到的各种冲突问题的原因,我已经创建了示例应用程序以找出根本原因.

Step 1:Using Espresso-Contrib Lib version 2.2.1
Run Code Online (Sandbox Code Playgroud)

创建应用程序使用"意式的contrib"库版本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'
Run Code Online (Sandbox Code Playgroud)

}

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

上述设置的依赖关系图如下所示:
在此输入图像描述

可以看出的是espresso-contrib 2.2.1解放运动在上的23.0.1版本的传递依赖
support-v4,recyclerview-v7,support-annotations,等.

由于我没有定义依赖项recyclerview-v7,support-annotations在我的项目中,上面的设置可以正常工作.

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

compile 'com.android.support:appcompat-v7:23.3.0'
compile 'com.android.support:support-v4:23.3.0'
Run Code Online (Sandbox Code Playgroud)

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

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

这可以确保不会将这些依赖项作为espresso-contrib传递依赖项的一部分下载.
以上设置一切正常.没有问题!

Step 2: Using Espresso-Contrib lib version 2.2.2
Run Code Online (Sandbox Code Playgroud)

通过更改以前的build.gradle文件,将App的build.gradle更改为使用'espresso- contrib'lib 2.2.2版:

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

但是,当我使用上面的setup..build构建项目失败并发布了错误的错误..

错误:

错误:与依赖项"com.android.support:appcompat-v7"冲突.app(23.3.0)和测试app(23.1.1)的已解决版本有所不同.有关详细信息,请参阅http://g.co/androidstudio/app-test-app-conflict.

所以,看错误,我在build.gradle上面添加了一行:

exclude module: 'appcompat-v7' (inside androidTestCompile block of espresso-contrib)
Run Code Online (Sandbox Code Playgroud)

但这并没有解决冲突问题,我在评论中发布了价值依赖性错误.
所以我再次检查我的应用程序的依赖图: 在此输入图像描述

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

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

exclude module: 'design'
Run Code Online (Sandbox Code Playgroud)

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