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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With