Android Gradle 管理依赖包

Android Gradle 管理依赖包

Android 开发常见的gradle 依赖包相关的问题有如下:

Android 开发常见 Gradle 问题

  1. 如何查看项目里面引入的某个依赖包,具体是由哪个三方库引入的?
    • 问题场景1,多版本依赖:项目里面某个库存在多个版本,导致编译冲突,如何查看某个版本的库是由谁引入的 ?
    • 问题场景2,相同版本重复依赖:相同的依赖包,被重复引入。某些第三方库提供时内部已经集成了部分依赖库,接入方在接入时,同时引入了该版本的库。
    • 解决参看:解决问题1
  2. gradle 下载慢问题如何解决?
  3. 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
  1. 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:
  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值