Skip to content

Instantly share code, notes, and snippets.

Last active August 14, 2023 08:46
  • Star 35 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
Star You must be signed in to star a gist
What would you like to do?
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