Android gradle 依赖树和exclude使用
[TOC]
目录
Android gradle 依赖树和exclude使用
一、输出Android Gradle 项目依赖树
1.Gradle task执行task
2.双击help-->dependencies
3.Terminal 通过gradlew执行指令
二、查看项目依赖树
1.依赖内容查看
2.依赖关系和符合
3.官方文档
三、查看依赖树使用场景
1.重复类报错Duplicate class
2.使用特定版本(避免高版本版本冲突覆盖)
Android gradle 依赖树和exclude使用
本文截图根据New UI Beta 页面, ui切换参考 https://www.jianshu.com/p/4190f21fe26b
一、输出Android Gradle 项目依赖树
查看Android Gradle 项目依赖树的方式有很多这里列举比较简单的几种方法
- Gradle task执行task
- 双击help-->dependencies
- Terminal 通过gradlew执行指令
1.Gradle task执行task
点击Gradle图标 --> 点击Gradle task图标幻想task窗口 --> 输入 app:dependencies 执行task gradle app:dependencies具体操作如下图:
Gradle task执行task.png
2.双击help-->dependencies
点击Gradle图标 -->点击 app --> 点击 help --> 双击 dependencies 具体操作如下图:
如果Gradle的app下没有help相关task, 配置显示全部task 参考我之前链接: https://www.jianshu.com/p/9644f901fa41
双击help--dependencies.png
3.Terminal 通过gradlew执行指令
点击左下角的Terminal 图标 --> 在指令窗口中输入 ./gradlew :app:dependencies指令,然后回车执行
Terminal 通过gradlew执行指令.png
此外通过指令我们可以通过 >指定输入内容保存的文件路径 e.g.
//指定通过gradlew 执行 :app:dependencies并将结果 生成到绝对路径 D盘的dependencies.txt中
./gradlew :app:dependencies >D://dependencies.txt
//指定通过gradlew 执行 :app:dependencies并将结果 生成到相对路径 当前项目app/build/dependencies.txt 路径中
./gradlew :app:dependencies >./app/build/dependencies.txt
二、查看项目依赖树
1.依赖内容查看
依赖树输入后依赖关系查看非常简单, 如下为我截取的部分依赖树内容:
+--- androidx.appcompat:appcompat:1.6.1
| +--- androidx.activity:activity:1.6.0
| | +--- androidx.annotation:annotation:1.1.0 -> 1.6.0 (*)
....
| +--- androidx.fragment:fragment:1.3.6
+--- com.github.bumptech.glide:glide:4.12.0
| +--- com.github.bumptech.glide:gifdecoder:4.12.0
| | \--- androidx.annotation:annotation:1.0.0 -> 1.6.0 (*)
| +--- com.github.bumptech.glide:disklrucache:4.12.0
| +--- com.github.bumptech.glide:annotations:4.12.0
| +--- androidx.fragment:fragment:1.0.0 -> 1.3.6 (*)
| +--- androidx.vectordrawable:vectordrawable-animated:1.0.0 -> 1.1.0 (*)
| \--- androidx.exifinterface:exifinterface:1.2.0
| \--- androidx.annotation:annotation:1.1.0 -> 1.6.0 (*)
\--- com.airbnb.android:lottie:5.2.0
+--- androidx.appcompat:appcompat:1.3.1 -> 1.6.1 (*)
\--- com.squareup.okio:okio:1.17.4
从上面内容可以看到主要有一下关系和符号
- 直接依赖
- 传递依赖(Transitive Dependencies)
- 版本冲突与解析
- 星号标记(*)
2.依赖关系和符合
直接依赖
直接依赖是指在项目的 build.gradle 文件中明确声明的依赖项。这些是你主动添加到项目中的库, 从上面依赖树可以看到我直接依赖了
androidx.appcompat:appcompat:1.6.1
com.github.bumptech.glide:glide:4.12.0
com.airbnb.android:lottie:5.2.0
传递依赖(Transitive Dependencies)
传递依赖是由于直接依赖间接引入的其他库。这些库通常是由直接依赖项自身所依赖的。从上面依赖树可以看到传递依赖有
androidx.activity:activity:1.6.0://这是 appcompat:1.6.1 的传递依赖,它需要这个版本的 activity 库来正常工作。
com.github.bumptech.glide:gifdecoder:4.12.0://这是 glide:4.12.0 的传递依赖,用于处理 GIF 解码。
com.squareup.okio:okio:1.17.4://这是 lottie:5.2.0 的传递依赖,用于处理 I/O 操作。
版本冲突与解析
当同一个库的不同版本被不同的依赖引入时,Gradle 会尝试解析出一个合适的版本。这可以通过箭头 (->) 来识别,箭头后面的版本号是最终使用的版本。]
androidx.annotation:annotation:1.1.0 -> 1.6.0://表示有多个依赖引入了不同版本的 annotation 库,但 Gradle 最终选择了 1.6.0 版本。
androidx.fragment:fragment:1.0.0 -> 1.3.6://glide:4.12.0 引入了 fragment:1.0.0,但 Gradle 解析后选择了 fragment:1.3.6
androidx.vectordrawable:vectordrawable-animated:1.0.0 -> 1.1.0://glide:4.12.0 引入了 vectordrawable-animated:1.0.0,但 Gradle 解析后选择了 1.1.0。
星号标记()*
星号 ((*)) 表示该依赖已经被之前列出,并且其完整路径已经在依赖树中显示过,因此不再重复展示。这有助于简化输出并避免冗余信息。
3.官方文档
有兴趣的可以查看官方文档链接如下:
声明依赖:https://docs.gradle.org/current/userguide/declaring_dependencies.html
依赖解析策略:https://docs.gradle.org/current/userguide/dependency_resolution.html
三、查看依赖树使用场景
我们需要使用到依赖树的场景通常如下
- 重复类报错Duplicate class
- 使用特定版本(避免高版本版本冲突覆盖)
1.重复类报错Duplicate class
我们使用Android studio编译的时候有时候会出现编译报错Duplicate class xxx
这个时候我们可以通过上面输出依赖树, 然后通过exclude指令去排除
implementation ("com.github.bumptech.glide:glide:4.12.0"){
exclude group: 'com.squareup.okio', module: 'okio' //如果group就已经确定了唯一, module相关内容可以省略
}
2.使用特定版本(避免高版本版本冲突覆盖)
有时候我们发布自己定义的aar时会存在某个aar带入了一个高版本的aar; 我们只希望使用一个低版本的
+--- androidx.appcompat:appcompat:1.6.1
| +--- androidx.activity:activity:1.6.0
| | +--- androidx.annotation:annotation:1.1.0 -> 1.6.0 (*)
....
| +--- androidx.fragment:fragment:1.3.6
+--- com.github.bumptech.glide:glide:4.12.0
| +--- com.github.bumptech.glide:gifdecoder:4.12.0
| | \--- androidx.annotation:annotation:1.0.0 -> 1.6.0 (*)
| +--- com.github.bumptech.glide:disklrucache:4.12.0
| +--- com.github.bumptech.glide:annotations:4.12.0
| +--- androidx.fragment:fragment:1.0.0 -> 1.3.6 (*)
比如上面输出的依赖树中 androidx.appcompat:appcompat:1.6.1 里面包含了androidx.fragment:fragment:1.3.6
如果我们项目只需要使用androidx.fragment:fragment:1.0.0 不希望使用高版本; 则我们需要找到所有高版本的进行排除
implementation ("androidx.appcompat:appcompat:1.6.1"){
exclude group: 'androidx.fragment', module: 'fragment'
}
通常我们不会exclude androidx.fragment, 我上面只是为了方便举例说明场景