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






边栏推荐
- R language ggplot2 visualization: use ggplot2 to display dataframe data that are all classified variables in the form of thermal diagram, and customize the legend color legend of factor
- Appuyez sur l'apprentissage de l'esprit de frappe - reconnaissance des coordonnées de fond multithreadées
- Matlab Doppler effect produces vibration signal and processing
- Win10 can't be installed in many ways Problems with NET3.5
- excel IF公式判断两列是否相同
- 关于Fibonacci数列
- Merge K sorted linked lists
- 全志A40i/T3如何通过SPI转CAN
- Usage of using clause in kingbases alter table
- Linear programming of mathematical modeling (including Matlab code)
猜你喜欢

Esp32 simple speed message test of ros2 (limit frequency)

Strongly connected components of digraph

leetcode 6103 — 从树中删除边的最小分数

数学建模之线性规划(含MATLAB代码)
![[shutter] image component (cached_network_image network image caching plug-in)](/img/cc/967ff62c7f82e1c6613b3d0f26bb3e.gif)
[shutter] image component (cached_network_image network image caching plug-in)

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

Daily topic: movement of haystack

How wide does the dual inline for bread board need?

每日一题之干草堆的移动

拥抱平台化交付的安全理念
随机推荐
1696C. Fishingprince Plays With Array【思维题 + 中间状态 + 优化存储】
按键精灵打怪学习-前台和内网发送后台验证码
Basic remote connection tool xshell
Appuyez sur l'apprentissage de l'esprit de frappe - reconnaissance des coordonnées de fond multithreadées
465. DFS backtracking of optimal bill balance
[shutter] image component (configure local GIF image resources | load placeholder with local resources)
这不平凡的两年,感谢我们一直在一起!
JDBC courses
[love crash] neglected details of gibaro
Meibeer company is called "Manhattan Project", and its product name is related to the atomic bomb, which has caused dissatisfaction among Japanese netizens
数据分析思维分析犯法和业务知识——分析方法(一)
基本远程连接工具Xshell
R language generalized linear model function GLM, (model fit and expression diagnostics), model adequacy evaluation method, use plot function and car package function
[AUTOSAR twelve mode management]
What is needed to develop a domestic arm intelligent edge computing gateway
【我的OpenGL学习进阶之旅】关于欧拉角、旋转顺序、旋转矩阵、四元数等知识的整理
Excel removes the data after the decimal point and rounds the number
Key wizard play strange learning - multithreaded background coordinate recognition
[overview of AUTOSAR four BSW]
excel IF公式判断两列是否相同