Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How include git branch name in apk file name automatically

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.

like image 621
Pierre Rymiortz Avatar asked Feb 07 '23 01:02

Pierre Rymiortz


1 Answers

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..

like image 79
iMDroid Avatar answered Feb 08 '23 13:02

iMDroid