详解Android获取所有依赖库的几种方式
作者:FullStack 时间:2023-12-13 05:41:51
当项目越来越大的时候,依赖的库也越来越多,再加上aar的传递依赖,导致dependency的急速膨胀。我们可以通过如下几种方式,查看项目依赖的所有库(包含直接依赖和间接依赖)。
方式一:通过dependencies命令
./gradlew :app:dependencies
该task会显示如下所示的输出:
输出列表展示了所有configuration下的依赖树,依赖关系明显,层次清晰。如果觉得输出的结果太冗长(通常情况下包含几十个configuration),可以通过指定configuration来显示特定的依赖树:
./gradlew :app:dependencies --configuration releaseCompileClasspath
该命令只会显示release模式下编译过程中的依赖树。
方式二: 通过androidDependencies命令
./gradlew :app:androidDependencies
输出结果如下:
如图所示,该task会平铺展示依赖树,并且只展示几个主要的variant,看起来较为清爽,但是缺点是不能像方式一那样指定configuration。
方式三:自定义task获取
project.afterEvaluate {
project.android.applicationVariants.all { variant ->
tasks.create(name: "showDependencies${variant.name.capitalize()}",
description: "展示所有依赖") {
doLast {
Configuration configuration
try {
// 3.x
configuration = project.configurations."${variant.name}CompileClasspath"
} catch (Exception e) {
// 2.x
configuration = project.configurations."_${variant.name}Compile"
}
configuration.resolvedConfiguration.lenientConfiguration.allModuleDependencies.each {
def identifier = it.module.id
println("${identifier.group}:${identifier.name}:${identifier.version}")
}
}
}
}
}
如上,通过这种自定义task的方式,可以选择打印依赖,也可以选择保存到文件中,灵活度最高。
总结:
方式一:通用task,按层次展示依赖树,可以通过指定configuration来过滤输出。
方式二:android项目特有的task,平铺展示依赖树,不能过滤输出结果。
方式三:自定义task获取依赖,灵活度最高,但是需要对gradle有较深的理解。
来源:https://www.jianshu.com/p/1360299a4c17
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
JAVA实现SOCKET多客户端通信的案例
![](https://img.aspxhome.com/file/2023/0/122950_0s.jpg)
基于标准http实现Android多文件上传
Java实现简单员工管理系统
![](https://img.aspxhome.com/file/2023/2/60932_0s.jpg)
SpringBoot常用注解详细整理
![](https://img.aspxhome.com/file/2023/3/75763_0s.jpg)
详解Android Webview加载网页时发送HTTP头信息
深入c# GDI+简单绘图的具体操作步骤(三)
![](https://img.aspxhome.com/file/2023/9/113829_0s.jpg)
IDEA 2020 本土化,真的是全中文了(真香)
![](https://img.aspxhome.com/file/2023/6/60196_0s.png)
SpringMVC使用hibernate-validator进行参数校验最佳实践记录
SpringBoot+Spring Security+JWT实现RESTful Api权限控制的方法
![](https://img.aspxhome.com/file/2023/4/65604_0s.png)
Android实现授权访问网页的方法
详解Spring Bean 之间的特殊关系
SpringBoot整合Activiti工作流框架的使用
![](https://img.aspxhome.com/file/2023/2/63842_0s.png)
Android实现复制Assets文件到SD卡
![](https://img.aspxhome.com/file/2023/6/139186_0s.jpg)
Android中退出确认框的实现代码
![](https://img.aspxhome.com/file/2023/9/100629_0s.jpg)
Java零基础精通方法篇
![](https://img.aspxhome.com/file/2023/1/75481_0s.png)
flutter TextField换行自适应的实现
Android中Retrofit的简要介绍
![](https://img.aspxhome.com/file/2023/7/97847_0s.png)
详解C++ STL模拟实现forward_list
![](https://img.aspxhome.com/file/2023/0/60100_0s.jpg)
Android实现双层ViewPager嵌套
![](https://img.aspxhome.com/file/2023/4/96804_0s.gif)
Mybatis generator如何自动生成代码
![](https://img.aspxhome.com/file/2023/0/58180_0s.png)