当前位置:网站首页>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岁从来不是危机,只要永远不要忘记自己为何踏上征程!
最后需要同款资料的,可以 私信我回复【学习】或者扫描下方二维码我愿意分享给你!
为了让更多在学习中或者最近要准备面试的朋友们看到这篇文章,希望你们能多多评论,点赞+转发!
**再次感谢所有给我提供过题目的朋友们,感谢一路有你!
边栏推荐
猜你喜欢

dotConnect for PostgreSQL数据提供程序
![[overview of AUTOSAR four BSW]](/img/19/c2273bbedb7f8d859e5a3805ed5740.png)
[overview of AUTOSAR four BSW]

【爱死机】《吉巴罗》被忽略的细节

Matlab Doppler effect produces vibration signal and processing

(C language) data storage

leetcode 2097 — 合法重新排列数对
![[AUTOSAR + IO Architecture]](/img/cf/9ea42b50bed298c0546764b63bd957.png)
[AUTOSAR + IO Architecture]

Initial order of pointer (basic)

瑞萨电子RZ/G2L开发板上手评测

Excel if formula determines whether the two columns are the same
随机推荐
攻克哈希的基本概念与实现
Strongly connected components of digraph
[love crash] neglected details of gibaro
Database SQL language 01 where condition
无向图的割点
[AUTOSAR + IO Architecture]
[Arduino experiment 17 L298N motor drive module]
[introduction to AUTOSAR seven tool chain]
12_ Implementation of rolling automatic video playback effect of wechat video number of wechat applet
合并K个已排序的链表
Embrace the safety concept of platform delivery
leetcode 6103 — 从树中删除边的最小分数
Basic remote connection tool xshell
[AUTOSAR VI description document]
[case sharing] let the development of education in the new era advance with "number"
How to convert Quanzhi a40i/t3 to can through SPI
R language generalized linear model function GLM, (model fit and expression diagnostics), model adequacy evaluation method, use plot function and car package function
产业互联网的产业范畴足够大 消费互联网时代仅是一个局限在互联网行业的存在
Basis of information entropy
465. DFS backtracking of optimal bill balance