Gradle依赖项学习之依赖冲突解决总结

Gradle是一个非常好用的编译工具,特别是继承了maven的依赖项管理功能,需要的Library不需要像传统IDE一样手动下载复制到项目中,只需要简单的写一行gradle脚本,就能自动下载下来并编译。

但是有时候会出现各种不明情况的报错,最常见的一种原因就是依赖项版本冲突。

每个模块都可能依赖其他模块,这些模块又会依赖别的模块。而一个项目中的多个模块,对同一个模块的不同版本有依赖,就可能产生冲突。

通过gradle命令查看依赖树,可以比较直观的看到冲突。具体方法是在模块所在的目录,也即build.gradle所在目录下执行gradle dependencies(需要将gradle加入PATH环境变量),执行结果如图。

Transitive

Transitive用于自动处理子依赖项。默认为true,gradle自动添加子依赖项,形成一个多层树形结构;设置为false,则需要手动添加每个依赖项。

案例

以安卓单元测试espresso的配置为例,gradle依赖如下:

  1. dependencies {
  2. androidTestCompile('com.android.support.test:runner:0.2')
  3. androidTestCompile('com.android.support.test:rules:0.2')
  4. androidTestCompile('com.android.support.test.espresso:espresso-core:2.1')
  5. }

运行gradle dependencies的结果如下。可以看到每个包的依赖项都被递归分析并添加进来。

  1. +--- com.android.support.test:runner:0.2
  2. | +--- junit:junit-dep:4.10
  3. | | --- org.hamcrest:hamcrest-core:1.1
  4. | +--- com.android.support.test:exposed-instrumentation-api-publish:0.2
  5. | --- com.android.support:support-annotations:22.0.0
  6. +--- com.android.support.test:rules:0.2
  7. | --- com.android.support.test:runner:0.2 ()
  8. --- com.android.support.test.espresso:espresso-core:2.1
  9. +--- com.android.support.test:rules:0.2 ()
  10. +--- com.squareup:javawriter:2.1.1
  11. +--- org.hamcrest:hamcrest-integration:1.1
  12. | --- org.hamcrest:hamcrest-core:1.1
  13. +--- com.android.support.test.espresso:espresso-idling-resource:2.1
  14. +--- org.hamcrest:hamcrest-library:1.1
  15. | --- org.hamcrest:hamcrest-core:1.1
  16. +--- javax.inject:javax.inject:1
  17. +--- com.google.code.findbugs:jsr305:2.0.1
  18. +--- com.android.support.test:runner:0.2 ()
  19. +--- javax.annotation:javax.annotation-api:1.2
  20. --- org.hamcrest:hamcrest-core:1.1

统一指定transitive

可以给dependencies统一指定transitive为false,再次执行dependencies可以看到如下结果。

  1. configurations.all {
  2. transitive = false
  3. }
  4. dependencies {
  5. androidTestCompile('com.android.support.test:runner:0.2')
  6. androidTestCompile('com.android.support.test:rules:0.2')
  7. androidTestCompile('com.android.support.test.espresso:espresso-core:2.1')
  8. }
  1. +--- com.android.support.test:runner:0.2
  2. +--- com.android.support.test:rules:0.2
  3. --- com.android.support.test.espresso:espresso-core:2.1

单独指定依赖项的transitive

  1. dependencies {
  2. androidTestCompile('com.android.support.test:runner:0.2')
  3. androidTestCompile('com.android.support.test:rules:0.2')
  4. androidTestCompile('com.android.support.test.espresso:espresso-core:2.1') {
  5. transitive = false
  6. }
  7. }

版本冲突

