Skip to content

Instantly share code, notes, and snippets.

@michaelbel
Last active January 29, 2023 03:01
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save michaelbel/d6a10d13e8e3e842e56965da0e8005dc to your computer and use it in GitHub Desktop.
Save michaelbel/d6a10d13e8e3e842e56965da0e8005dc to your computer and use it in GitHub Desktop.
Git commits count as version code (Groovy & Kotlin implementation)
static def gitVersion() {
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()
}
android {
defaultConfig {
versionCode gitVersion()
}
}
val gitVersion: Int by lazy {
val stdout = ByteArrayOutputStream()
rootProject.exec {
commandLine("git", "rev-list", "--count", "HEAD")
standardOutput = stdout
}
stdout.toString().trim().toInt()
}
android {
defaultConfig {
versionCode = gitVersion
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment