Tag Archives: versionName

Gradle :: make your builds be part of your commits

Gradle, great product used in the new build system of the Android projects can be a real power tool to maintain your projects.

Why? First of all, one of the greatest challenge in the tests, debugs and release package is to keep an eye on to what version of the source code the crash corresponds. For the main part, this can be achieved by manually updating the code version and code name to be correct source code… If for simple projects this can be ok for short term ; in the long term or in most of “industrial” sized projects, this can be really a pain in the byte…

So what? First of all, in the projects, you should keep for each package, a commit, to make a backup of the source code and the information about what changes have been made in the package.

But now, how can you insert easily the information into the package information?

Here comes the following snippets to help you. The idea behing them are simple, the first one returns directly the current timestamp of the package build and the second one, the most important snippet here, simply takes the latest commit name of the current path and returns it.

def getVersionCode = {
  def code = Math.round(new Date().getTime()/1000).toInteger()
  return code

def getDefinedVersionName() {
  def stdout = new ByteArrayOutputStream()
  exec {
    commandLine 'git', 'log', '-n', '1', '--pretty=format:"%H"'
    standardOutput = stdout
  return stdout.toString().trim().replace("\"","")

It is now easy to add the two or only the git into your gradle build structure. It is recommended to add the function into the root build.gradle in order to propagate the snippets usability.

The final part is to call the required function, I prefer using the commit one since you have the most relevant information (you can concat the two output to make much more powerful results)

For instance you could have :

android {
  defaultConfig {
    versionName getDefinedVersionName()

And voila, it is now installed and every AndroidManifest.xml in the outputs build will be merged with the latest commit version.