传递依赖与排除 (Exclude)
在 Gradle 中,默认会自动下载所有依赖库所需要的库。但这往往会导致冲突或引入多余代码。
1. 排除特定依赖 (Exclude)
如果你依赖了 A 库,但 A 库自带了一个旧版的 okio,而你想用自己的:
kotlin
implementation("com.some.library:A:1.0.0") {
exclude(group = "com.squareup.okio", module = "okio")
}2. 强行禁止依赖 (Strict)
如果你想禁止全项目出现某个库:
kotlin
configurations.all {
exclude(group = "org.apache.httpcomponents", module = "httpclient")
}3. 依赖可视化 (Dependency Insight)
当你想知道为什么某个库被引入了,使用 dependencyInsight:
bash
./gradlew :app:dependencyInsight --dependency okio --configuration debugRuntimeClasspath它会清晰地画出依赖路径。