1. 程式人生 > >【安卓】android第三方庫導致support版本衝突解決方案

【安卓】android第三方庫導致support版本衝突解決方案

問題

升級compileSdk版本到26,同時修改了support包的版本,報錯

all com.android.support libraries must use the exact same version specification(mixing versions can lead to runtime crashes)

也就是說有引入的第三方庫和目前編譯版本有衝突。

解決

一般這種問題解決方案是,在指定的有衝突的庫的依賴處,新增 exclude group: 'com.android.support',可以將衝突庫不包含在編譯,如

compile('xx.xxx.xxxxx:xxxxx:1.5.5'
) { exclude group: 'com.android.support' }

但是問題是我不知道哪個第三方庫衝突,不可能一個個檢查吧?

這時候只需要在gradle檔案中新增如下程式碼,讓所有的第三方包強制使用指定版本的support包:

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' } } } }

以及在自己寫第三方庫給別人用的時候,對於support包的依賴方式改成provided(或者compileOnly,gradle3.0),這樣不會把support
打包,方便使用的人。

關於gradle3.0更多

gradle升級到3.0後,依賴的方式變得更多了,最顯著的變化就是,之前一直用的compile

可以替換為implementation, 如

implementation 'xx.xxx.xxxxx:xxxxx::1.5.5'

implementation是指引入依賴,這個第三方包引入的東西,你在專案裡無法使用,有點介面的味道,遮蔽內部實現。可以加快gradle編譯的速度。

同樣的對於 debugcompile releasecompile 都有debug implementation release implementation 與之對應。