是的。如果您在项目 A 和 B 中特别要求同一库的不同版本,您最终可能会得到相同直接依赖项的不同版本。
对于临时依赖,默认行为是选择请求依赖的最新版本。请注意最新这个词,而不是要求的最高版本。只要版本向后兼容您的项目实际期望的最低版本,这很好。
幸运的是,gradle 有几个内置的方法来解决依赖冲突。
我在这里写了很多关于这个主题的文章:http://www.devsbedevin.net/android-understanding-gradle-dependencies-and-resolving-conflicts/
TL;DR
您可以选择在冲突中失败:
configurations.all {
resolutionStrategy {
failOnVersionConflict()
}
}
强制一个特定的依赖:
configurations.all {
resolutionStrategy {
force 'asm:asm-all:3.3.1', 'commons-io:commons-io:1.4', 'com.parse.bolts:bolts-android:1.+'
}
}
更喜欢你自己的模块:
configurations.all {
resolutionStrategy {
preferProjectModules()
}
}
将库 X 的所有实例替换为 Y(库、模块或项目):
configurations.all {
resolutionStrategy {
dependencySubstitution {
substitute module('commons-io:commons-io:2.4') with project(':my-commons-io')
}
}
}
排除特定库的所有临时依赖项并手动添加必要的库:
dependencies {
compile('com.android.support:appcompat-v7:23.1.0') {
transitive = false
}
}
排除特定的传递依赖:
dependencies {
compile('com.android.support:appcompat-v7:23.1.0') {
exclude group: 'com.parse.bolts'
}
}
无论实际的依赖要求如何,强制您的项目使用特定版本:
dependencies {
compile('com.parse.bolts:bolts-android:1.+') {
force = true
}
}