Automatic APK versioning with git and gradle

Until recently, all my Android mobile games were built using Eclipse. As I made a new game recently, I found out that now everything is made for Android Studio and Gradle. I didn’t want to change my build system at first but recently found two reasons to make the change:

  • I want to use recent APIs (Android TV, AdMob) and all the docs are mostly for Android Studio
  • I find Android Studio much better (and lighter) than Eclipse

I’m using cocos2d-x and they are still using Eclipse but with the help of some resources available online, I could have a proper Gradle build. I didn’t know Gradle and to learn a bit about it, I decided to rename my APK directly in Gradle instead of using external scripts.

When building, I like to create an APK with a version number. I’m therefore using the git commit counts as a version number. For example, the latest APK of Thermometers Puzzles is called swg26-release-943.apk.
* swg26 is the name of the project (basically, it’s my 26th game, but not all have been released 😉 ).
* it’s either a release (signed) or debug version
* it’s version 943 (943 git commits were made)

To do that automatically, I took inspiration from this script from Google and came up with this:

In this script, I also compute the git commit id (gitSha) just as an example, but I prefer to use only the git commit count for readability. I save my APK on a server in a directory with the version name, it allows me to have builds from all platforms (iOS, Amazon, Windows Phone…) on the same location.

To use it, I include it in my main build.gradle using:
project.ext.applicationName = "swg26"
apply from: "../apk-naming.gradle"

Feel free to use and share this code! 🙂