在同一个配置下(例如androidTestCompile),某个模块的不同版本同时被依赖时,默认使用最新版,gradle同步时不会报错。例如下面的hamcrest-core和runner。

  1. dependencies {
  2. androidTestCompile('com.android.support.test:runner:0.4')
  3. androidTestCompile('com.android.support.test:rules:0.2')
  4. androidTestCompile('com.android.support.test.espresso:espresso-core:2.1')
  5. }
  1. +--- com.android.support.test:runner:0.4
  2. | +--- com.android.support:support-annotations:23.0.1
  3. | +--- junit:junit:4.12
  4. | | --- org.hamcrest:hamcrest-core:1.3
  5. | --- com.android.support.test:exposed-instrumentation-api-publish:0.4
  6. +--- com.android.support.test:rules:0.2
  7. | --- com.android.support.test:runner:0.2 -> 0.4 ()
  8. --- com.android.support.test.espresso:espresso-core:2.1
  9. +--- com.android.support.test:rules:0.2 ()
  10. +--- com.squareup:javawriter:2.1.1
  11. +--- org.hamcrest:hamcrest-integration:1.1
  12. | --- org.hamcrest:hamcrest-core:1.1 -> 1.3
  13. +--- com.android.support.test.espresso:espresso-idling-resource:2.1
  14. +--- org.hamcrest:hamcrest-library:1.1
  15. | --- org.hamcrest:hamcrest-core:1.1 -> 1.3
  16. +--- javax.inject:javax.inject:1
  17. +--- com.google.code.findbugs:jsr305:2.0.1
  18. +--- com.android.support.test:runner:0.2 -> 0.4 ()
  19. +--- javax.annotation:javax.annotation-api:1.2
  20. --- org.hamcrest:hamcrest-core:1.1 -> 1.3

Force

force强制设置某个模块的版本。

  1. configurations.all {
  2. resolutionStrategy {
  3. force 'org.hamcrest:hamcrest-core:1.3'
  4. }
  5. }
  6. dependencies {
  7. androidTestCompile('com.android.support.test:runner:0.2')
  8. androidTestCompile('com.android.support.test:rules:0.2')
  9. androidTestCompile('com.android.support.test.espresso:espresso-core:2.1')
  10. }

可以看到,原本对hamcrest-core 1.1的依赖,全部变成了1.3。

  1. +--- com.android.support.test:runner:0.2
  2. | +--- junit:junit-dep:4.10
  3. | | --- org.hamcrest:hamcrest-core:1.1 -> 1.3
  4. | +--- com.android.support.test:exposed-instrumentation-api-publish:0.2
  5. | --- com.android.support:support-annotations:22.0.0
  6. +--- com.android.support.test:rules:0.2
  7. | --- com.android.support.test:runner:0.2 ()
  8. --- com.android.support.test.espresso:espresso-core:2.1
  9. +--- com.android.support.test:rules:0.2 ()
  10. +--- com.squareup:javawriter:2.1.1
  11. +--- org.hamcrest:hamcrest-integration:1.1
  12. | --- org.hamcrest:hamcrest-core:1.1 -> 1.3
  13. +--- com.android.support.test.espresso:espresso-idling-resource:2.1
  14. +--- org.hamcrest:hamcrest-library:1.1
  15. | --- org.hamcrest:hamcrest-core:1.1 -> 1.3
  16. +--- javax.inject:javax.inject:1
  17. +--- com.google.code.findbugs:jsr305:2.0.1
  18. +--- com.android.support.test:runner:0.2 ()
  19. +--- javax.annotation:javax.annotation-api:1.2
  20. --- org.hamcrest:hamcrest-core:1.1 -> 1.3

Exclude

Exclude可以设置不编译指定的模块

  1. configurations {
  2. all.exclude group: 'org.hamcrest', module: 'hamcrest-core'
  3. }
  4. dependencies {
  5. androidTestCompile('com.android.support.test:runner:0.2')
  6. androidTestCompile('com.android.support.test:rules:0.2')
  7. androidTestCompile('com.android.support.test.espresso:espresso-core:2.1')
  8. }
  1. +--- com.android.support.test:runner:0.2
  2. | +--- junit:junit-dep:4.10
  3. | +--- com.android.support.test:exposed-instrumentation-api-publish:0.2
  4. | --- com.android.support:support-annotations:22.0.0
  5. +--- com.android.support.test:rules:0.2
  6. | --- com.android.support.test:runner:0.2 ()
  7. --- com.android.support.test.espresso:espresso-core:2.1
  8. +--- com.android.support.test:rules:0.2 ()
  9. +--- com.squareup:javawriter:2.1.1
  10. +--- org.hamcrest:hamcrest-integration:1.1
  11. +--- com.android.support.test.espresso:espresso-idling-resource:2.1
  12. +--- org.hamcrest:hamcrest-library:1.1
  13. +--- javax.inject:javax.inject:1
  14. +--- com.google.code.findbugs:jsr305:2.0.1
  15. +--- com.android.support.test:runner:0.2 ()
  16. --- javax.annotation:javax.annotation-api:1.2

单独使用group或module参数

exclude后的参数有group和module,可以分别单独使用,会排除所有匹配项。例如下面的脚本匹配了所有的group为’com.android.support.test’的模块。

  1. configurations {
  2. all.exclude group: 'com.android.support.test'
  3. }
  4. dependencies {
  5. androidTestCompile('com.android.support.test:runner:0.2')
  6. androidTestCompile('com.android.support.test:rules:0.2')
  7. androidTestCompile('com.android.support.test.espresso:espresso-core:2.1')
  8. }
  1. --- com.android.support.test.espresso:espresso-core:2.1
  2. +--- com.squareup:javawriter:2.1.1
  3. +--- org.hamcrest:hamcrest-integration:1.1
  4. | --- org.hamcrest:hamcrest-core:1.1
  5. +--- com.android.support.test.espresso:espresso-idling-resource:2.1
  6. +--- org.hamcrest:hamcrest-library:1.1
  7. | --- org.hamcrest:hamcrest-core:1.1
  8. +--- javax.inject:javax.inject:1
  9. +--- com.google.code.findbugs:jsr305:2.0.1
  10. +--- javax.annotation:javax.annotation-api:1.2
  11. --- org.hamcrest:hamcrest-core:1.1

单独给某个模块指定exclude

  1. dependencies {
  2. androidTestCompile('com.android.support.test:runner:0.2')
  3. androidTestCompile('com.android.support.test:rules:0.2')
  4. androidTestCompile('com.android.support.test.espresso:espresso-core:2.1') {
  5. exclude group: 'org.hamcrest'
  6. }
  7. }
  1. +--- com.android.support.test:runner:0.2
  2. | +--- junit:junit-dep:4.10
  3. | | --- org.hamcrest:hamcrest-core:1.1
  4. | +--- com.android.support.test:exposed-instrumentation-api-publish:0.2
  5. | --- com.android.support:support-annotations:22.0.0
  6. +--- com.android.support.test:rules:0.2
  7. | --- com.android.support.test:runner:0.2 ()
  8. --- com.android.support.test.espresso:espresso-core:2.1
  9. +--- com.android.support.test:rules:0.2 ()
  10. +--- com.squareup:javawriter:2.1.1
  11. +--- com.android.support.test.espresso:espresso-idling-resource:2.1
  12. +--- javax.inject:javax.inject:1
  13. +--- com.google.code.findbugs:jsr305:2.0.1
  14. +--- com.android.support.test:runner:0.2 ()
  15. --- javax.annotation:javax.annotation-api:1.2

不同配置下的版本冲突

同样的配置下的版本冲突,会自动使用最新版;而不同配置下的版本冲突,gradle同步时会直接报错。可使用exclude、force解决冲突。

例如compile 'com.android.support:appcompat-v7:23.1.1',和androidTestCompile 'com.android.support.test.espresso:espresso-core:2.1',所依赖的com.android.support:support-annotations版本不同,就会导致冲突。

  1. dependencies {
  2. compile 'com.android.support:appcompat-v7:23.1.1'
  3. androidTestCompile('com.android.support.test:runner:0.2')
  4. androidTestCompile('com.android.support.test:rules:0.2')
  5. androidTestCompile('com.android.support.test.espresso:espresso-core:2.1')
  6. }

gradle同步时会提示

  1. Warning:Conflict with dependency 'com.android.support:support-annotations'. Resolved versions for app and test app differ.

执行dependencies会提示

  1. FAILURE: Build failed with an exception.
  2. What went wrong:
  3. A problem occurred configuring project ':app'.
  4. > Conflict with dependency 'com.android.support:support-annotations'. Resolved versions for app and test app differ.
  5. * Try:
  6. Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.
  7. BUILD FAILED

不兼容

虽然可以通过force、exclude等方式避免依赖项版本冲突,使得grade同步成功,但是并不能代表编译时没有问题。由于不同版本可能不完全兼容,于是会出现各种奇怪的报错。已知的解决思路是更改包的版本、尝试强制使用不同版本的依赖项,找到可兼容的依赖组合。

报错例如:

  1. com.android.dex.DexException: Multiple dex files define Lorg/hamcrest/MatcherAssert;
  2. at com.android.dx.merge.DexMerger.readSortableTypes(DexMerger.java:596)
  3. at com.android.dx.merge.DexMerger.getSortedTypes(DexMerger.java:554)
  4. at com.android.dx.merge.DexMerger.mergeClassDefs(DexMerger.java:535)
  5. at com.android.dx.merge.DexMerger.mergeDexes(DexMerger.java:171)
  6. at com.android.dx.merge.DexMerger.merge(DexMerger.java:189)
  7. at com.android.dx.command.dexer.Main.mergeLibraryDexBuffers(Main.java:454)
  8. at com.android.dx.command.dexer.Main.runMonoDex(Main.java:303)
  9. at com.android.dx.command.dexer.Main.run(Main.java:246)
  10. at com.android.dx.command.dexer.Main.main(Main.java:215)
  11. at com.android.dx.command.Main.main(Main.java:106)
  12. Error:Execution failed for task ':app:dexDebugAndroidTest'.
  13. > com.android.ide.common.process.ProcessException: org.gradle.process.internal.ExecException: Process 'command '/Library/Java/JavaVirtualMachines/jdk1.8.0_40.jdk/Contents/Home/bin/java'' finished with non-zero exit value 2
  14. BUILD FAILED

