定义版本号
System.setProperty("verName", "1.0.0")
ext.versionCofig = [
"verCode" : getVersionCode(),
"buildCount": getBuildCount(),
]
android {
compileSdk 32
defaultConfig {
applicationId "xxx"
minSdk 21
targetSdk 32
versionName System.getProperty("verName")
versionCode Integer.parseInt(versionCofig.verCode + versionCofig.buildCount)
}
...
根据verName生成verCode
def getVersionCode() {
StringBuffer sb = new StringBuffer()
for (item in System.getProperty("verName").split("\\.")) {
if (item.length() == 1) {
//1位补0
sb.append('0')
}
sb.append(item)
}
//转int去掉0开头
return Integer.parseInt(sb.toString()).toString()
}
读取build自增开关
boolean getBuildLogicEnable() {
Properties localProperties = new Properties()
File file = project.rootProject.file('local.properties')
if (!file.exists()) {
file.createNewFile()
}
localProperties.load(file.newReader("UTF-8"))
if (localProperties.getProperty("enable_build_logic") == null) {
//未定义 默认开启
return true
}
//配置enable_build_logic=0为关闭
return localProperties.getProperty("enable_build_logic") as Integer as boolean
}
获取打包次数 alpha\release包通过git检查 自增
def getBuildCount() {
//读取文件
Properties localProperties = new Properties()
File file = project.rootProject.file('version.properties')
if (!file.exists()) {
file.createNewFile()
}
localProperties.load(file.newReader("UTF-8"))
//当前记录的verCode
def locVerCode = "0"
if (localProperties.getProperty("version_code") != null) {
locVerCode = localProperties.getProperty("version_code")
}
//当前记录的buildCount
def locBuildCount = "00"
if (localProperties.getProperty("build_count") != null) {
locBuildCount = localProperties.getProperty("build_count")
}
//逻辑启用开关
if (getBuildLogicEnable()) {
//仅在打生产包的时候执行自增
def runTasks = gradle.startParameter.taskNames
if ("assembleRelease" in runTasks) {
//获取最新的commit
def pullResult = ("git pull").execute().text.toString()
print pullResult
if (!pullResult.contains("Already up to date.")) {
//冲突等的情况 中断任务
return locBuildCount
}
def curCommitType = ("git log --no-merges --pretty=format:%s HEAD -1").execute().text.toString()
if (curCommitType.startsWith("generator")) {
return locBuildCount
}
//非generator提交 修改版本号
if (Integer.parseInt(getVersionCode()) > Integer.parseInt(locVerCode)) {
//verCode变化 即版本变化 buildCount归零
locVerCode = getVersionCode()
locBuildCount = "00"
}
//自增1
def curBuildCount = ++Integer.parseInt(locBuildCount)
//个位数补零 转string
locBuildCount = curBuildCount > 9 ? curBuildCount.toString() : "0" + curBuildCount
try {
//存储自增后的打包次数及verCode记录
localProperties.setProperty("version_code", locVerCode)
localProperties.setProperty("build_count", locBuildCount)
//写入
FileOutputStream fos = new FileOutputStream(rootDir.getAbsolutePath() + "/version.properties", false)
OutputStreamWriter opw = new OutputStreamWriter(fos, "utf-8")
localProperties.store(opw, "")
fos.close()
//提交
print(("git add ${project.rootProject.file('version.properties')}").execute().text.toString())
print(("git commit ${project.rootProject.file('version.properties')} -m \"" +
"generator:v${System.getProperty("verName")}-build$locBuildCount-${locVerCode + locBuildCount}"
+ "\"").execute().text.toString())
print(("git push").execute().text.toString())
print(("git pull").execute().text.toString())
return locBuildCount
} catch (Exception e) {
e.printStackTrace()
}
}
}
return locBuildCount
}
根据当前分支与master提交次数对比差值
static def getCurBranchCommitCount() {
int devCommitCount = Integer.parseInt((("git rev-list --max-parents=1 --count HEAD").execute().text.toString().replace(" ", "").replace("\n", "")))
int masterCommitCount = Integer.parseInt((("git rev-list --max-parents=1 --count master").execute().text.toString().replace(" ", "").replace("\n", "")))
def currentBranchName = "git rev-parse --abbrev-ref HEAD".execute().text.toString().trim()
if (currentBranchName == "master")
return masterCommitCount.toString()
return (devCommitCount - masterCommitCount).toString()
}