Skip to content

Instantly share code, notes, and snippets.

@piffall
Last active August 29, 2015 14:13
Show Gist options
  • Save piffall/7dec5faf535ecb2691da to your computer and use it in GitHub Desktop.
Save piffall/7dec5faf535ecb2691da to your computer and use it in GitHub Desktop.
Get version from git tagging
// Copy and paste the content of this file in app/build.gradle
// Now you are able to use it under defaultConfig, for example:
// versionCode getRevCount()
// versionName getTag() + '-' + getRevHash()
System.println( "App: " + "com.example.app" + " v" + getTag() + '-' + getRevHash() + "(" + getRevCount() + ")" )
def getTag() {
def cmd1 = "git rev-list --tags --max-count=1"
def proc1 = cmd1.execute()
def cmd2 = "git describe --tags " + proc1.text.trim()
def proc2 = cmd2.execute()
def revision = proc2.text.trim()
return revision
}
def getRevCount() {
def cmd = "git rev-list --all --count"
def proc = cmd.execute()
def revision = Integer.valueOf(proc.text.trim())
return Integer.valueOf(revision)
}
def getRevHash() {
def cmd = "git rev-parse --short HEAD"
def proc = cmd.execute()
def revision = proc.text.trim()
return revision
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment