Android Gradle 管理依赖包

本文详细介绍了Android Gradle在管理依赖包时遇到的问题及其解决方案,包括查看依赖关系、理解implementation、compileOnly、api的区别、处理重复依赖与多版本依赖冲突,以及exclude排除依赖包的方法。通过实例解析,帮助开发者解决Gradle配置中的常见问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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:${arouter}"
     kapt "com.alibaba:arouter-compiler:${arouter}"
     implementation project(":lib_order")
     implementation project(":lib_pay")
    
  2. 得到的依赖结果以 debugCompileClasspath 为例,如下:
     debugCompileClasspath - Compile classpath for compilation 'debug' (target  (androidJvm)).
     +--- org.jetbrains.kotlin:kotlin-stdlib:1.3.72
     |    +--- org.jetbrains.kotlin:kotlin-stdlib-common:1.3.72
     |    \--- org.jetbrains:annotations:13.0
     +--- androidx.core:core-ktx:1.3.2
     |    +--- org.jetbrains.kotlin:kotlin-stdlib:1.3.71 -> 1.3.72 (*)
     |    +--- androidx.annotation:annotation:1.1.0
     |    \--- androidx.core:core:1.3.2
     |         +--- androidx.annotation:annotation:1.1.0
     |         +--- androidx.lifecycle:lifecycle-runtime:2.0.0 -> 2.1.0
     |         |    +--- androidx.lifecycle:lifecycle-common:2.1.0
     |         |    |    \--- androidx.annotation:annotation:1.1.0
     |         |    +--- androidx.arch.core:core-common:2.1.0
     |         |    |    \--- androidx.annotation:annotation:1.1.0
     |         |    \--- androidx.annotation:annotation:1.1.0
     |         \--- androidx.versionedparcelable:versionedparcelable:1.1.0
     |              +--- androidx.annotation:annotation:1.1.0
     |              \--- androidx.collection:collection:1.0.0 -> 1.1.0
     |                   \--- androidx.annotation:annotation:1.1.0
     +--- androidx.appcompat:appcompat:1.2.0
     |    +--- a
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值