http://www.paincker.com/gradle-dependencies
https://docs.gradle.org/current/userguide/dependency_management.html
http://www.concretepage.com/build-tools/gradle/gradle-exclude-transitive-dependency-example
Gradle是一個非常好用的編譯工具,特別是繼承了maven的依賴項管理功能,需要的Library不需要像傳統IDE一樣手動下載復制到項目中,只需要簡單的寫一行gradle腳本,就能自動下載下來並編譯。
但是有時候會出現各種不明情況的報錯,最常見的一種原因就是依賴項版本沖突。
每個模塊都可能依賴其他模塊,這些模塊又會依賴別的模塊。而一個項目中的多個模塊,對同一個模塊的不同版本有依賴,就可能產生沖突。
通過gradle命令查看依賴樹,可以比較直觀的看到沖突。具體方法是在模塊所在的目錄,也即build.gradle所在目錄下執行gradle dependencies(需要將gradle加入PATH環境變量),執行結果如圖。

Transitive
Transitive用於自動處理子依賴項。默認為true,gradle自動添加子依賴項,形成一個多層樹形結構;設置為false,則需要手動添加每個依賴項。
案例
以安卓單元測試espresso的配置為例,gradle依賴如下:
dependencies {androidTestCompile('com.android.support.test:runner:0.2')androidTestCompile('com.android.support.test:rules:0.2')androidTestCompile('com.android.support.test.espresso:espresso-core:2.1')}
運行gradle dependencies的結果如下。可以看到每個包的依賴項都被遞歸分析並添加進來。
+--- com.android.support.test:runner:0.2| +--- junit:junit-dep:4.10| | \--- org.hamcrest:hamcrest-core:1.1| +--- com.android.support.test:exposed-instrumentation-api-publish:0.2| \--- com.android.support:support-annotations:22.0.0+--- com.android.support.test:rules:0.2| \--- com.android.support.test:runner:0.2 (*)\--- com.android.support.test.espresso:espresso-core:2.1+--- com.android.support.test:rules:0.2 (*)+--- com.squareup:javawriter:2.1.1+--- org.hamcrest:hamcrest-integration:1.1| \--- org.hamcrest:hamcrest-core:1.1+--- com.android.support.test.espresso:espresso-idling-resource:2.1+--- org.hamcrest:hamcrest-library:1.1| \--- org.hamcrest:hamcrest-core:1.1+--- javax.inject:javax.inject:1+--- com.google.code.findbugs:jsr305:2.0.1+--- com.android.support.test:runner:0.2 (*)+--- javax.annotation:javax.annotation-api:1.2\--- org.hamcrest:hamcrest-core:1.1
統一指定transitive
可以給dependencies統一指定transitive為false,再次執行dependencies可以看到如下結果。
configurations.all {transitive = false}dependencies {androidTestCompile('com.android.support.test:runner:0.2')androidTestCompile('com.android.support.test:rules:0.2')androidTestCompile('com.android.support.test.espresso:espresso-core:2.1')}
+--- com.android.support.test:runner:0.2+--- com.android.support.test:rules:0.2\--- com.android.support.test.espresso:espresso-core:2.1
單獨指定依賴項的transitive
dependencies {androidTestCompile('com.android.support.test:runner:0.2')androidTestCompile('com.android.support.test:rules:0.2')androidTestCompile('com.android.support.test.espresso:espresso-core:2.1') {transitive = false}}
版本沖突
在同一個配置下(例如androidTestCompile),某個模塊的不同版本同時被依賴時,默認使用最新版,gradle同步時不會報錯。例如下面的hamcrest-core和runner。
dependencies {androidTestCompile('com.android.support.test:runner:0.4')androidTestCompile('com.android.support.test:rules:0.2')androidTestCompile('com.android.support.test.espresso:espresso-core:2.1')}
+--- com.android.support.test:runner:0.4| +--- com.android.support:support-annotations:23.0.1| +--- junit:junit:4.12| | \--- org.hamcrest:hamcrest-core:1.3| \--- com.android.support.test:exposed-instrumentation-api-publish:0.4+--- com.android.support.test:rules:0.2| \--- com.android.support.test:runner:0.2 -> 0.4 (*)\--- com.android.support.test.espresso:espresso-core:2.1+--- com.android.support.test:rules:0.2 (*)+--- com.squareup:javawriter:2.1.1+--- org.hamcrest:hamcrest-integration:1.1| \--- org.hamcrest:hamcrest-core:1.1 -> 1.3+--- com.android.support.test.espresso:espresso-idling-resource:2.1+--- org.hamcrest:hamcrest-library:1.1| \--- org.hamcrest:hamcrest-core:1.1 -> 1.3+--- javax.inject:javax.inject:1+--- com.google.code.findbugs:jsr305:2.0.1+--- com.android.support.test:runner:0.2 -> 0.4 (*)+--- javax.annotation:javax.annotation-api:1.2\--- org.hamcrest:hamcrest-core:1.1 -> 1.3
Force
force強制設置某個模塊的版本。
configurations.all {resolutionStrategy {force 'org.hamcrest:hamcrest-core:1.3'}}dependencies {androidTestCompile('com.android.support.test:runner:0.2')androidTestCompile('com.android.support.test:rules:0.2')androidTestCompile('com.android.support.test.espresso:espresso-core:2.1')}
可以看到,原本對hamcrest-core 1.1的依賴,全部變成了1.3。
+--- com.android.support.test:runner:0.2| +--- junit:junit-dep:4.10| | \--- org.hamcrest:hamcrest-core:1.1 -> 1.3| +--- com.android.support.test:exposed-instrumentation-api-publish:0.2| \--- com.android.support:support-annotations:22.0.0+--- com.android.support.test:rules:0.2| \--- com.android.support.test:runner:0.2 (*)\--- com.android.support.test.espresso:espresso-core:2.1+--- com.android.support.test:rules:0.2 (*)+--- com.squareup:javawriter:2.1.1+--- org.hamcrest:hamcrest-integration:1.1| \--- org.hamcrest:hamcrest-core:1.1 -> 1.3+--- com.android.support.test.espresso:espresso-idling-resource:2.1+--- org.hamcrest:hamcrest-library:1.1| \--- org.hamcrest:hamcrest-core:1.1 -> 1.3+--- javax.inject:javax.inject:1+--- com.google.code.findbugs:jsr305:2.0.1+--- com.android.support.test:runner:0.2 (*)+--- javax.annotation:javax.annotation-api:1.2\--- org.hamcrest:hamcrest-core:1.1 -> 1.3
Exclude
Exclude可以設置不編譯指定的模塊
configurations {all*.exclude group: 'org.hamcrest', module: 'hamcrest-core'}dependencies {androidTestCompile('com.android.support.test:runner:0.2')androidTestCompile('com.android.support.test:rules:0.2')androidTestCompile('com.android.support.test.espresso:espresso-core:2.1')}
+--- com.android.support.test:runner:0.2| +--- junit:junit-dep:4.10| +--- com.android.support.test:exposed-instrumentation-api-publish:0.2| \--- com.android.support:support-annotations:22.0.0+--- com.android.support.test:rules:0.2| \--- com.android.support.test:runner:0.2 (*)\--- com.android.support.test.espresso:espresso-core:2.1+--- com.android.support.test:rules:0.2 (*)+--- com.squareup:javawriter:2.1.1+--- org.hamcrest:hamcrest-integration:1.1+--- com.android.support.test.espresso:espresso-idling-resource:2.1+--- org.hamcrest:hamcrest-library:1.1+--- javax.inject:javax.inject:1+--- com.google.code.findbugs:jsr305:2.0.1+--- com.android.support.test:runner:0.2 (*)\--- javax.annotation:javax.annotation-api:1.2
單獨使用group或module參數
exclude后的參數有group和module,可以分別單獨使用,會排除所有匹配項。例如下面的腳本匹配了所有的group為’com.android.support.test’的模塊。
configurations {all*.exclude group: 'com.android.support.test'}dependencies {androidTestCompile('com.android.support.test:runner:0.2')androidTestCompile('com.android.support.test:rules:0.2')androidTestCompile('com.android.support.test.espresso:espresso-core:2.1')}
\--- com.android.support.test.espresso:espresso-core:2.1+--- com.squareup:javawriter:2.1.1+--- org.hamcrest:hamcrest-integration:1.1| \--- org.hamcrest:hamcrest-core:1.1+--- com.android.support.test.espresso:espresso-idling-resource:2.1+--- org.hamcrest:hamcrest-library:1.1| \--- org.hamcrest:hamcrest-core:1.1+--- javax.inject:javax.inject:1+--- com.google.code.findbugs:jsr305:2.0.1+--- javax.annotation:javax.annotation-api:1.2\--- org.hamcrest:hamcrest-core:1.1
單獨給某個模塊指定exclude
dependencies {androidTestCompile('com.android.support.test:runner:0.2')androidTestCompile('com.android.support.test:rules:0.2')androidTestCompile('com.android.support.test.espresso:espresso-core:2.1') {exclude group: 'org.hamcrest'}}
+--- com.android.support.test:runner:0.2| +--- junit:junit-dep:4.10| | \--- org.hamcrest:hamcrest-core:1.1| +--- com.android.support.test:exposed-instrumentation-api-publish:0.2| \--- com.android.support:support-annotations:22.0.0+--- com.android.support.test:rules:0.2| \--- com.android.support.test:runner:0.2 (*)\--- com.android.support.test.espresso:espresso-core:2.1+--- com.android.support.test:rules:0.2 (*)+--- com.squareup:javawriter:2.1.1+--- com.android.support.test.espresso:espresso-idling-resource:2.1+--- javax.inject:javax.inject:1+--- com.google.code.findbugs:jsr305:2.0.1+--- com.android.support.test:runner:0.2 (*)\--- 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版本不同,就會導致沖突。
dependencies {compile 'com.android.support:appcompat-v7:23.1.1'androidTestCompile('com.android.support.test:runner:0.2')androidTestCompile('com.android.support.test:rules:0.2')androidTestCompile('com.android.support.test.espresso:espresso-core:2.1')}
gradle同步時會提示
Warning:Conflict with dependency 'com.android.support:support-annotations'. Resolved versions for app and test app differ.
執行dependencies會提示
FAILURE: Build failed with an exception.* What went wrong:A problem occurred configuring project ':app'.> Conflict with dependency 'com.android.support:support-annotations'. Resolved versions for app and test app differ.* Try:Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.BUILD FAILED
不兼容
雖然可以通過force、exclude等方式避免依賴項版本沖突,使得grade同步成功,但是並不能代表編譯時沒有問題。由於不同版本可能不完全兼容,於是會出現各種奇怪的報錯。已知的解決思路是更改包的版本、嘗試強制使用不同版本的依賴項,找到可兼容的依賴組合。
報錯例如:
com.android.dex.DexException: Multiple dex files define Lorg/hamcrest/MatcherAssert;at com.android.dx.merge.DexMerger.readSortableTypes(DexMerger.java:596)at com.android.dx.merge.DexMerger.getSortedTypes(DexMerger.java:554)at com.android.dx.merge.DexMerger.mergeClassDefs(DexMerger.java:535)at com.android.dx.merge.DexMerger.mergeDexes(DexMerger.java:171)at com.android.dx.merge.DexMerger.merge(DexMerger.java:189)at com.android.dx.command.dexer.Main.mergeLibraryDexBuffers(Main.java:454)at com.android.dx.command.dexer.Main.runMonoDex(Main.java:303)at com.android.dx.command.dexer.Main.run(Main.java:246)at com.android.dx.command.dexer.Main.main(Main.java:215)at com.android.dx.command.Main.main(Main.java:106)Error:Execution failed for task ':app:dexDebugAndroidTest'.> 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 2BUILD FAILED
又例如Android執行Espresso單元測試時出現:
Running testsTest running startedjava.lang.NoSuchMethodError: org.hamcrest.core.AnyOf.anyOfat org.hamcrest.Matchers.anyOf(Matchers.java:87)at android.support.test.espresso.Espresso.<clinit>(Espresso.java:158)at com.jzj1993.unittest.test.MainActivityEspressoTest.sayHello(MainActivityEspressoTest.java:28)at java.lang.reflect.Method.invokeNative(Native Method)at java.lang.reflect.Method.invoke(Method.java:525)at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:45)at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:42)at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)at android.support.test.internal.statement.UiThreadStatement.evaluate(UiThreadStatement.java:55)at android.support.test.rule.ActivityTestRule$ActivityStatement.evaluate(ActivityTestRule.java:257)at org.junit.rules.RunRules.evaluate(RunRules.java:18)at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:263)at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:68)at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:47)at org.junit.runners.ParentRunner$3.run(ParentRunner.java:231)at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:60)at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:229)at org.junit.runners.ParentRunner.access$000(ParentRunner.java:50)at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:222)at org.junit.runners.ParentRunner.run(ParentRunner.java:300)at org.junit.runners.Suite.runChild(Suite.java:128)at org.junit.runners.Suite.runChild(Suite.java:24)at org.junit.runners.ParentRunner$3.run(ParentRunner.java:231)at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:60)at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:229)at org.junit.runners.ParentRunner.access$000(ParentRunner.java:50)at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:222)at org.junit.runners.ParentRunner.run(ParentRunner.java:300)at org.junit.runner.JUnitCore.run(JUnitCore.java:157)at org.junit.runner.JUnitCore.run(JUnitCore.java:136)at android.support.test.internal.runner.TestExecutor.execute(TestExecutor.java:54)at android.support.test.runner.AndroidJUnitRunner.onStart(AndroidJUnitRunner.java:228)at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1701)Finish
參考資料與擴展閱讀
https://docs.gradle.org/current/userguide/dependency_management.html
http://www.concretepage.com/build-tools/gradle/gradle-exclude-transitive-dependency-example
