Skip to content

Instantly share code, notes, and snippets.

@SeanZoR
Last active November 9, 2020 01:27
Show Gist options
  • Save SeanZoR/85ba3976fec20a4189d1 to your computer and use it in GitHub Desktop.
Save SeanZoR/85ba3976fec20a4189d1 to your computer and use it in GitHub Desktop.
Automatic versioning and increment using Git tags and Gradle
android {
defaultConfig {
...
// Fetch the version according to git latest tag and "how far are we from last tag"
def longVersionName = "git -C ${rootDir} describe --tags --long".execute().text.trim()
def (fullVersionTag, versionBuild, gitSha) = longVersionName.tokenize('-')
def(versionMajor, versionMinor, versionPatch) = fullVersionTag.tokenize('.')
// Set the version name
versionName "$versionMajor.$versionMinor.$versionPatch($versionBuild)"
// Turn the version name into a version code
versionCode versionMajor.toInteger() * 100000 +
versionMinor.toInteger() * 10000 +
versionPatch.toInteger() * 1000 +
versionBuild.toInteger()
// Friendly print the version output to the Gradle console
printf("\n--------" + "VERSION DATA--------" + "\n" + "- CODE: " + versionCode + "\n" +
"- NAME: " + versionName + "\n----------------------------\n")
...
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment