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






边栏推荐
- 电话网络问题
- Basic concept and implementation of overcoming hash
- Key wizard play strange learning - multithreaded background coordinate recognition
- Excel removes the data after the decimal point and rounds the number
- 【系统分析师之路】第五章 复盘软件工程(开发模型开发方法)
- 1038 Recover the Smallest Number
- 基本远程连接工具Xshell
- [overview of AUTOSAR four BSW]
- [untitled]
- [C language] branch and loop statements (Part 1)
猜你喜欢

Excel calculates the difference between time and date and converts it into minutes

Machine learning terminology

Merge K sorted linked lists

Find a benchmark comrade in arms | a million level real-time data platform, which can be used for free for life

数学建模之线性规划(含MATLAB代码)

The arm core board / development board of Feiling equipped with Ti am62x made its debut in embedded world 2022

异步、郵件、定時三大任務

正确甄别API、REST API、RESTful API和Web Service之间的异同

【FH-GFSK】FH-GFSK信号分析与盲解调研究

Lu Zhe, chief scientist of Shiping information: building data and personnel centered security capabilities
随机推荐
【FPGA教程案例5】基于vivado核的ROM设计与实现
按键精灵打怪学习-自动寻路回打怪点
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
MySQL foundation 07-dcl
产业互联网的产业范畴足够大 消费互联网时代仅是一个局限在互联网行业的存在
按鍵精靈打怪學習-多線程後臺坐標識別
删除有序链表中重复的元素-II
How to convert Quanzhi a40i/t3 to can through SPI
[flutter] icons component (fluttericon Download Icon | customize SVG icon to generate TTF font file | use the downloaded TTF icon file)
寻找标杆战友 | 百万级实时数据平台,终身免费使用
Infrared thermography temperature detection system based on arm rk3568
[introduction to AUTOSAR seven tool chain]
【C语言】分支和循环语句(上)
Merge K sorted linked lists
【FH-GFSK】FH-GFSK信号分析与盲解调研究
详解RDD基本概念、RDD五大属性
The R language uses the ctree function in the party package to build conditional inference decision trees, uses the plot function to visualize the trained conditional inference decision tree, and the
数学建模之线性规划(含MATLAB代码)
excel IF公式判断两列是否相同
SwiftUI 组件大全之使用 SceneKit 和 SwiftUI 构建交互式 3D 饼图(教程含源码)