Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Increment version number and update version name on your android builds automatically. Version name is generated by the last tag on your git commits.
apply from: 'versions.gradle'
android {
defaultConfig {
versionName VERSION_NAME
versionCode VERSION_CODE.toInteger()
}
buildTypes {
release {}
beta {}
}
//apply this at the end of the file
android.applicationVariants.all { variant ->
versions.apply(variant)
}
VERSION_NAME=1.0.0
VERSION_CODE=1
project.ext.versions = new HashMap<String, Object>()
project.ext.versions.apply = { variant ->
versions.applyVersionCode(variant)
versions.applyVersionName(variant)
}
// increments version code
project.ext.versions.applyVersionCode = { variant ->
def buildType = variant.buildType.name
def versionCode = VERSION_CODE.toInteger()
//increment only for 'release' or 'beta' builds
if('release'.equals(buildType) || 'beta'.equals(buildType)) {
versionCode += 1
ant.propertyfile(file: "../gradle.properties") {
entry(key: "VERSION_CODE", value: versionCode)
}
}
variant.mergedFlavor.versionCode = versionCode
}
// updates version name based on last git tag
project.ext.versions.applyVersionName = { variant ->
def lastGitTag = 'git describe --abbrev=0 --tags'.execute().text.trim()
//update version name if we have a new tag
if (!VERSION_NAME.equals(lastGitTag)) {
ant.propertyfile(file: "../gradle.properties") {
entry(key: "VERSION_NAME", value: lastGitTag)
}
}
variant.mergedFlavor.versionName = lastGitTag
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.