1 查依赖
Gradle 默认开启了 依赖传递 意思就是 项目依赖了A,A又依赖了B和C,这时候,我们只需要写一行代码:implementation A
就行了,由传递依赖导致的冲突,默认是以最高版本的依赖为准,要想查看整个项目的依赖传递关系,使用命令:
./gradlew app:dependencies --configuration releaseRuntimeClasspath
app
是具体的module
。releaseRuntimeClasspath
是具体的variants
类型。
结果:
符号的含义:
x.x.x (*)
该依赖已经有了,将不再重复依赖。x.x.x -> x.x.x
该依赖的版本被箭头所指的版本代替。x.x.x -> x.x.x(*)
该依赖的版本被箭头所指的版本代替,并且该依赖已经有了,不再重复依赖。
也可以在Android studio
左侧的Project
栏的External Libraries
中查看,可以看到 由于引入了和当前编译版本号不同的支持库所产生的问题。
2 解决方式
2.1 排除所有:
// 在build.gradle 中添加下面节点
configurations{
all*.exclude module: "support-fragment"
}
2.2 排除指定:
A:
implementation ('com.github.bumptech.glide:glide:4.7.1'){
exclude module:"support-fragment"
}
B:
configurations { //编译期排除commons模块 compile.exclude module: ‘commons‘ //在整个构建过程中排除pkaq.tiger:share all*.exclude group: ‘pkaq.tiger‘, module: ‘share‘ } dependencies { compile("pkaq.tiger:web:1.0") { exclude module: ‘share‘ } }
2.3 Force 强制指定
A:全局配置强制使用某个版本的依赖来解决依赖冲突中出现的依赖
configurations.all {
resolutionStrategy {
force 'com.android.support:support-fragment:26.1.0'
}
}
B:指定force = true
属性可以冲突时优先使用该版本进行解决
compile(‘org.hibernate:hibernate:3.1‘) { force = true }
3 依赖传递特性
A 全局性的关闭依赖传递特性
configurations.all { transitive = false }
B : 局部
传递依赖特性可以轻松地通过transitive参数进行开启或关闭,上面的示例中如果要忽略spring-web
的传递性依赖可以采用指定 transitive = false
的方式来关闭依赖传递特性,也可以采用添加@jar
的方式忽略该依赖的所有传递性依赖。
compile("org.springframework:spring-web:4.3.4.RELEASE") { transitive = false } compile `org.springframework:spring-web:4.3.4.RELEASE@jar`
4 使用动态版本
+
表示始终采用该依赖最新的1.x
版本的最新依赖。
compile ‘org.springframework:spring-web:+‘
=====================================================
一个综合示例
compile(‘org.hibernate:hibernate:3.1‘) { // 冲突时优先使用该版本 force = true // 依据构建名称排除 exclude module: ‘cglib‘ // 依据组织名称排除 exclude group: ‘org.jmock‘ // 依据组织名称+构件名称排除 exclude group: ‘org.unwanted‘, module: ‘iAmBuggy‘ // 为本依赖关闭依赖传递特性 transitive = false }
记录自:
https://segmentfault.com/a/1190000015805844
http://www.mamicode.com/info-detail-2423841.html