2016-09-23 75 views
1

我正在尋找一種方式在建設它包括的Git分支名到我的android apk文件名。如何包括apk文件名的Git分支名稱自動

我想我的名字apk文件 「在構建它ProjectName- 混帳BRANCHNAME .apk文件,自動 」

例: 「爲MyTestProject-master.apk」

我搜索在線並閱讀gradle文檔,但無法找到如何將分支名稱包含到輸出文件名中的參考。

我知道如何使用gradle構造一個文件名,一般來說。我具體詢問有關git分支的參考。

+1

嘗試尋找[在這裏](http://stackoverflow.com/a/28250257/1790644),當然,這將需要一些修改。 –

+0

@MattClark我沒有在該網站上看到對git分支名稱的引用。我一般都知道如何使用gradle。 –

+0

Whelp,使用Gradle改變文件的名字在我看來就像是大部分的工作,現在想出如何得到分支名稱:DI只是指出了一個有用的資源,因爲我沒有完整的答案。 –

回答

3

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

也許模擬一個使用這兩個而不僅僅是鏈接的例子;) –

0

,因爲你知道如何使用搖籃構造一個文件名,

這裏是任務獲取當前的Git分支名稱到您的搖籃......

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

你可以連接它與您的文件名。

希望它可以幫助..