解決 Android Gradle 依賴的各類版本問題

1、問題的產生

1.1 引入的支持庫版本和編譯版本不一致

相信你們在 build.gradle中引入各類依賴的時候,或多或少會見過一些紅線, gradle會提示你,當前的編譯版本和你依賴的這個支持庫的版本號不一樣,應該使用相同的支持庫版本,來比避免編譯不經過問題,相似於這種。

This support library should not use a different version (27) than the compileSdkVersion (26)android

clipboard.png

還有連鎖反應會出現這種問題:全部的Android支持庫 support版本號要一致,也是避免運行時崩潰的問題。

All com.android.support libraries must use the exact same version specification (mixing versions can lead to runtime crashes). Found versions 27.1.0, 26.1.0. Examples include com.android.support:recyclerview-v7:27.1.0 and com.android.support:animated-vector-drawable:26.1.0git

clipboard.png

上面的問題,也能夠在 Android studio左側的 Project欄的 External Libraries中查看,能夠看到 因爲引入了和當前編譯版本號不一樣的支持庫所產生的問題:

clipboard.png

1.2 第三方庫中的子依賴和當前項目的編譯版本不一致。

當引入一個第三方庫,該庫中也依賴了 Android支持庫,且支持庫的版本,和當前版本不一致而致使的版本衝突:

2、如何解決

解決衝突的方式包括:強制指定,排除。

2.1 查看依賴樹

Gradle 默認開啓了 依賴傳遞 意思就是 項目依賴了A,A又依賴了B和C,這時候,咱們只須要寫一行代碼: implementation A就好了,由傳遞依賴致使的衝突,默認是以最高版本的依賴爲準,要想查看整個項目的依賴傳遞關係,使用命令:

./gradlew app:dependencies --configuration releaseRuntimeClasspathgithub

app是具體的module
releaseRuntimeClasspath是具體的variants類型。app

+--- com.android.support.constraint:constraint-layout:1.1.2
|    \--- com.android.support.constraint:constraint-layout-solver:1.1.2
+--- com.android.support:appcompat-v7:26.1.0
|    +--- com.android.support:support-annotations:26.1.0 -> 27.1.1
|    +--- com.android.support:support-v4:26.1.0
|    |    +--- com.android.support:support-compat:26.1.0 -> 27.1.1
|    |    |    +--- com.android.support:support-annotations:27.1.1
|    |    |    \--- android.arch.lifecycle:runtime:1.1.0
|    |    |         +--- android.arch.lifecycle:common:1.1.0
|    |    |         \--- android.arch.core:common:1.1.0
|    |    +--- com.android.support:support-media-compat:26.1.0
|    |    |    +--- com.android.support:support-annotations:26.1.0 -> 27.1.1
|    |    |    \--- com.android.support:support-compat:26.1.0 -> 27.1.1 (*)
|    |    +--- com.android.support:support-core-utils:26.1.0 -> 27.1.1
|    |    |    +--- com.android.support:support-annotations:27.1.1
|    |    |    \--- com.android.support:support-compat:27.1.1 (*)
|    |    +--- com.android.support:support-core-ui:26.1.0 -> 27.1.1
|    |    |    +--- com.android.support:support-annotations:27.1.1
|    |    |    +--- com.android.support:support-compat:27.1.1 (*)
|    |    |    \--- com.android.support:support-core-utils:27.1.1 (*)
|    |    \--- com.android.support:support-fragment:26.1.0 -> 27.1.1
|    |         +--- com.android.support:support-compat:27.1.1 (*)
|    |         +--- com.android.support:support-core-ui:27.1.1 (*)
|    |         +--- com.android.support:support-core-utils:27.1.1 (*)
|    |         +--- com.android.support:support-annotations:27.1.1
|    |         +--- android.arch.lifecycle:livedata-core:1.1.0
|    |         |    +--- android.arch.lifecycle:common:1.1.0
|    |         |    +--- android.arch.core:common:1.1.0
|    |         |    \--- android.arch.core:runtime:1.1.0
|    |         |         \--- android.arch.core:common:1.1.0
|    |         \--- android.arch.lifecycle:viewmodel:1.1.0
|    +--- com.android.support:support-vector-drawable:26.1.0
|    |    +--- com.android.support:support-annotations:26.1.0 -> 27.1.1
|    |    \--- com.android.support:support-compat:26.1.0 -> 27.1.1 (*)
|    \--- com.android.support:animated-vector-drawable:26.1.0
|         +--- com.android.support:support-vector-drawable:26.1.0 (*)
|         \--- com.android.support:support-core-ui:26.1.0 -> 27.1.1 (*)
+--- com.android.support:recyclerview-v7:26.1.0
|    +--- com.android.support:support-annotations:26.1.0 -> 27.1.1
|    +--- com.android.support:support-compat:26.1.0 -> 27.1.1 (*)
|    \--- com.android.support:support-core-ui:26.1.0 -> 27.1.1 (*)
\--- com.github.bumptech.glide:glide:4.7.1
     +--- com.github.bumptech.glide:gifdecoder:4.7.1
     |    \--- com.android.support:support-annotations:27.1.1
     +--- com.github.bumptech.glide:disklrucache:4.7.1
     +--- com.github.bumptech.glide:annotations:4.7.1
     \--- com.android.support:support-fragment:27.1.1 (*)

符號的含義:ide

  • x.x.x (*) 該依賴已經有了,將再也不重複依賴。
  • x.x.x -> x.x.x 該依賴的版本被箭頭所指的版本代替。
  • x.x.x -> x.x.x(*) 該依賴的版本被箭頭所指的版本代替,而且該依賴已經有了,再也不重複依賴。

2.2 Exclude 排除

  • 排除全部:

// 在build.gradle 中添加下面節點
configuration{
    all*.exclude module: "support-fragment"
}

執行結果:(部分)gradle

\--- com.github.bumptech.glide:glide:4.7.1
     +--- com.github.bumptech.glide:gifdecoder:4.7.1
     |    \--- com.android.support:support-annotations:27.1.1
     +--- com.github.bumptech.glide:disklrucache:4.7.1
     \--- com.github.bumptech.glide:annotations:4.7.1

能夠看到相比最開始的執行結果,已經將 glide 的子依賴 com.android.support:support-fragment 排除了。ui

  • 排除指定:

implementation ('com.github.bumptech.glide:glide:4.7.1'){
        exclude module:"support-fragment"
    }

執行結果:(部分)google

+--- com.android.support:appcompat-v7:26.1.0
|    +--- com.android.support:support-annotations:26.1.0 -> 27.1.1
|    +--- com.android.support:support-v4:26.1.0
|    |    +--- com.android.support:support-compat:26.1.0
|    |    |    ...
|    |    +--- com.android.support:support-media-compat:26.1.0
|    |    |    ...
|    |    +--- com.android.support:support-core-utils:26.1.0
|    |    |    ...
|    |    +--- com.android.support:support-core-ui:26.1.0
|    |    |    //不影響該子依賴
|    |    \--- com.android.support:support-fragment:26.1.0
|    |         +--- com.android.support:support-compat:26.1.0 (*)
|    |         +--- com.android.support:support-core-ui:26.1.0 (*)
|    |         \--- com.android.support:support-core-utils:26.1.0 (*)
|    +--- com.android.support:support-vector-drawable:26.1.0
|    |    ...
|    \--- com.android.support:animated-vector-drawable:26.1.0
|         ...
+--- com.android.support:recyclerview-v7:26.1.0
|    //該依賴的子依賴被排除
\--- com.github.bumptech.glide:glide:4.7.1
     +--- com.github.bumptech.glide:gifdecoder:4.7.1
     |    \--- com.android.support:support-annotations:27.1.1
     +--- com.github.bumptech.glide:disklrucache:4.7.1
     \--- com.github.bumptech.glide:annotations:4.7.1

能夠看到指定排除glide依賴的子依賴 com.android.support:support-fragment 不影響其餘依賴。spa

exclude 能夠搭配 groupmodule使用,將會排除全部被匹配的依賴。

2.3 Force 強制指定

強制指定依賴的版本。
configurations.all {
   resolutionStrategy {
       force 'com.android.support:support-fragment:26.1.0'
   }
}

執行結果(部分):code

+--- com.android.support:appcompat-v7:26.1.0
|    ...
|    +--- com.android.support:support-v4:26.1.0
|    |    +--- com.android.support:support-compat:26.1.0
|    |    |    ...
|    |    +--- com.android.support:support-media-compat:26.1.0
|    |    |    ...
|    |    +--- com.android.support:support-core-utils:26.1.0
|    |    |    ...
|    |    +--- com.android.support:support-core-ui:26.1.0
|    |    |    //該依賴被強制指定了版本號
|    |    \--- com.android.support:support-fragment:26.1.0
|    |         +--- com.android.support:support-compat:26.1.0 (*)
|    |         +--- com.android.support:support-core-ui:26.1.0 (*)
|    |         \--- com.android.support:support-core-utils:26.1.0 (*)
|    +--- com.android.support:support-vector-drawable:26.1.0
|    |    ...
|    \--- com.android.support:animated-vector-drawable:26.1.0
|         ...
+--- com.android.support:recyclerview-v7:26.1.0
|    ...
\--- com.github.bumptech.glide:glide:4.7.1
     +--- com.github.bumptech.glide:gifdecoder:4.7.1
     |    \--- com.android.support:support-annotations:27.1.1
     +--- com.github.bumptech.glide:disklrucache:4.7.1
     +--- com.github.bumptech.glide:annotations:4.7.1
          //強制指定了版本號
     \--- com.android.support:support-fragment:27.1.1 -> 26.1.0 (*)

寫的匆忙,若有紕漏,還望指正,若是不當心綁到了你,那真是極好的,今天順便google了一下,如何查看具體的某一個依賴的狀況,可是一直沒有頭緒,使用了dependencyInsight命令也不行。 若是有知道的但願能夠交流一下。 謝謝。

相關文章
相關標籤/搜索