文章目录
Android Gradle 管理依赖包
Android 开发常见的gradle 依赖包相关的问题有如下:
Android 开发常见 Gradle 问题
- 如何查看项目里面引入的某个依赖包,具体是由哪个三方库引入的?
- 问题场景1,多版本依赖:项目里面某个库存在多个版本,导致编译冲突,如何查看某个版本的库是由谁引入的 ?
- 问题场景2,相同版本重复依赖:相同的依赖包,被重复引入。某些第三方库提供时内部已经集成了部分依赖库,接入方在接入时,同时引入了该版本的库。
- 解决参看:解决问题1
- gradle 下载慢问题如何解决?
- 解决参看:gradle 下载慢问题
- gradle 下载的依赖包存放位置在哪儿,如何手动管理?
1. 查看 Gradle 项目包依赖关系
命令总结:
gradle :app:dependencies --configuration debugCompileClasspath > de.txt
为了解决问题1, 我们需要知道如何查看项目里的依赖关系,可以通过gradle命令查看:
// 开发环境已经配置gradle环境变量时直接使用gradle,模块名替换成项目中的模块名如 app
gradle :模块名:dependencies > de.txt
上述命令,将指定模块的依赖关系,输出到 de.txt
文件中,若环境中没有配gradle环境变量,也可以使用项目里面的graldew
,效果是一样的:
// 若无法执行,可先给 graldew 加可执行权限:chmod +x gradlew
./graldew :模块名:dependencies > de.txt
1.1 gradle :app:dependencies --configuration debugCompileClasspath
打开 de.txt
,里面会根据不同的编译模式,输出相应的依赖关系,例如有:
debugAndroidTestCompileClasspath - Compile classpath for compilation 'debugAndroidTest' (target (androidJvm)).
debugCompileClasspath - Compile classpath for compilation 'debug' (target (androidJvm)).
debugUnitTestCompileClasspath - Compile classpath for compilation 'debugUnitTest' (target (androidJvm)).
releaseCompileClasspath - Compile classpath for compilation 'release' (target (androidJvm)).
releaseUnitTestCompileClasspath - Compile classpath for compilation 'releaseUnitTest' (target (androidJvm)).
...
这里,我们重点关注 debugCompileClasspath
(即 debug下的依赖关系) 与 releaseCompileClasspath
(即 release下的依赖关系)。
我们还可以通过加过滤参数 --configuration 编译模式
,只关注对应编译模式下的依赖关系,例如:
gradle :app:dependencies --configuration debugCompileClasspath > de.txt
- app module 的build.gradle如下 :
implementation "org.jetbrains.kotlin:kotlin-stdlib:${kotlin_version}" implementation "androidx.core:core-ktx:${androidx_core_ktx}" implementation "androidx.appcompat:appcompat:${androidx_appcompat}" implementation "androidx.constraintlayout:constraintlayout:${androidx_constraintlayout}" implementation "com.google.android.material:material:${material}" implementation "com.alibaba:arouter-api:${arouter}" annotationProcessor "com.alibaba:arouter-compiler: