./gradlew -q moduleName:dependencies > outfile.txt
./gradlew -q dependencies moduleName1:dependencies moduleName2:dependencies > outfile.txt
# 只看通过implementation依赖的库关系
./gradlew -q moduleName:dependencies --configuration implementation > outfile.txt
其中moduleName为Module的名字,如app;
outfile.txt 是任务执行结果输出的文件,由于这些命令输出的内容比较多,在终端查看和搜索都不是很方便,因此建议将结果输出到文件。
举个例子:
在终端(控制台)执行:
./gradlew -q tools:dependencies > ./build/deps_tools.txt
部分结果如下:
+--- com.google.zxing:core:3.3.0
+--- io.reactivex.rxjava2:rxandroid:2.0.1
| \--- io.reactivex.rxjava2:rxjava:2.0.1 -> 2.1.5
| \--- org.reactivestreams:reactive-streams:1.0.1
+--- io.reactivex.rxjava2:rxjava:2.1.5 (*)
+--- com.squareup.okhttp3:okhttp:3.9.0
| \--- com.squareup.okio:okio:1.13.0
+--- com.squareup.okhttp3:logging-interceptor:3.9.0
| \--- com.squareup.okhttp3:okhttp:3.9.0 (*)
+--- com.android.support:support-annotations:27.1.1
+--- com.android.support:appcompat-v7:27.1.1
| +--- com.android.support:support-annotations:27.1.1
| +--- com.android.support:support-core-utils:27.1.1
| | +--- com.android.support:support-annotations:27.1.1
| | \--- com.android.support:support-compat: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-fragment:27.1.1
| | +--- com.android.support:support-compat:27.1.1 (*)
| | +--- com.android.support:support-core-ui: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-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:27.1.1
| | +--- com.android.support:support-annotations:27.1.1
| | \--- com.android.support:support-compat:27.1.1 (*)
| \--- com.android.support:animated-vector-drawable:27.1.1
| +--- com.android.support:support-vector-drawable:27.1.1 (*)
| \--- com.android.support:support-core-ui:27.1.1 (*)
+--- com.orhanobut:logger:2.1.1
+--- com.github.bumptech.glide:glide:4.2.0
| +--- com.github.bumptech.glide:gifdecoder:4.2.0
| | \--- com.android.support:support-annotations:26.0.2 -> 27.1.1
| +--- com.github.bumptech.glide:disklrucache:4.2.0
| \--- com.github.bumptech.glide:annotations:4.2.0
+--- com.alibaba:arouter-api:1.2.4
| +--- com.alibaba:arouter-annotation:1.0.4
| \--- com.android.support:support-v4:25.2.0
| +--- com.android.support:support-compat:25.2.0 -> 27.1.1 (*)
| +--- com.android.support:support-media-compat:25.2.0
| | +--- com.android.support:support-annotations:25.2.0 -> 27.1.1
| | \--- com.android.support:support-compat:25.2.0 -> 27.1.1 (*)
| +--- com.android.support:support-core-utils:25.2.0 -> 27.1.1 (*)
| +--- com.android.support:support-core-ui:25.2.0 -> 27.1.1 (*)
| \--- com.android.support:support-fragment:25.2.0 -> 27.1.1 (*)
+--- org.jetbrains.kotlin:kotlin-stdlib-jdk7:1.2.41
| \--- org.jetbrains.kotlin:kotlin-stdlib:1.2.41
| \--- org.jetbrains:annotations:13.0
\--- org.jetbrains.anko:anko:0.10.4
+--- org.jetbrains.kotlin:kotlin-stdlib:1.1.0 -> 1.2.41 (*)
+--- org.jetbrains.anko:anko-commons:0.10.4
| \--- org.jetbrains.kotlin:kotlin-stdlib:1.1.0 -> 1.2.41 (*)
+--- org.jetbrains.anko:anko-coroutines:0.10.4
| +--- org.jetbrains.kotlin:kotlin-stdlib:1.1.0 -> 1.2.41 (*)
| \--- org.jetbrains.kotlinx:kotlinx-coroutines-android:0.15
| +--- org.jetbrains.kotlinx:kotlinx-coroutines-core:0.15
| | \--- org.jetbrains.kotlin:kotlin-stdlib:1.1.2 -> 1.2.41 (*)
| \--- org.jetbrains.kotlin:kotlin-stdlib:1.1.2 -> 1.2.41 (*)
+--- org.jetbrains.anko:anko-sqlite:0.10.4
| +--- org.jetbrains.kotlin:kotlin-stdlib:1.1.0 -> 1.2.41 (*)
| \--- org.jetbrains.anko:anko-commons:0.10.4 (*)
+--- org.jetbrains.anko:anko-sdk25:0.10.4
| +--- org.jetbrains.kotlin:kotlin-stdlib:1.1.0 -> 1.2.41 (*)
| \--- org.jetbrains.anko:anko-commons:0.10.4 (*)
+--- org.jetbrains.anko:anko-sdk25-coroutines:0.10.4
| +--- org.jetbrains.kotlin:kotlin-stdlib:1.1.0 -> 1.2.41 (*)
| +--- org.jetbrains.kotlinx:kotlinx-coroutines-android:0.15 (*)
| \--- org.jetbrains.anko:anko-commons:0.10.4 (*)
+--- org.jetbrains.anko:anko-appcompat-v7:0.10.4
| +--- org.jetbrains.kotlin:kotlin-stdlib:1.1.0 -> 1.2.41 (*)
| +--- org.jetbrains.anko:anko-support-v4:0.10.4
| | +--- org.jetbrains.kotlin:kotlin-stdlib:1.1.0 -> 1.2.41 (*)
| | +--- org.jetbrains.anko:anko-commons:0.10.4 (*)
| | \--- org.jetbrains.anko:anko-support-v4-commons:0.10.4
| | +--- org.jetbrains.kotlin:kotlin-stdlib:1.1.0 -> 1.2.41 (*)
| | \--- org.jetbrains.anko:anko-commons:0.10.4 (*)
| \--- org.jetbrains.anko:anko-appcompat-v7-commons:0.10.4
| +--- org.jetbrains.kotlin:kotlin-stdlib:1.1.0 -> 1.2.41 (*)
| \--- org.jetbrains.anko:anko-support-v4-commons:0.10.4 (*)
\--- org.jetbrains.anko:anko-appcompat-v7-coroutines:0.10.4
+--- org.jetbrains.kotlin:kotlin-stdlib:1.1.0 -> 1.2.41 (*)
+--- org.jetbrains.kotlinx:kotlinx-coroutines-android:0.15 (*)
+--- org.jetbrains.anko:anko-support-v4-commons:0.10.4 (*)
\--- org.jetbrains.anko:anko-appcompat-v7-commons:0.10.4 (*)
其中 * 表示 之前已经列出过这个依赖,就不会再下载了
其中 -> 表示 低版本 -> 高版本,会强制依赖项目中版本高的依赖库
依赖冲突一般有两种,一个是版本冲突,一个是重复依赖。
解决依赖冲突的原则就是最终确定依赖一个固定的版本的库。
可以用多种方法解决依赖冲突:
依赖传递指的是:如果A依赖于B,则A也同样依赖B所依赖的库。
可以通过transitive来控制
统一指定transitive:
//建议不要这么做,因为一旦关闭所有的依赖传递,你很可能不清楚具体依赖什么库,而导致出错
configurations.all {
transitive = false
}
单独排除关闭某个库的依赖传递:
implementation("com.android.support:support-compat:27.1.1"){
transitive = false
}
gradle还可以强制使用某个版本的依赖
比如项目中有的库使用了低版本的lifecyle库或者support库,这个时候引入最新的Lifecycle库就会出现版本冲突,可以强制指定版本:
configurations.all {
resolutionStrategy {
def lifecycle_version = "1.1.1"
force "android.arch.lifecycle:extensions:$lifecycle_version"
force "android.arch.lifecycle:runtime:$lifecycle_version"
force "android.arch.lifecycle:compiler:$lifecycle_version"
}
}
当出现依赖冲突的时候,比如重复依赖,可以排除掉多余的依赖,或者排除低版本的依赖。
//排除RecyclerView依赖库
configurations.all {
exclude group: 'com.android.support', module: 'recyclerview-v7'
}
//排除所有support库包括v7,v4等
configurations.all {
exclude group: 'com.android.support'
}
//单独排除依赖
implementation("com.google.dagger:dagger:2.15") {
exclude group: 'javax.inject', module: 'javax.inject'
}
http://www.paincker.com/gradle-dependencies#i-5