1.出現(xiàn)的沖突
2.解決方法(解決方法都跟第三部分依賴樹有很大關(guān)系茫蛹,建議結(jié)合起來看啦)
方法①
configurations.all {
//強(qiáng)制使用某個(gè)版本的依賴,若需強(qiáng)制多個(gè)依賴,可以逗號(hào)分割,
resolutionStrategy.force 'com.android.support:support-annotations:26.1.0'
}
方法②
configurations.all {
resolutionStrategy.eachDependency { DependencyResolveDetails details ->
def requested = details.requested
if (requested.group == 'com.android.support') {
if (!requested.name.startsWith("multidex")) {
details.useVersion '26.1.0'
}
}
}
}
方法③
androidTestImplementation ('com.android.support.test:runner:1.0.2'){
exclude group:'com.android.support',module: 'support-annotations'
}
androidTestImplementation ('com.android.support.test.espresso:espresso-core:3.0.2'){
exclude group:'com.android.support',module: 'support-annotations'
}
這里有個(gè)重要的知識(shí)點(diǎn)
3.查看依賴的依賴列表(依賴樹)
在android studio的Terminal窗口輸入命令
gradlew -q app:dependencies
可以看到appcompat-v7和runner中都有依賴support-annotations,可以看到?jīng)_突存在的位置是26.1.0-->27.1.1
還有一種更簡單的方法可以看到?jīng)_突所在茄蚯,在module下的gradle.buildd下添加如下代碼拓萌,但缺點(diǎn)就是不知道具體它是哪個(gè)上層依賴引起的碍沐,可以結(jié)合起來在 Terminal 打印的日志中直接搜索,可快速定位到?jīng)_突所在激涤,畢竟依賴樹打印出來是一大串的
configurations.all {
resolutionStrategy {
failOnVersionConflict()
}
}
4.什么情況下會(huì)產(chǎn)生依賴沖突拟糕?
如果通過相同的方式引入不同版本的依賴庫,默認(rèn)會(huì)選擇最新版本,不同的方式引入則會(huì)產(chǎn)生依賴沖突送滞。引入的方式有:通過jar/aar侠草、maven(不同的compile也算不同方式,如cmpile和androidTestCompile兩者引入的方式不同)
具體可看:android studio 關(guān)于gradle依賴管理的一些知識(shí)
了解Android支持庫
各種導(dǎo)入方式