当前位置:网站首页>[Androd] Gradle 使用技巧之模块依赖替换
[Androd] Gradle 使用技巧之模块依赖替换
2022-07-03 00:55:00 【小陈乱敲代码】
背景
我们在多模块项目开发过程中,会遇到这样的场景,工程里依赖了一个自己的或者其他同事的 aar 模块,有时候为了开发调试方便,经常会把 aar 改为本地源码依赖,开发完毕并提交的时候,会再修改回 aar 依赖,这样就会很不方便,开发流程图示如下:

解决
一开始我们通过在 app 的 build.gradle 里的 dependency 判断如果是需要本地依赖的 aar,就替换为 implementation project 依赖,伪代码如下:
dependencies {
if(enableLocalModule) {
implementation 'custom:test:0.0.1'
} else {
implementation project(path: ':test')
}
}
这样就可以不用每次提交代码还要修改回 aar 依赖,但是如果其他模块如果也依赖了该 aar 模块,就会出现问题,虽然可以继续修改其他模块里的依赖方式,但是这样就会有侵入性,而且不能彻底解决问题,仍然有可能出现本地依赖和 aar 依赖的代码不一致问题。
Gradle 官方针对这种场景提供了更好的解决方式 DependencySubstitution,使用方式如下:
步骤1:在 settting.gradle,添加如下代码:
// 加载本地 module
if (file("local.properties").exists()) {
def properties = new Properties()
def inputStream = file("local.properties").newDataInputStream()
properties.load( inputStream )
def moduleName = properties.getProperty("moduleName")
def modulePath = properties.getProperty("modulePath")
if (moduleName != null && modulePath != null) {
include moduleName
project(moduleName).projectDir = file(modulePath)
}
}
步骤2:在 app 的 build.gradle 添加以下代码
configurations.all {
resolutionStrategy.dependencySubstitution.all { DependencySubstitution dependency ->
// use local module
if (dependency.requested instanceof ModuleComponentSelector && dependency.requested.group == "custom") {
def targetProject = findProject(":test")
if (targetProject != null) {
dependency.useTarget targetProject
}
}
}
}
步骤3::在 local.properties 里
moduleName=:test
modulePath=../AndroidStudioProjects/TestProject/testModule
到这里就大功告成了,后续只需要在 local.properties 里开启和关闭,即可实现 aar 模块本地依赖调试,提交代码也不用去手动修改回 aar 依赖。
最后
赠送大家一套完整的Android学习资料吧。
以前一直是自己在网上东平西凑的找,找到的东西也是零零散散,很多时候都是看着看着就没了,时间浪费了,问题却还没得到解决,很让人抓狂。
后面我就自己整理了一套资料,还别说,真香!
资料有条理,有系统,还很全面,我不方便直接放出来,大家可以先看看有没有用得到的地方吧。
**附上白嫖地址:《Android架构视频+BATJ面试专题PDF+学习笔记》






边栏推荐
猜你喜欢

MySQL foundation 04 MySQL architecture

Reading and writing speed of Reza rz/g2l arm development board storage and network measurement

dotConnect for PostgreSQL数据提供程序

有向图的强连通分量

Excel removes the data after the decimal point and rounds the number

Matlab saves the digital matrix as geospatial data, and the display subscript index must be of positive integer type or logical type. Solve the problem

Foundations of data science is free to download
![[case sharing] let the development of education in the new era advance with](/img/11/af88d16dc66f00840cbfc5ba5d68bd.jpg)
[case sharing] let the development of education in the new era advance with "number"
![leetcode:701. Insertion in binary search tree [BST insertion]](/img/bc/1dda73198488eb81b49be2c1dff6c2.png)
leetcode:701. Insertion in binary search tree [BST insertion]

How wide does the dual inline for bread board need?
随机推荐
1038 Recover the Smallest Number
比较版本号
matlab查找某一行或者某一列在矩阵中的位置
Basic use of sringcloud & use of component Nacos
Kivy教程大全之 创建您的第一个kivy程序 hello word(教程含源码)
Solve the cache problem of reactnative using WebView
2022.2.14 resumption
异步、邮件、定时三大任务
按键精灵打怪学习-自动寻路回打怪点
[C language] branch and loop statements (Part 1)
【FPGA教程案例6】基于vivado核的双口RAM设计与实现
Excel if formula determines whether the two columns are the same
[overview of AUTOSAR three RTE]
Key wizard hit strange learning - automatic path finding back to hit strange points
Array and collection performance comparison
每日一题之干草堆的移动
Merge K sorted linked lists
leetcode:871. 最低加油次数【以前pat做过 + 最大堆 +贪心】
【系统分析师之路】第五章 复盘软件工程(开发模型开发方法)
Find a benchmark comrade in arms | a million level real-time data platform, which can be used for free for life