(转)Gradle依赖项学习总结,dependencies、transitive、force、exclude的使用与依赖冲突解决

转载自 http://www.paincker.com/gradle-dependencies

之前仔细研究过gradle,看的也挺全面,但是没有做过任何记录,现在发现基本上全都忘光了,所以今天看到这篇文章,赶紧记录一下。

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*.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参数

exclude后的参数有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.

执行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同步成功,但是并不能代表编译时没有问题。由于不同版本可能不完全兼容,于是会出现各种奇怪的报错。已知的解决思路是更改包的版本、尝试强制使用不同版本的依赖项,找到可兼容的依赖组合。

报错例如:

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执行Espresso单元测试时出现:

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

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 158,847评论 4 362
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,208评论 1 292
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 108,587评论 0 243
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 43,942评论 0 205
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,332评论 3 287
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,587评论 1 218
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,853评论 2 312
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,568评论 0 198
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,273评论 1 242
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,542评论 2 246
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,033评论 1 260
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,373评论 2 253
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,031评论 3 236
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,073评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,830评论 0 195
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,628评论 2 274
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,537评论 2 269

推荐阅读更多精彩内容