但是有時(shí)候會(huì)出現(xiàn)各種不明情況的報(bào)錯(cuò),最常見的一種原因就是依賴項(xiàng)版本沖突。
每個(gè)模塊都可能依賴其他模塊,這些模塊又會(huì)依賴別的模塊。而一個(gè)項(xiàng)目中的多個(gè)模塊,對(duì)同一個(gè)模塊的不同版本有依賴,就可能產(chǎn)生沖突。
通過gradle命令查看依賴樹,可以比較直觀的看到?jīng)_突。具體方法是在模塊所在的目錄,也即build.gradle所在目錄下執(zhí)行g(shù)radle dependencies(需要將gradle加入PATH環(huán)境變量),執(zhí)行結(jié)果如圖。

Transitive
Transitive用于自動(dòng)處理子依賴項(xiàng)。默認(rèn)為true,gradle自動(dòng)添加子依賴項(xiàng),形成一個(gè)多層樹形結(jié)構(gòu);設(shè)置為false,則需要手動(dòng)添加每個(gè)依賴項(xiàng)。
案例
以安卓單元測(cè)試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')
}
運(yùn)行g(shù)radle dependencies的結(jié)果如下。可以看到每個(gè)包的依賴項(xiàng)都被遞歸分析并添加進(jìn)來。
+--- 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
統(tǒng)一指定transitive
可以給dependencies統(tǒng)一指定transitive為false,再次執(zhí)行dependencies可以看到如下結(jié)果。
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
單獨(dú)指定依賴項(xiàng)的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
? }
}
版本沖突
在同一個(gè)配置下(例如androidTestCompile),某個(gè)模塊的不同版本同時(shí)被依賴時(shí),默認(rèn)使用最新版,gradle同步時(shí)不會(huì)報(bào)錯(cuò)。例如下面的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強(qiáng)制設(shè)置某個(gè)模塊的版本。
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')
}
可以看到,原本對(duì)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可以設(shè)置不編譯指定的模塊
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
單獨(dú)使用group或module參數(shù)
exclude后的參數(shù)有g(shù)roup和module,可以分別單獨(dú)使用,會(huì)排除所有匹配項(xiàng)。例如下面的腳本匹配了所有的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
單獨(dú)給某個(gè)模塊指定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
不同配置下的版本沖突
同樣的配置下的版本沖突,會(huì)自動(dòng)使用最新版;而不同配置下的版本沖突,gradle同步時(shí)會(huì)直接報(bào)錯(cuò)??墒褂胑xclude、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版本不同,就會(huì)導(dǎo)致沖突。
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同步時(shí)會(huì)提示
Warning:Conflict with dependency 'com.android.support:support-annotations'. Resolved versions for app and test app differ.
執(zhí)行dependencies會(huì)提示
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等方式避免依賴項(xiàng)版本沖突,使得grade同步成功,但是并不能代表編譯時(shí)沒有問題。由于不同版本可能不完全兼容,于是會(huì)出現(xiàn)各種奇怪的報(bào)錯(cuò)。已知的解決思路是更改包的版本、嘗試強(qiáng)制使用不同版本的依賴項(xiàng),找到可兼容的依賴組合。
報(bào)錯(cuò)例如:
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 2
BUILD FAILED
又例如Android執(zhí)行Espresso單元測(cè)試時(shí)出現(xiàn):
Running tests
Test running started
java.lang.NoSuchMethodError: org.hamcrest.core.AnyOf.anyOf
at 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
參考資料與擴(kuò)展閱讀
https://docs.gradle.org/current/userguide/dependency_management.html
http://www.concretepage.com/build-tools/gradle/gradle-exclude-transitive-dependency-example
最后,歡迎掃碼關(guān)注微信公眾號(hào)。

本文由jzj1993原創(chuàng),轉(zhuǎn)載請(qǐng)注明來源:http://www.paincker.com/gradle-dependencies