7

I'm looking for a way to include the git branch name into my android apk file name upon building it.

I would like to name my apk file "ProjectName-git branchname.apk, automatically upon building it. "

Example: "MyTestProject-master.apk"

I've searched online and read the gradle docs but can't find a reference to how to include the branch name into the output file name.

I know how to use gradle to construct a file name, generally speaking. I'm specifically asking about the git branch reference.

Pierre Rymiortz
  • 1,127
  • 3
  • 15
  • 31
  • 1
    Try looking [over here](http://stackoverflow.com/a/28250257/1790644), of course, it would require some modifications. – Matt Clark Sep 23 '16 at 07:02
  • @MattClark I don't see a reference to git branch name on that site. I know generaly how to use gradle. – Pierre Rymiortz Sep 23 '16 at 07:08
  • Whelp, using Gradle to change the name of the file seems to me like a bulk of the work, now figure out how to get the branch name :D I was just pointing out a helpful resources because I didn't have the full answer.. – Matt Clark Sep 23 '16 at 07:09
  • @MattClark ic, understood, thanks for help – Pierre Rymiortz Sep 23 '16 at 07:12
  • Did you check this link? May be this could help. Its about auto-versioning a build. http://stackoverflow.com/questions/21414399/android-gradle-dynamically-change-versionname-at-build-time – Muhammed Neswine Sep 23 '16 at 07:23

2 Answers2

6

since you know how to use Gradle to construct a file name,

Here is the task to fetch current git branch name into your Gradle...

def getBranchName = { ->
    try {
        println "Task Getting Branch Name.."

        def stdout = new ByteArrayOutputStream()
        exec {
            commandLine 'git', 'rev-parse', '--abbrev-ref', 'HEAD'
            standardOutput = stdout
        }

        println "Git Current Branch = " + stdout.toString()

        return stdout.toString()
    }
    catch (Exception e) {
        println "Exception = " + e.getMessage()
        return null;
    }
}

You can concatenate it with your file name.

Hope it helps..

iMDroid
  • 2,108
  • 1
  • 16
  • 29
5

This stackoverflow post shows how to get git branch name from command line.
Compose them to get what you need

Sharing the code to do this:
Git executable must be on your system PATH.

def changeApkName = { variant ->
    variant.outputs.each { output ->
        def apk = output.outputFile;
        def newName = androidApplicationName;
        def branch = getGitRevParseInfo("--abbrev-ref");
        if (variant.buildType.name == "release") {
            newName += "-v" + variant.mergedFlavor.versionName + "-" + branch + "-release.apk";
        } else {
            newName += "-v" + variant.mergedFlavor.versionName + "-" + branch + ".apk";
        }

        if (!output.zipAlign) {
            newName = newName.replace(".apk", "-unaligned.apk");
        }
        output.outputFile = new File(apk.parentFile, newName);

        println 'INFO: Set outputFile to ' + output.outputFile + " for [" + output.name + "]"
    }
}

def getGitRevParseInfo(what) {
    def cmd = "git rev-parse " + what + " HEAD"
    def proc = cmd.execute()
    proc.text.trim()
}

android {
        applicationVariants.all { variant ->
            variant.outputs.each { output ->
                changeApkName(variant)
            }
        }
    }
}
Graham
  • 7,431
  • 18
  • 59
  • 84
R. Zagórski
  • 20,020
  • 5
  • 65
  • 90