(轉)Gradle依賴項學習總結,dependencies、transitive、force、exclude的使用與依賴沖突解決

轉載自 http://www.paincker.com/gradle-dependencies

之前仔細研究過gradle,看的也挺全面,但是沒有做過任何記錄,現(xiàn)在發(fā)現(xiàn)基本上全都忘光了,所以今天看到這篇文章,趕緊記錄一下。

Gradle是一個非常好用的編譯工具,特別是繼承了maven的依賴項管理功能,需要的Library不需要像傳統(tǒng)IDE一樣手動下載復制到項目中,只需要簡單的寫一行gradle腳本,就能自動下載下來并編譯。

但是有時候會出現(xiàn)各種不明情況的報錯,最常見的一種原因就是依賴項版本沖突。

每個模塊都可能依賴其他模塊,這些模塊又會依賴別的模塊。而一個項目中的多個模塊,對同一個模塊的不同版本有依賴,就可能產生沖突。

通過gradle命令查看依賴樹,可以比較直觀的看到沖突。具體方法是在模塊所在的目錄,也即build.gradle所在目錄下執(zhí)行gradle dependencies(需要將gradle加入PATH環(huán)境變量),執(zhí)行結果如圖。


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

統(tǒng)一指定transitive

可以給dependencies統(tǒng)一指定transitive為false,再次執(zhí)行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*.excludegroup:'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參數(shù)

exclude后的參數(shù)有group和module,可以分別單獨使用,會排除所有匹配項。例如下面的腳本匹配了所有的group為’com.android.support.test’的模塊。

configurations{

all*.excludegroup:'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'){

excludegroup:'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:Conflictwithdependency'com.android.support:support-annotations'.Resolvedversionsforappandtest app differ.

執(zhí)行dependencies會提示

FAILURE:Buildfailedwithan exception.

*Whatwent wrong:

A problem occurred configuring project':app'.

>Conflictwithdependency'com.android.support:support-annotations'.Resolvedversionsforappandtest app differ.

*Try:

Runwith--stacktrace option togetthe stack trace.Runwith--infoor--debug option togetmore log output.

BUILD FAILED

不兼容


雖然可以通過force、exclude等方式避免依賴項版本沖突,使得grade同步成功,但是并不能代表編譯時沒有問題。由于不同版本可能不完全兼容,于是會出現(xiàn)各種奇怪的報錯。已知的解決思路是更改包的版本、嘗試強制使用不同版本的依賴項,找到可兼容的依賴組合。

報錯例如:

com.android.dex.DexException:Multipledex files defineLorg/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:Executionfailedfortask':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''finishedwithnon-zeroexitvalue2

BUILD FAILED

又例如Android執(zhí)行Espresso單元測試時出現(xiàn):

Runningtests

Testrunning started

java.lang.NoSuchMethodError:org.hamcrest.core.AnyOf.anyOf

at org.hamcrest.Matchers.anyOf(Matchers.java:87)

at android.support.test.espresso.Espresso.(Espresso.java:158)

at com.jzj1993.unittest.test.MainActivityEspressoTest.sayHello(MainActivityEspressoTest.java:28)

at java.lang.reflect.Method.invokeNative(NativeMethod)

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

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
【社區(qū)內容提示】社區(qū)部分內容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發(fā)布,文章內容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內容

  • Spring Cloud為開發(fā)人員提供了快速構建分布式系統(tǒng)中一些常見模式的工具(例如配置管理,服務發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 136,715評論 19 139
  • afinalAfinal是一個android的ioc,orm框架 https://github.com/yangf...
    passiontim閱讀 15,900評論 2 45
  • 下午開班會老師說 生前何必長睡 死后必定長眠 “生時何必多睡,死后必定長眠”大概是雞湯邪教里最讓我生氣的一句話了。...
    月半月半_閱讀 603評論 0 0
  • 文/陌上陌路人 露重風沉籬院淺 ,鳴蟬訴影半妝金。 經年草色枯殘月 ,累月花香染素衾。 細辯青山來處唱, 輕聆綠水...
    陌諾流年閱讀 1,332評論 58 47
  • 小時候我和姐姐看《還珠格格》,姐姐說她討厭紫薇,因為她臉皮太厚。那時候我還不知道臉皮厚是什么意思,于是姐姐跟我解釋...
    朝歌閱讀 4,302評論 10 23

友情鏈接更多精彩內容