本文介绍了我的具有Android测试的Android应用中的依赖项冲突错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用AndroidStudio和Gradle在源代码"androidTest"中构建带有测试的Android应用.我添加了一个新的依赖项,现在在AndroidStudio或通过"./gradlew connectedCheck"运行Android测试时遇到以下问题.解决此问题的首选方法是什么?

I'm using AndroidStudio and Gradle to build my Android app with tests in the 'androidTest' source directory. I added a new dependency and am now getting the following issue when running Android Tests either in AndroidStudio or via './gradlew connectedCheck'. What's the preferred way to resolve this?

'警告:与依赖项'org.somelibrary:library-core'冲突.应用程序和测试应用程序的已解决版本不同.'

'Warning:Conflict with dependency 'org.somelibrary:library-core'. Resolved versions for app and test app differ.'

从Android Gradle Plugin 1.1.1开始,错误显示如下:警告:与依赖项'com.google.code.findbugs:jsr305'发生冲突.应用(1.3.9)和测试应用(2.0.1)的已解决版本不同."

As of Android Gradle Plugin 1.1.1 the error displays like this:"Warning:Conflict with dependency 'com.google.code.findbugs:jsr305'. Resolved versions for app (1.3.9) and test app (2.0.1) differ."

推荐答案

为您的应用构建并运行Android测试时,Android Gradle插件会生成两个APK(应用和测试APK).在gradle运行期间,将比较应用程序和测试版本的依赖关系.当版本号相同时,两者中都存在的依赖项将从测试版本中删除.如果使用相同的依存关系,但版本号不同,则需要手动解决依存关系冲突,并且会出现此错误.

When you build and run Android Tests for your app the Android Gradle plugin builds two APKs (the app and the test APK). During the gradle run the dependencies for the app and test builds are compared. Dependencies that exist in both are removed from the test build when the version numbers are the same. When the same dependencies are in use, but differ by version number then you will need to manually resolve the dependency conflict and this error is presented.

要解决冲突,您首先需要弄清楚两个冲突的版本.如果您尚未使用Android Gradle插件v1.1.1 +,则如果升级到该版本,错误消息将为您提供冲突的版本号.选择您需要的那个.

To resolve the conflict you first need to figure out the two versions that are conflicting. If you aren't already using the Android Gradle Plugin v1.1.1+ then if you upgrade to that version the error message will give you the conflicting version numbers. Choose which one you need.

*在冲突编号之间进行选择时,请务必记住,除非您已覆盖默认的gradle依赖关系解决策略(),然后在应用程序内部发生冲突,并通过选择更大的版本来(单独)解决测试版本.

*When choosing between the conflict numbers it might be important to keep in mind that unless you've overridden the default gradle dependency resolution strategy (failOnVersionConflict) then conflicts internally within the app and test builds (separately) will be resolved by choosing the greater version.

现在,您需要确定如何解决冲突.如果您需要强制使用库的较低版本(1.2),则需要强制将应用程序和测试版本的依赖关系解析为库的特定版本,如下所示:

Now you need to decide how to resolve the conflict. If you need to force the use of the lower version (1.2) of the library you will need to force the dependency to be resolved for both the app and test builds to a specific version of the library like this:

// Needed to resolve app vs test dependencies, specifically, transitive dependencies of
// libraryq and libraryz. Forcing the use of the smaller version after regression testing.
configurations.all {
    resolutionStrategy.force 'org.somelibrary:library-core:1.2'
}

如果需要使用2.1版本的依赖项,那么也可以使用上面的代码段,但是无论传递依赖项更新是否需要它,您都永远不会开始使用库的较新版本.另外,您也可以向应用程序或测试版本添加新的常规依赖项(无论使用哪种1.2版本的依赖项).这将迫使应用程序或测试版本依赖于(先前提到的)gradle依赖关系解析策略,因此对该版本使用库的2.1版本.

If you need to use the 2.1 version of the dependency then you can use the snippet above as well, but you will never start using a newer version of the library regardless of whether transitive dependency updates require it. Alternatively, you can also add a new normal dependency to either the app or the test builds (whichever was trying to use the 1.2 version of the dependency). This will force the app or test build to depend on the (previously mentioned) gradle dependency resolution strategy and therefore use the 2.1 version of the library for that build.

// Force the use of 2.1 because the app requires that version in libraryq transitively.
androidTestCompile 'org.somelibrary:library-core:2.1'

// Force the use of 2.1 because the Android Tests require that version in libraryz.
compile 'org.somelibrary:library-core:2.1'

在此解决方案中,该错误可能会重新出现(例如说版本3.3),开始仅在测试或应用程序构建之一中使用,但这通常是可以的,因为在构建时会通知您另一个不兼容问题,并且可以采取行动.

In this solution the error could resurface, if say version 3.3, started to be used in only one of either the test or the app builds, but this is typically OK because you'll be notified of another incompatibility at build time and can take action.

更新:该问题的一些新解决方案现在也列出了从声明的依赖项中排除特定传递性依赖项的情况.这是一个有效的解决方案,但会使开发人员承担更多责任.与上面的强制依赖项解决方案建议将版本硬编码到版本中一样,exclude-transitive-dependency解决方案专门覆盖了库的规定要求.有时,库开发人员会在其他库中遇到bug或解决bug,因此,在实施这些解决方案时,您可能会冒着必须追逐非常晦涩的bug的风险.

Update: A few new solutions to this question now also list excluding a particular transitive dependency from a declared dependency. This is a valid solution, but puts more onus on the developers. In the same way that the forced dependency resolution suggestion above above hard codes a version into the build, the exclude-transitive-dependency solution specifically overrides the stated requirements of a library. Sometimes library developers have bugs or work around bugs in various other libraries so when you implement these solutions you take some risk in potentially having to chase down very obscure bugs.

这篇关于我的具有Android测试的Android应用中的依赖项冲突错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-18 14:31
查看更多