Skip to content

Instantly share code, notes, and snippets.

@friederbluemle
Created January 17, 2020 21:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save friederbluemle/3e07a0a38efd62ecf6801e663114e620 to your computer and use it in GitHub Desktop.
Save friederbluemle/3e07a0a38efd62ecf6801e663114e620 to your computer and use it in GitHub Desktop.
Simple minimal Android Gradle <-> Git tag version mapping
android {
// ...
defaultConfig {
def gitDescribe = "git -C ${rootDir} describe --tags --long --dirty".execute().text.trim()
def (tag, commitsString, commitSha, dirty) = gitDescribe ? gitDescribe.tokenize('-') : []
def (tagMajor, tagMinor, tagPatch) = tag ? tag.tokenize('.') : []
def commits = commitsString ? commitsString.toInteger() : 0
def tmpMajor = tagMajor ? tagMajor.replaceAll('^v', '') : ''
def major = (tmpMajor && tmpMajor.isNumber()) ? tmpMajor.toInteger() : 0
def minor = (tagMinor && tagMinor.isNumber()) ? tagMinor.toInteger() : 0
def patch = (tagPatch && tagPatch.isNumber()) ? tagPatch.toInteger() : 1
def suffix = !gitDescribe || !tag || commits > 0 || (dirty == 'dirty') ? 'SNAPSHOT' : ''
printf("\n" +
"--------VERSION DATA--------" + "\n" +
"- gitDescribe: " + gitDescribe + "\n" +
"- tag: " + tag + "\n" +
"- commits: " + commits + "\n" +
"- commitSha: " + commitSha + "\n" +
"- dirty: " + (dirty == 'dirty') + "\n" +
"- suffix: " + suffix + "\n" +
"----------------------------" + "\n" +
"- tagMajor: " + tagMajor + "\n" +
"- tagMinor: " + tagMinor + "\n" +
"- tagPatch: " + tagPatch + "\n" +
"----------------------------" + "\n" +
"- major: " + major + "\n" +
"- minor: " + minor + "\n" +
"- patch: " + patch + "\n" +
"----------------------------" + "\n" +
"")
versionCode major * 10000 + minor * 100 + patch
versionName "$major.$minor.$patch" + (suffix ? "-$suffix" : '')
printf("\n" +
"--------FINAL DATA--------" + "\n" +
"- versionCode: " + versionCode + "\n" +
"- versionName: " + versionName + "\n" +
"----------------------------" + "\n" +
"")
// ...
}
// ...
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment