当前位置:网站首页>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 依赖。
尾声
一转眼时间真的过的飞快。我们各奔东西,也各自踏上了自己的旅途,但是即使多年不见,也因为这份情谊我们依旧如从前那般“亲密”。不忘初心方得始终。加油吧,程序员们,在我看来35岁,40岁从来不是危机,只要永远不要忘记自己为何踏上征程!
最后需要同款资料的,可以 私信我回复【学习】或者扫描下方二维码我愿意分享给你!
为了让更多在学习中或者最近要准备面试的朋友们看到这篇文章,希望你们能多多评论,点赞+转发!
**再次感谢所有给我提供过题目的朋友们,感谢一路有你!
边栏推荐
- Excel calculates the difference between time and date and converts it into minutes
- [untitled]
- [flutter] icons component (load the built-in icon of flutter | display the material design icon completely)
- What is needed to develop a domestic arm intelligent edge computing gateway
- 寻找标杆战友 | 百万级实时数据平台,终身免费使用
- 【FPGA教程案例5】基于vivado核的ROM设计与实现
- Mongodb common commands of mongodb series
- 【系统分析师之路】第五章 复盘软件工程(开发模型开发方法)
- Button wizard play strange learning - go back to the city to buy medicine and add blood
- MySQL基础用法02
猜你喜欢
![[AUTOSAR I overview]](/img/e4/b97c6beebf6f431d2d7cf209c6683e.png)
[AUTOSAR I overview]

Explain the basic concepts and five attributes of RDD in detail

Embrace the safety concept of platform delivery

Correctly distinguish the similarities and differences among API, rest API, restful API and web service
![[C language] branch and loop statements (Part 1)](/img/47/6efcc59bd26e26f66c698635c26c8b.png)
[C language] branch and loop statements (Part 1)
![[love crash] neglected details of gibaro](/img/d6/baa4b5185ddaf88f3df71a94a87ee2.jpg)
[love crash] neglected details of gibaro
![[shutter] image component (configure local GIF image resources | load placeholder with local resources)](/img/73/19e2e0fc5ea6f05e34584ba40a452d.jpg)
[shutter] image component (configure local GIF image resources | load placeholder with local resources)

机器学习术语

leetcode:871. 最低加油次数【以前pat做过 + 最大堆 +贪心】

leetcode:701. 二叉搜索树中的插入操作【bst的插入】
随机推荐
The difference between relational database and non relational database
[AUTOSAR 11 communication related mechanism]
RISA rz/g2l processor introduction | frame diagram | power consumption | schematic diagram and hardware design guide
Strongly connected components of digraph
【FH-GFSK】FH-GFSK信号分析与盲解调研究
On Fibonacci sequence
leetcode:871. 最低加油次数【以前pat做过 + 最大堆 +贪心】
按键精灵打怪学习-自动回城路线的判断
[AUTOSAR XIII NVM]
FPGA - 7 Series FPGA internal structure clocking -04- multi area clock
递归处理组织的几种情况
Reading and writing speed of Reza rz/g2l arm development board storage and network measurement
Kivy教程大全之如何在 Kivy 中创建下拉列表
Machine learning terminology
Excel calculates the difference between time and date and converts it into minutes
Excel removes the data after the decimal point and rounds the number
18_微信小程序之微信视频号滚动自动播放视频效果实现2.0
信息熵的基础
正确甄别API、REST API、RESTful API和Web Service之间的异同
如今少年已归来,人间烟火气最抚凡人心 复工了~