[Android Studio] Gradle 排除子依賴的三種方式

最近在匯入aar時遇到一些問題浪費了很多時間,趁還沒忘記前紀錄一下


假設有兩個依賴套件:

implementation 'app.ruyut.exclude:core:1.0.0'
implementation 'app.ruyut.exclude:example:1.0.0'

假設他們的依賴關係為:

app.ruyut.exclude:core:1.0.0
+--- app.ruyut.exclude:example:1.0.0

'app.ruyut.exclude:core'依賴'app.ruyut.exclude:example:1.0.0'套件


排除子依賴

當某天我們想要匯入第一個但想要排除第二個時

dependencies {
implementation('app.ruyut.exclude:core:1.0.0', {
exclude group: 'app.ruyut.exclude', module: 'example'
})
}

匯入套件時禁止該套件的所有子依賴

dependencies {
implementation('app.ruyut.exclude:core:1.0.0', {
transitive = false
})
}

全部不准匯入此依賴:

android {
configurations {
implementation.exclude group: 'app.ruyut.exclude', module: 'example'
}
}


套件查詢

在使用前最好去MVNrepository查詢一下套件資訊

例如Android AppCompat Library這個就是由許多套件組成的



查看依賴

也可以查看專案中的依賴關係
gradle app:dependencies
但是這樣顯示不是很好查詢,如果過長還會被蓋掉
這時候就可以查看並輸出成文字檔案
gradle app:dependencies > log.log 2>&1
會在執行路徑下產生檔名為log.log的檔案


如果你嘗試多次,還是有依賴發生衝突,或許可以換個方式

我有遇過類似這樣的問題:
Duplicate class app.ruyut.exclude found in modules jetified-android-ruyut-1.0.0-runtime.jar (app.ruyut.exclude:core:1.0.0) and jetified-commonframework-runtime.jar (:commonframework:)

因為依賴套件的衝突是核心依賴(:commonframework:),幾乎把所有各種能想到的方式嘗試過了,但還是不行...

俗話說的好:「解決不了問題,就解決提出問題的人」

因為專案期限,最後的實在是沒有辦法,直接把用到這個衝突套件的地方全部重寫,

(一共四個地方用到,真不懂為什麼原本要寫四次)

寫完花的時間連當初研究的時間一半都不到...

不過希望知道我上面那個依賴衝突正確解法的高手可以指點一下


留言