Last active
January 29, 2023 03:01
-
-
Save michaelbel/d6a10d13e8e3e842e56965da0e8005dc to your computer and use it in GitHub Desktop.
Git commits count as version code (Groovy & Kotlin implementation)
This file contains 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
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() | |
} | |
} |
This file contains 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
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