Skip to content

Instantly share code, notes, and snippets.

@bungabear
Forked from paramsen/build.groovy
Created April 14, 2021 05:22
Show Gist options
  • Save bungabear/c37e2dc5b1382c89cd5df3277d65a7cd to your computer and use it in GitHub Desktop.
Save bungabear/c37e2dc5b1382c89cd5df3277d65a7cd to your computer and use it in GitHub Desktop.
Generate Android version code from git count in Gradle (build.gradle)
// Generate a minor version code from git commit count (for prod builds)
static def generateVersionCode() {
def result = "git rev-list HEAD --count".execute().text.trim() //unix
if(result.empty) result = "PowerShell -Command git rev-list HEAD --count".execute().text.trim() //windows
if(result.empty) throw new RuntimeException("Could not generate versioncode on this platform? Cmd output: ${result.text}")
return result.toInteger()
}
def majorVersion = 1
android {
defaultConfig {
versionCode generateVersionCode()
versionName "${majorVersion}.${generateVersionCode()}"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment