GitHashses For Android Freaks

The best practice is to store a git-hash in the BuildConfig field so that one can track the commit when using such analytical services as CrashAnalytics, etc. The problem arises as the MS Windows version of the git executables do not fully implement git-hashses as far as getting the tag name properly.

Bus as with Android Development and really anything one can hack one’s way to a solution.

This will come in handy with all those beginning Android developers who all bought cheap MS Windows laptops, yes even I bought one as they are very cheap despite only lasting two-hours on a battery charge.


First, we use a little tools information. Our Android SDK and IDE tool chain uses the Eclipse JGit library as the interface to the git. So how do we use that in our gradle build? Well, someone had the same idea and did a java wrapper for JGIt called GrGit:


To get every thing on the Gradle build script classpath you first load the wrapper just like any gradle plugin:

classpath ‘org.ajoberstar:grgit:1.4.1’

at the top of your root gradle build script than put:

import org.ajoberstar.grgit.*

Okay now the last part in the root gradle build script in your ext block:

than in the app module build script under the release buildTypes you would have:

first at head define a def


than under buildTypes in release


I have just shown one use the GrGit library so that you can use git-hashes in the gradle builds in a cross-platform way. That is not the only use as you may have a requirement in your project to use the git things to construct the versioning scheme of the application.