Auto increment version number on release build.
目标
希望每次CI build 后versionCode 会自动+1, build number 会自动+1
实现
在这里所有的版本信息通过外部文件version.properties得到。version.properties 放在app目录下,如果app有多个flavor就放在每个flavor对应的目录下下。
app
- src
- flavor1
- version.properties
- flavor2
- version.properties
- version.gradle
version.properties 文件
VERSION_MAJOR=1
VERSION_MINOR=0
VERSION_PATCH=0
VERSION_BUILD=0
VERSION_NAME=1.0.0.0
VERSION_CODE=10000
version.gradle
新建一个version.gradle文件,该文件放在App 根目录下
import java.util.regex.Matcher
import java.util.regex.Pattern
android {
def versionMajor = 1
def versionMinor = 0
def versionPatch = 0
def versionBuild = 0
def versionFileName = "version.properties";
def versionPropsFile
def releaseTaskName
//Initialize versionPropsFile name & releaseTaskName
Gradle gradle = getGradle()
println(gradle.getStartParameter().getTaskRequests().toString())
Pattern pattern = Pattern.compile("assemble(\\w+)(Release|Debug)");
Matcher matcher = pattern.matcher(gradle.getStartParameter().getTaskRequests().toString());
//Get current flavor name
if (matcher.find()) {
def flavor = matcher.group(1).toLowerCase();
versionPropsFile = file("src/${flavor}/${versionFileName}")
releaseTaskName = "assemble${matcher.group(1)}Release"
} else {
println "NO MATCH FOUND"
}
/*Setting default value for versionBuild which is the last incremented value stored in the file */
if (versionPropsFile != null && versionPropsFile.canRead()) {
def Properties versionProps = new Properties()
versionProps.load(new FileInputStream(versionPropsFile))
versionMajor = versionProps.getProperty('VERSION_MAJOR', '1').toInteger()
versionMinor = versionProps.getProperty('VERSION_MINOR', '0').toInteger()
versionPatch = versionProps.getProperty('VERSION_PATCH', '0').toInteger()
versionBuild = versionProps.getProperty('VERSION_BUILD', '0').toInteger()
project.ext.versionCode = versionMajor * 10000 + versionMinor * 1000 + versionPatch * 100 + versionBuild
project.ext.versionName = "${versionMajor}.${versionMinor}.${versionPatch}.${versionBuild}"
println '--------Building----------'
println 'PropsFile: ' + versionPropsFile.toString()
println 'versionMajor ' + versionMajor.toString()
println 'versionMinor ' + versionMinor.toString()
println 'versionPatch ' + versionPatch.toString()
println 'Version Code: ' + project.ext.versionCode.toString()
println 'Version Name: ' + project.ext.versionName
println '---------------------------'
} else {
throw new GradleException("Could not read version.properties!")
}
/*
* VersionCode : versionMajor * 10000 + versionMinor * 1000 + versionPatch * 100 + versionBuild
* VersionName : "${versionMajor}.${versionMinor}.${versionPatch}.${versionBuild}"
* versionBuild: Auto increment (+1) every Jenkins release build
* Reset version build to 0 if any of versionMajor, versionMinor or versionPatch changed.
*/
println 'releaseTaskName ' + releaseTaskName
//ci == true, 只有CI release build 时才做+1操作
if (ci) {
def runTasks = gradle.startParameter.taskNames
if (releaseTaskName in runTasks) {
if (versionPropsFile != null && versionPropsFile.canRead()) {
def Properties versionProps = new Properties()
versionProps.load(new FileInputStream(versionPropsFile))
versionBuild = versionBuild + 1
project.ext.versionCode =
versionMajor * 10000 + versionMinor * 1000 + versionPatch * 100 + versionBuild
project.ext.versionName = "${versionMajor}.${versionMinor}.${versionPatch}.${versionBuild}"
versionProps.setProperty('VERSION_MAJOR', versionMajor.toString())
versionProps.setProperty('VERSION_MINOR', versionMinor.toString())
versionProps.setProperty('VERSION_PATCH', versionPatch.toString())
versionProps.setProperty('VERSION_BUILD', versionBuild.toString())
versionProps.setProperty('VERSION_CODE', project.ext.versionCode.toString())
versionProps.setProperty('VERSION_NAME', project.ext.versionName)
versionProps.store(versionPropsFile.newWriter(), null)
println '--------Building----------'
println 'Version Code: ' + project.ext.versionCode.toString()
println 'Version Name: ' + project.ext.versionName
println '---------------------------'
} else {
throw new GradleException("Could not read version.properties!")
}
}
}
}
修改 root 的build.gradle
增加以下代码,用于判断是否是CI build
ext {
//'CI' environment variables is set in CI(eg. Jenkins) configuration page
ci = 'true'.equals(System.getenv('CI'))
}
修改 app 的build.gradle
增加一行 apply from: 'version.gradle'
apply plugin: 'com.android.application'
apply from: 'version.gradle'
结果
CI release build 后,版本号为
--------Building----------
Version Code: 10001
Version Name: 1.0.0.1
---------------------------
网友评论