Put the branch name near the application version name

Paul Stanescu
2 min readMar 21, 2019

--

Problem

Most of the times, besides the production flavor, you also have at least one more flavor for development and testing. In my case, I have 3 flavors, one for development, called “dev”, one for acceptance testing, called “acceptance”, and, of course, the production one, called “production”.

In order to have a better track of the testing process on different features (each on a separate branch), there was a request from the testing team to know on what branch the build was made.

The simplest way to do that is to have the branch name near version name. But how this can be done?

Manually concatenating the branch name to version name is an option but, do you really want to do this manually? Will you remember to exclude this change before release?

Solution

In order to achieve the goal of having an automated solution we have to follow 2 steps:

  • get current branch name dynamic
  • add the branch name together with the version name

This work can be done on the app gradle file.

def getCurrentBranch() {
def branch = ""
def proc = "git rev-parse --abbrev-ref HEAD".execute()
proc.in.eachLine { line -> branch = line }
proc.err.eachLine { line -> println line }
proc.waitFor()
branch
}

By putting this code on the gradle file you will be able to get the branch name.

The next step is to add the following lines within the android statement:

android.applicationVariants.all { variant ->
variant.outputs.each { output ->
if ("<appName>Acceptance" == "${output.apkData.fullName}") {
output.versionNameOverride = "${variant.versionName}-" + getCurrentBranch()
}
}
}

Remember that we want to have a different version name only for the acceptance build due to the fact I added the if statement within the above code.

Just code it!

--

--