我需要编写一个 gradle 脚本来在每次提交时自动对我的应用程序进行版本控制。我还需要在测试人员的应用程序中包含提交哈希作为引用。
我很困惑自动版本控制通常是如何工作的。有人能解释一下自动版本化过程吗?
最佳答案
我遇到了类似的问题,但不想修改 versionName 以包含 git 哈希。我们希望将其保留为类似于 1.2.2,但仍有可能在 UI 中显示 git 哈希。
我修改了 the other answer here 的代码使用 buildConfigField 任务生成可以在 Java 代码中引用的 BuildConfig.GitHash 值。
在模块的 build.gradle 文件的 android
部分上方添加:
def getGitHash = { ->
def stdout = new ByteArrayOutputStream()
exec {
commandLine 'git', 'rev-parse', '--short', 'HEAD'
standardOutput = stdout
}
return stdout.toString().trim()
}
然后将以下行添加到build.gradle的android
部分的defaultConfig
部分,即versionName
下面:
buildConfigField "String", "GitHash", "\"${getGitHash()}\""
这会在自动生成的 BuildConfig.java 文件中生成以下行:
// Fields from default config.
public static final String GitHash = "e61af97";
现在您可以使用 BuildConfig.GitHash
在您的 Java 代码中获取 git 哈希。
关于android - Gradle 脚本自动版本并在 Android 中包含提交哈希,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28498688/