Pie*_*rtz 7 git android gradle
我正在寻找一种方法,在构建时将git 分支名称包含到我的 android apk 文件名中。
我想将我的 apk 文件命名为“ProjectName- gitbranchname.apk,在构建时自动命名。”
示例:“MyTestProject-master.apk”
我已经在线搜索并阅读了 gradle 文档,但找不到有关如何将分支名称包含到输出文件名中的参考。
一般来说,我知道如何使用 gradle 来构造文件名。我特别询问 git 分支参考。
既然您知道如何使用 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;
}
}
Run Code Online (Sandbox Code Playgroud)
您可以将其与您的文件名连接起来。
希望能帮助到你..
这篇 stackoverflow 文章展示了如何从命令行获取 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)
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
2992 次 |
最近记录: |