Created
February 15, 2018 18:05
-
-
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
VERSION_NAME=1.0.0 | |
VERSION_CODE=1 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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