【问题标题】:How include git branch name in apk file name automatically如何在 apk 文件名中自动包含 git 分支名称
【发布时间】:2016-09-23 07:00:03
【问题描述】:

我正在寻找一种在构建时将 git 分支名称 包含到我的 android apk 文件名中的方法。

我想在构建时自动将我的 apk 文件命名为“ProjectName-git branchname.apk”。 "

示例:“MyTestProject-master.apk”

我已经在线搜索并阅读了 gradle 文档,但找不到有关如何将分支名称包含到输出文件名中的参考。

一般来说,我知道如何使用 gradle 来构造文件名。我特意询问了 git 分支参考。

【问题讨论】:

  • 试试看over here,当然,这需要一些修改。
  • @MattClark 我在该站点上没有看到对 git 分支名称的引用。我一般都知道如何使用 gradle。
  • Whelp,在我看来,使用 Gradle 更改文件名是一项繁重的工作,现在弄清楚如何获取分支名称:DI 只是指出了一个有用的资源,因为我没有'没有完整的答案..
  • @MattClark ic,明白了,谢谢帮助
  • 你检查了这个链接吗?可能这会有所帮助。它关于自动版本化构建。 stackoverflow.com/questions/21414399/…>

标签: android git gradle


【解决方案1】:

This stackoverflow post 展示了如何从命令行获取 git 分支名称。
组合它们以获得您需要的东西

分享代码来做到这一点:
Git 可执行文件必须在您的系统路径中。

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)
            }
        }
    }
}

【讨论】:

  • 也许可以使用这两个而不只是链接来模拟一个示例;)
【解决方案2】:

既然你知道如何使用 Gradle 来构造文件名,

这是获取当前 git 分支名称到 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;
    }
}

您可以将它与您的文件名连接起来。

希望对你有帮助..

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-10-25
    • 2013-10-29
    • 2018-07-17
    • 2012-07-13
    • 1970-01-01
    • 2010-09-06
    • 2022-08-02
    • 2018-01-25
    相关资源
    最近更新 更多