Saturday, September 6, 2014

Automatic versioning and increment using Git tags and Gradle

How do you set versions for you Android app? There are many ways at it. At the manifest, at the gradle in a "def", in a file... But, if you are looking for a cleaner way where you don't have to actually change a file, and it will continuously increment after each commit - here it is.

Lets get started:
  1. We will be using the git command "git describe --tags --long" this commands shows us some useful info:
    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
  2. 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.
  3. Open up your build.gradle and set the code: 
  4. Assumption - this script assumes that your tags don't have a prefix of some kind.