一、背景
文章 Android项目中独立Git项目分库后的编译调试时Gradle的配置中介绍了Android项目中独立Git库源码依赖时的本地处理方案。但在实际的项目开发中,由于团队多人协作以及可能存在的多独立Git库等情况,实际上使用还是有所不便。如采用Google Repo或者submodule方式,实际上也只是基于Git命令基础上的脚本封装,只是解决多库的Git使用问题,而不能解决实际项目场景中的多独立Git库的统一AAR发布、源码依赖与AAR依赖的一键切换、甚至构建过程中的定制与扩展等。
另一方面,在源码依赖领域,Gradle在几年前就开始进行了尝试,并在官网博文中发布了文章: Introducing source dependencies,只是到目前为止,在关键的authentication
问题上还没有给出实质性的方案,还不能被正式使用。
具体参见:
github.com/gradle/grad…
同时,对于独立Git库存在经常修改的情况,这种方案也必须依赖于经常往远程push commit
方式。现实中,对于独立Git库的修改,可以等主工程可以直接有效果,基本稳定后,确认没问题了再发布AAR,然后再一键切换到二进制AAR依赖。如此才能满足复杂项目场景中的现实需要。
当然,本文重点主要还是围绕源码依赖进行,相当于现实项目场景整体方案其中的关键一个组成部分。
二、技术方案
文章 Android项目中独立Git项目分库后的编译调试时Gradle的配置,其实已经提供了基本的源码依赖思路,只是在具体实现上,没有考虑到团队多人协作以及可能存在的多独立Git库等情况,手动逐个配置往往不便。如此,可以在其基础上进行一次升级,针对多独立Git库,通过Gralde Task和开关配置的方式,尽可能的自动化其中的手动操作过程。
主要过程简单示例如下,项目实际使用中可以抽取成Gradle插件:
1,gradle.properties
中配置源码依赖开关:
sourceDependence = false
2, settings.gradle
中include
独立Git库本地源码项目
if(sourceDependence.toBoolean()) {
include ':components:uicomponent'
project(':components:uicomponent').projectDir = new File(getRootDir().getParent() + '/components/uicomponent')
}
!!特别注意:虽然配置中写的是true或false,但xx.gradle中获取到此配置值实际上还是对应的字符串,需要调用.toBoolean()强转成对应的布尔类型,否则没有效果。
3,项目根目录build.gradle
定义与独立Git库交互脚本,封装成task
:
buildscript {
apply from: './buildConfig.gradle'
repositories {
google()
jcenter()
....
}
dependencies {
....
classpath 'org.ajoberstar.grgit:grgit-core:3.0.0'
classpath 'org.ajoberstar.grgit:grgit-gradle:3.0.0'
}
}
task sourceDependenceLib {
doLast {
doClone('components', 'git@git.xxx.com:xxx/android/xxx/components.git', 'dev')
}
}
import org.ajoberstar.grgit.Grgit
void doClone(String dirName, String remote, String branch) {
File dir = new File(getRootDir().getParent() + "/" + dirName)
def git
if(!dir.exists()) {
git = Grgit.clone(dir: dir, uri: remote, refToCheckout: branch)
}
git = Grgit.open(dir: dir)
git.checkout(branch: branch)
git.pull(rebase: false)
}
4,主工程依赖中添加上源码依赖切换逻辑:
ext {
dependencies = [
....
uicomponent: "com.shopee.ui:uicomponent:$uicomponent_version"
]
}
if(sourceDependence.toBoolean()) {
dependencies.uicomponent = project(':components:uicomponent')
}
执行sourceDependenceLib task
,可以直接git load下最新的指定分支源码,打开sourceDependence
开关,即可直接切换从二进制依赖切换到源码依赖。
最终效果如下:
源码依赖情况下,可以直接修改独立Git库代码,直接源码编译。阶段性稳定后发布AAR。
end~