当前位置:网站首页>[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+学习笔记》






边栏推荐
- Foundations of data science is free to download
- Usage of using clause in kingbases alter table
- 比较版本号
- Matlab Doppler effect produces vibration signal and processing
- 异步、郵件、定時三大任務
- matlab查找某一行或者某一列在矩阵中的位置
- Key wizard play strange learning - front desk and Intranet send background verification code
- 链表中的节点每k个一组翻转
- Embrace the safety concept of platform delivery
- Initial order of pointer (basic)
猜你喜欢

【无标题】

Assets, vulnerabilities, threats and events of the four elements of safe operation

Niu Ke swipes questions and clocks in
![leetcode:871. Minimum refueling times [Pat has done before + maximum stacking + greed]](/img/2c/8ec3926243fac8db9ed45d8053f3af.png)
leetcode:871. Minimum refueling times [Pat has done before + maximum stacking + greed]

有向图的强连通分量
![[AUTOSAR + IO Architecture]](/img/cf/9ea42b50bed298c0546764b63bd957.png)
[AUTOSAR + IO Architecture]

1696C. Fishingprince Plays With Array【思维题 + 中间状态 + 优化存储】

Understanding and distinguishing of some noun concepts in adjustment / filtering

【FH-GFSK】FH-GFSK信号分析与盲解调研究
![[overview of AUTOSAR four BSW]](/img/19/c2273bbedb7f8d859e5a3805ed5740.png)
[overview of AUTOSAR four BSW]
随机推荐
Reading and writing speed of Reza rz/g2l arm development board storage and network measurement
matlab 多普勒效应产生振动信号和处理
How wide does the dual inline for bread board need?
The difference between relational database and non relational database
Meibeer company is called "Manhattan Project", and its product name is related to the atomic bomb, which has caused dissatisfaction among Japanese netizens
Trois tâches principales: asynchrone, courrier et timing
删除有序链表中重复的元素-II
kivy教程之在 Kivy App 中使用 matplotlib 的示例
Key wizard play strange learning - front desk and Intranet send background verification code
Find a benchmark comrade in arms | a million level real-time data platform, which can be used for free for life
Daily topic: movement of haystack
Database SQL language 02 connection query
[shutter] image component (cached_network_image network image caching plug-in)
每日一题之干草堆的移动
Infrared thermography temperature detection system based on arm rk3568
Several cases of recursive processing organization
MySQL foundation 06 DDL
Draw love with go+ to express love to her beloved
递归处理组织的几种情况
ROS2之ESP32简单速度消息测试(极限频率)