AS下解决Gradle dependencies compile jar包冲突、重复问题

我们希望能够尽我们所能,来让这个世界变的更简单,如果你想了解我们,请点击这里


一、情景复现:
在使用 dependencies { compile … } 添加 libs时,经常遇到同一个lib

出现了两个不同的版本,导致不同的问题。

例如:工程 A 添加了 rxandroid:2.0.1adapter-rxjava

两个libs.

而adapter-rxjava中使用了rxandroid:1.1.5

这样在工程中就会出现两个不同的类库。
引用的部分libs:

compile ‘io.reactivex.rxjava2:rxandroid:2.0.1’ 
compile ‘io.reactivex.rxjava2:rxjava:2.0.1’ 
compile ‘com.google.code.gson:gson:2.7’ 
compile ‘com.squareup.retrofit2:retrofit:2.1.0’ 
compile ‘com.squareup.retrofit2:converter-gson:2.1.0’ 
compile (‘com.squareup.retrofit2:adapter-rxjava:2.1.0’)
编译之后 如下图:

这里写图片描述

二、问题分析:

1、 rxandroid:1.1.5 在 dependencies 并没有添加1.1.5版本(注意工程中添加的是rxandroid:2.0.1),此lib从何而来?

答案是肯定的,显然在dependencies 添加的其他libs中有使用到 rxandroid:1.1.5 版本的。

2、多余的rxandroid:1.1.5 是被那个lib使用到的呢?如何知道呢?

在文件目录下或 Android Studio的 Terminal下敲

gradlew -q app:dependencies

便有各个libs引用关系的输出:

+--- com.squareup.okhttp3:okhttp:3.6.0
|    \--- com.squareup.okio:okio:1.11.0
+--- org.greenrobot:eventbus:3.0.0
+--- com.jakewharton:butterknife:8.5.1
|    +--- com.jakewharton:butterknife-annotations:8.5.1
|    |    \--- com.android.support:support-annotations:25.1.0 -> 25.1.1
|    +--- com.android.support:support-annotations:25.1.0 -> 25.1.1
|    \--- com.android.support:support-compat:25.1.0 -> 25.1.1 (*)
+--- io.reactivex.rxjava2:rxandroid:2.0.1
|    \--- io.reactivex.rxjava2:rxjava:2.0.1
|         \--- org.reactivestreams:reactive-streams:1.0.0
+--- io.reactivex.rxjava2:rxjava:2.0.1 (*)
+--- com.google.code.gson:gson:2.7
+--- com.squareup.retrofit2:retrofit:2.1.0
|    \--- com.squareup.okhttp3:okhttp:3.3.0 -> 3.6.0 (*)
+--- com.squareup.retrofit2:converter-gson:2.1.0
|    +--- com.squareup.retrofit2:retrofit:2.1.0 (*)
|    \--- com.google.code.gson:gson:2.7
\--- com.squareup.retrofit2:adapter-rxjava:2.1.0
     +--- com.squareup.retrofit2:retrofit:2.1.0 (*)
     \--- io.reactivex:rxjava:1.1.5
通过各个libs引用的关系图,就可以清楚的看出:
adapter-rxjava:2.1.0 中使用的是 io.reactivex:rxjava:1.1.5

三、问题解决:

1、知道是那个libs中和rxandroid:1.1.5 ,问题就好解决了,直接

exclude 问题解决!

compile (‘com.squareup.retrofit2:adapter-rxjava:2.1.0’){ 
exclude module: ‘rxjava’ 
}
注意: 
“(…)” 千万别省!!! 
“(…)” 千万别省!!! 
“(…)” 千万别省!!!

猜你喜欢

转载自blog.csdn.net/u011068996/article/details/79106356