Skip to content

Instantly share code, notes, and snippets.

@ArthurSav
Created February 15, 2018 18:05
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save ArthurSav/cd57202ae70c152e8b01f58e86ba9fcf to your computer and use it in GitHub Desktop.
Save ArthurSav/cd57202ae70c152e8b01f58e86ba9fcf to your computer and use it in GitHub Desktop.
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