解决 Gradle 依赖冲突

版本冲突

当依赖两个库时,这两个库(A,B)中又同时依赖于另一个库(C),如果这个时候 A,B 中依赖的 C 库版本不同,Gradle 会自动使用最新的版本,比如

1
2
3
4
5
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.2,这时 Gradle 会默认使用最新的版本

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
+--- 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

resolutionStrategy

在版本冲突时,Gradle 会默认帮我们处理,假如我们想自己处理版本冲突,则需要自定义规则。
应该在 configurations 中进行配置,如下所示强制使用 1.3.9 版本

1
2
3
4
5
configurations.all {
resolutionStrategy {
force 'com.google.code.findbugs:jsr305:1.3.9'
}
}
作者

PPTing

发布于

2023-02-01

更新于

2024-05-24

许可协议

评论