当前位置:网站首页>[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+学习笔记》
边栏推荐
- (C语言)数据的存储
- [shutter] image component (configure local GIF image resources | load placeholder with local resources)
- What is needed to develop a domestic arm intelligent edge computing gateway
- On Fibonacci sequence
- 按键精灵打怪学习-自动寻路回打怪点
- leetcode:701. Insertion in binary search tree [BST insertion]
- 【FH-GFSK】FH-GFSK信号分析与盲解调研究
- Kivy教程大全之如何在 Kivy 中创建下拉列表
- 1696C. Fishingprince plays with array [thinking questions + intermediate state + optimized storage]
- Canvas drawing -- bingdd
猜你喜欢
First hand evaluation of Reza electronics rz/g2l development board
Strongly connected components of digraph
[AUTOSAR nine c/s principle Architecture]
[shutter] image component (cached_network_image network image caching plug-in)
信息熵的基础
Daily topic: movement of haystack
异步、邮件、定时三大任务
leetcode:701. Insertion in binary search tree [BST insertion]
leetcode 2097 — 合法重新排列数对
Draw love with go+ to express love to her beloved
随机推荐
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
Lu Zhe, chief scientist of Shiping information: building data and personnel centered security capabilities
[love crash] neglected details of gibaro
Key wizard play strange learning - front desk and Intranet send background verification code
每日一题之干草堆的移动
What is needed to develop a domestic arm intelligent edge computing gateway
Embrace the safety concept of platform delivery
信息熵的基础
RISA rz/g2l processor introduction | frame diagram | power consumption | schematic diagram and hardware design guide
Linear programming of mathematical modeling (including Matlab code)
全志A40i/T3如何通过SPI转CAN
Esp32 simple speed message test of ros2 (limit frequency)
Database SQL language 01 where condition
Cut point of undirected graph
按键精灵打怪学习-回城买药加血
[AUTOSAR + IO Architecture]
按键精灵打怪学习-前台和内网发送后台验证码
电话网络问题
[flutter] icons component (load the built-in icon of flutter | display the material design icon completely)
Basic concept and implementation of overcoming hash