Lets get started:
- We will be using the git command "git describe --tags --long" this commands shows us some useful info:
[LATEST_TAG_NAME]-[HOW FAR AM I FROM THAT TAG]-[COMMIT HASH] .
For example - if our last tag was "1.4.3" and I'm currently checked out 23 commits ahead, the output will be: 1.4.3-23-g3s4s3dsf - Another helpful thing we will be using is a trick to turn version name into version code, have a look at Jake Wharton post to see where we got inspired from.
- Open up your build.gradle and set the code:
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
android { | |
defaultConfig { | |
... | |
// Fetch the version according to git latest tag and "how far are we from last tag" | |
def longVersionName = "git -C ${rootDir} describe --tags --long".execute().text.trim() | |
def (fullVersionTag, versionBuild, gitSha) = longVersionName.tokenize('-') | |
def(versionMajor, versionMinor, versionPatch) = fullVersionTag.tokenize('.') | |
// Set the version name | |
versionName "$versionMajor.$versionMinor.$versionPatch($versionBuild)" | |
// Turn the version name into a version code | |
versionCode versionMajor.toInteger() * 100000 + | |
versionMinor.toInteger() * 10000 + | |
versionPatch.toInteger() * 1000 + | |
versionBuild.toInteger() | |
// Friendly print the version output to the Gradle console | |
printf("\n--------" + "VERSION DATA--------" + "\n" + "- CODE: " + versionCode + "\n" + | |
"- NAME: " + versionName + "\n----------------------------\n") | |
... | |
} | |
} |
No comments:
Post a Comment