用于自动版本化的Gradle脚本,并在Android中包含提交哈希 [英] Gradle script to autoversion and include the commit hash in Android
问题描述
我需要编写gradle脚本以在每次提交时自动对应用程序进行版本控制.我还需要将提交哈希作为参考添加到测试人员的应用程序中.
I need to write a gradle script to auto version my application on every commit. I need to also include the commit hash as a reference in the application for testers.
我对自动版本控制通常如何工作感到困惑.有人可以解释自动版本控制的过程吗?
I am confused how auto versioning usually work. Can someone explain the autoversioning process?
推荐答案
我遇到了类似的问题,但是不想修改versionName以包含git哈希.我们希望将其保持为1.2.2,但是仍然可以在UI中显示git hash.
I encountered a similar problem, but did not want to modify the versionName to include the git hash. We wanted to keep that as something like 1.2.2, but still have the possibility of displaying the git hash in the UI.
我修改了此处的其他答案中的代码,以使用buildConfigField任务生成一个BuildConfig.GitHash值,该值可以在Java代码中被引用.
I modified the code from the other answer here to use the buildConfigField task to generate a BuildConfig.GitHash value that can be referenced in the Java code.
将其添加到模块的build.gradle文件的android
部分上方:
Add this above the android
section of your module's build.gradle file:
def getGitHash = { ->
def stdout = new ByteArrayOutputStream()
exec {
commandLine 'git', 'rev-parse', '--short', 'HEAD'
standardOutput = stdout
}
return stdout.toString().trim()
}
然后将以下行添加到build.gradle的android
部分的defaultConfig
部分,即versionName
以下:
Then add the following line to the defaultConfig
section of the android
section of the build.gradle, i.e. below versionName
:
buildConfigField "String", "GitHash", "\"${getGitHash()}\""
这将在自动生成的BuildConfig.java文件中生成以下行:
This generates the following line in the auto-generated BuildConfig.java file:
// Fields from default config.
public static final String GitHash = "e61af97";
现在,您可以使用BuildConfig.GitHash
在Java代码中获取git哈希.
Now you can get the git hash in your Java code with BuildConfig.GitHash
.
这篇关于用于自动版本化的Gradle脚本,并在Android中包含提交哈希的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!