又例如Android执行Espresso单元测试时出现:

  1. Running tests
  2. Test running started
  3. java.lang.NoSuchMethodError: org.hamcrest.core.AnyOf.anyOf
  4. at org.hamcrest.Matchers.anyOf(Matchers.java:87)
  5. at android.support.test.espresso.Espresso.<clinit>(Espresso.java:158)
  6. at com.jzj1993.unittest.test.MainActivityEspressoTest.sayHello(MainActivityEspressoTest.java:28)
  7. at java.lang.reflect.Method.invokeNative(Native Method)
  8. at java.lang.reflect.Method.invoke(Method.java:525)
  9. at org.junit.runners.model.FrameworkMethod$1</span><span class="pun">.</span><span class="pln">runReflectiveCall</span><span class="pun">(</span><span class="typ">FrameworkMethod</span><span class="pun">.</span><span class="pln">java</span><span class="pun">:</span><span class="lit">45</span><span class="pun">)</span></code></li><li style="list-style-type: none; padding-left: 0px;" class="L9"><code><span class="pln">at org</span><span class="pun">.</span><span class="pln">junit</span><span class="pun">.</span><span class="kwd">internal</span><span class="pun">.</span><span class="pln">runners</span><span class="pun">.</span><span class="pln">model</span><span class="pun">.</span><span class="typ">ReflectiveCallable</span><span class="pun">.</span><span class="pln">run</span><span class="pun">(</span><span class="typ">ReflectiveCallable</span><span class="pun">.</span><span class="pln">java</span><span class="pun">:</span><span class="lit">15</span><span class="pun">)</span></code></li><li style="list-style-type: none; padding-left: 0px;" class="L0"><code><span class="pln">at org</span><span class="pun">.</span><span class="pln">junit</span><span class="pun">.</span><span class="pln">runners</span><span class="pun">.</span><span class="pln">model</span><span class="pun">.</span><span class="typ">FrameworkMethod</span><span class="pun">.</span><span class="pln">invokeExplosively</span><span class="pun">(</span><span class="typ">FrameworkMethod</span><span class="pun">.</span><span class="pln">java</span><span class="pun">:</span><span class="lit">42</span><span class="pun">)</span></code></li><li style="list-style-type: none; padding-left: 0px;" class="L1"><code><span class="pln">at org</span><span class="pun">.</span><span class="pln">junit</span><span class="pun">.</span><span class="kwd">internal</span><span class="pun">.</span><span class="pln">runners</span><span class="pun">.</span><span class="pln">statements</span><span class="pun">.</span><span class="typ">InvokeMethod</span><span class="pun">.</span><span class="pln">evaluate</span><span class="pun">(</span><span class="typ">InvokeMethod</span><span class="pun">.</span><span class="pln">java</span><span class="pun">:</span><span class="lit">20</span><span class="pun">)</span></code></li><li style="list-style-type: none; padding-left: 0px;" class="L2"><code><span class="pln">at android</span><span class="pun">.</span><span class="pln">support</span><span class="pun">.</span><span class="pln">test</span><span class="pun">.</span><span class="kwd">internal</span><span class="pun">.</span><span class="pln">statement</span><span class="pun">.</span><span class="typ">UiThreadStatement</span><span class="pun">.</span><span class="pln">evaluate</span><span class="pun">(</span><span class="typ">UiThreadStatement</span><span class="pun">.</span><span class="pln">java</span><span class="pun">:</span><span class="lit">55</span><span class="pun">)</span></code></li><li style="list-style-type: none; padding-left: 0px;" class="L3"><code><span class="pln">at android</span><span class="pun">.</span><span class="pln">support</span><span class="pun">.</span><span class="pln">test</span><span class="pun">.</span><span class="pln">rule</span><span class="pun">.</span><span class="typ">ActivityTestRule$ActivityStatement.evaluate(ActivityTestRule.java:257)
  10. at org.junit.rules.RunRules.evaluate(RunRules.java:18)
  11. at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:263)
  12. at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:68)
  13. at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:47)
  14. at org.junit.runners.ParentRunner$3</span><span class="pun">.</span><span class="pln">run</span><span class="pun">(</span><span class="typ">ParentRunner</span><span class="pun">.</span><span class="pln">java</span><span class="pun">:</span><span class="lit">231</span><span class="pun">)</span></code></li><li style="list-style-type: none; padding-left: 0px;" class="L9"><code><span class="pln">at org</span><span class="pun">.</span><span class="pln">junit</span><span class="pun">.</span><span class="pln">runners</span><span class="pun">.</span><span class="typ">ParentRunner$1.schedule(ParentRunner.java:60)
  15. at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:229)
  16. at org.junit.runners.ParentRunner.access$000</span><span class="pun">(</span><span class="typ">ParentRunner</span><span class="pun">.</span><span class="pln">java</span><span class="pun">:</span><span class="lit">50</span><span class="pun">)</span></code></li><li style="list-style-type: none; padding-left: 0px;" class="L2"><code><span class="pln">at org</span><span class="pun">.</span><span class="pln">junit</span><span class="pun">.</span><span class="pln">runners</span><span class="pun">.</span><span class="typ">ParentRunner$2.evaluate(ParentRunner.java:222)
  17. at org.junit.runners.ParentRunner.run(ParentRunner.java:300)
  18. at org.junit.runners.Suite.runChild(Suite.java:128)
  19. at org.junit.runners.Suite.runChild(Suite.java:24)
  20. at org.junit.runners.ParentRunner$3</span><span class="pun">.</span><span class="pln">run</span><span class="pun">(</span><span class="typ">ParentRunner</span><span class="pun">.</span><span class="pln">java</span><span class="pun">:</span><span class="lit">231</span><span class="pun">)</span></code></li><li style="list-style-type: none; padding-left: 0px;" class="L7"><code><span class="pln">at org</span><span class="pun">.</span><span class="pln">junit</span><span class="pun">.</span><span class="pln">runners</span><span class="pun">.</span><span class="typ">ParentRunner$1.schedule(ParentRunner.java:60)
  21. at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:229)
  22. at org.junit.runners.ParentRunner.access$000</span><span class="pun">(</span><span class="typ">ParentRunner</span><span class="pun">.</span><span class="pln">java</span><span class="pun">:</span><span class="lit">50</span><span class="pun">)</span></code></li><li style="list-style-type: none; padding-left: 0px;" class="L0"><code><span class="pln">at org</span><span class="pun">.</span><span class="pln">junit</span><span class="pun">.</span><span class="pln">runners</span><span class="pun">.</span><span class="typ">ParentRunner$2.evaluate(ParentRunner.java:222)
  23. at org.junit.runners.ParentRunner.run(ParentRunner.java:300)
  24. at org.junit.runner.JUnitCore.run(JUnitCore.java:157)
  25. at org.junit.runner.JUnitCore.run(JUnitCore.java:136)
  26. at android.support.test.internal.runner.TestExecutor.execute(TestExecutor.java:54)
  27. at android.support.test.runner.AndroidJUnitRunner.onStart(AndroidJUnitRunner.java:228)
  28. at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1701)
  29. Finish

参考资料与扩展阅读

https://docs.gradle.org/current/userguide/dependency_management.html
http://www.concretepage.com/build-tools/gradle/gradle-exclude-transitive-dependency-example

展开阅读全文
©️2020 CSDN 皮肤主题: 大白 设计师: CSDN官方博客 返回首页
实付0元
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值