且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Android espresso-contrib gradle构建失败

更新时间:2022-04-09 09:51:13

TL; DR;

新版本的 espresso-contrib 2.2.2 库现在具有对com.android.support:appcompat-v7:23.1.1的依赖性,导致在我们的compile时间依赖性中使用不同版本的appcompat-v7时发生冲突,如下所示:

New version of espresso-contrib 2.2.2 library has now dependency on com.android.support:appcompat-v7:23.1.1 resulting into conflict when using different version of appcompat-v7 in our compile time dependency like below:

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的某些值依赖关系,它再次中断.

To avoid conflict when we exclude appcompat-v7 dependency from espresso-contrib like below it breaks again due to some value dependencies on 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'
}

错误:

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

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

根本原因:

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

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依赖项,如下所示:

So, the solution to above problem is to exclude 'design-support' lib dependency from espresso-contrib like below:

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'
}

解决了冲突问题!

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

LONGER VERSION (in case someone is interested):

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

To found out the reasons of various conflict issues we face when using `espresso-contrib' library i have created sample app to find out the root cause.

Step 1:Using Espresso-Contrib Lib version 2.2.1

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

Created App to use 'espresso-contrib' lib version 2.2.1 by adding following lines in app/build.gradle file:

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.

Note: In this case,I am not importing any other support library components like
appcompat-v7,recyclerview-v7,etc.

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

The dependency graph for the above setup looks like below:

可以看出,espresso-contrib 2.2.1 lib对
的23.0.1版本具有传递依赖关系. support-v4recyclerview-v7support-annotations

As can be seen that espresso-contrib 2.2.1lib has transitive dependencies on version 23.0.1 of
support-v4,recyclerview-v7,support-annotations ,etc.

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

As i am not defining dependencies for recyclerview-v7,support-annotations in my project the above setup would work just fine.

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

But when we define those as compile dependencies [like below] in our project we get version conflict issues as stated in your question.

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

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

To avoid those conflicts we add below line to our 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'
}

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

This makes sure those dependencies aren't downloaded as part of espresso-contrib transitive dependencies.
Everything runs fine with above setup.No issues!

Step 2: Using Espresso-Contrib lib version 2.2.2

通过更改先前的build.gradle文件,将应用程序的build.gradle更改为使用'espresso-contrib' lib版本 2.2.2

Changed App's build.gradle to use 'espresso-contrib' lib version 2.2.2 by changing the previous build.gradle file:

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'
  }
}

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

But when i build project using above setup..build fails with error posted in question..

错误:

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

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

So, looking at error i added one more line to above build.gradle:

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

但是,这不能解决冲突问题,并且我在注释中发布了值依赖错误.
所以我再次检查我的应用程序的依赖关系图:

But that doesn't resolves the conflict issue and i get value dependencies error posted in comments.
So i check for dependency graph of my app again:

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

As can be seen now that espresso-contrib 2.2.2 lib has now transitive dependency on com.android.support:design:23.1.1 causing the above conflict.

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

So, we need to add below line inside androidTestCompile ('com.android.support.test.espresso:espresso-contrib:2.2.2') block:

exclude module: 'design'

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

This resolves the conflict issue in lib version 2.2.2!