Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Automatic versioning of Android build using git describe with Gradle

I have searched extensively, but likely due to the newness of Android Studio and Gradle. I haven't found any description of how to do this. I want to do basically exactly what is described in this post, but with Android Studio, Gradle and Windows rather than Eclipse and Linux.

like image 338
Mike Yount Avatar asked Jun 13 '13 21:06

Mike Yount


1 Answers

Put the following in your build.gradle file for the project. There's no need to modify the manifest directly: Google provided the necessary hooks into their configuration.

def getVersionCode = { ->     try {         def code = new ByteArrayOutputStream()         exec {             commandLine 'git', 'tag', '--list'             standardOutput = code         }         return code.toString().split("\n").size()     }     catch (ignored) {         return -1;     } }  def getVersionName = { ->     try {         def stdout = new ByteArrayOutputStream()         exec {             commandLine 'git', 'describe', '--tags', '--dirty'             standardOutput = stdout         }         return stdout.toString().trim()     }     catch (ignored) {         return null;     } } android {     defaultConfig {         versionCode getVersionCode()         versionName getVersionName()     } } 

Note that if git is not installed on the machine, or there is some other error getting the version name/code, it will default to what is in your android manifest.

like image 69
moveaway00 Avatar answered Sep 28 '22 07:09

moveaway00