天天看點

gradle 使用 exclude 解決jar包沖突

1. 檢視沖突的jar包;一眼能看出來的跳到第二步; 遇到隐藏比較深的就比較難受了,肉眼完全看不出來,此時使用以下指令檢視:

gradlew -q app:dependencies 檢視依賴樹;

比如: 

Duplicate class androidx.dynamicanimation.animation.DynamicAnimation$OnAnimationUpdateListener found in modules classes.jar (androidx.dynamicanimation:dynamicanimation:1.0.0) and classes.jar (dynamicanimation-1.1.0-alpha04.aar)

Duplicate class androidx.dynamicanimation.animation.DynamicAnimation$ViewProperty found in modules classes.jar (androidx.dynamicanimation:dynamicanimation:1.0.0) and classes.jar (dynamicanimation-1.1.0-alpha04.aar)

gradlew -q app:dependencies:

。。。

+--- com.airbnb.android:lottie:3.3.1

|    +--- androidx.appcompat:appcompat:1.0.0 -> 1.3.0-rc01 (*)

|    \--- com.squareup.okio:okio:1.17.4

+--- com.google.android.material:material:1.3.0

|    +--- androidx.annotation:annotation:1.0.1 -> 1.2.0

|    +--- androidx.appcompat:appcompat:1.1.0 -> 1.3.0-rc01 (*)

|    +--- androidx.cardview:cardview:1.0.0

|    |    \--- androidx.annotation:annotation:1.0.0 -> 1.2.0

|    +--- androidx.coordinatorlayout:coordinatorlayout:1.1.0 (*)

|    +--- androidx.constraintlayout:constraintlayout:2.0.1 -> 2.1.0-beta01 (*)

|    +--- androidx.core:core:1.2.0 -> 1.6.0-alpha01 (*)

|    +--- androidx.dynamicanimation:dynamicanimation:1.0.0

|    |    +--- androidx.core:core:1.0.0 -> 1.6.0-alpha01 (*)

|    |    +--- androidx.collection:collection:1.0.0 -> 1.1.0 (*)

|    |    \--- androidx.legacy:legacy-support-core-utils:1.0.0 (*)

|    +--- androidx.annotation:annotation-experimental:1.0.0

|    +--- androidx.fragment:fragment:1.0.0 -> 1.3.2 (*)

|    +--- androidx.lifecycle:lifecycle-runtime:2.0.0 -> 2.3.1 (*)

|    +--- androidx.recyclerview:recyclerview:1.0.0 -> 1.2.0-rc01 (*)

右鍵find, 找到dynamicanimation:dynamicanimation,可以看到com.google.android.material:material:1.3.0 引入了它;

2.使用exclude過濾掉;

implementation ('com.google.android.material:material:1.3.0') {
    exclude group: "androidx.dynamicanimation", module: "dynamicanimation"
}