我进行了广泛的搜索,但可能是由于 Android Studio 和 Gradle 的新颖性。我还没有找到有关如何执行此操作的任何描述。我想基本上完全按照 this post 中的描述进行操作,但使用 Android Studio、Gradle 和 Windows 而不是 Eclipse 和 Linux。
最佳答案
将以下内容放入项目的 build.gradle 文件中。无需直接修改 list :Google 在其配置中提供了必要的 Hook 。
def getVersionCode = { ->
try {
def code = new ByteArrayOutputStream()
exec {
commandLine 'git', 'tag', '--list'
standardOutput = code
}
return code.toString().split("\n").size()
}
catch (ignored) {
return -1;
}
}
def getVersionName = { ->
try {
def stdout = new ByteArrayOutputStream()
exec {
commandLine 'git', 'describe', '--tags', '--dirty'
standardOutput = stdout
}
return stdout.toString().trim()
}
catch (ignored) {
return null;
}
}
android {
defaultConfig {
versionCode getVersionCode()
versionName getVersionName()
}
}
请注意,如果机器上没有安装 git,或者在获取版本名称/代码时出现其他错误,它将默认为您的 android manifest 中的内容。
关于android - 使用带有 Gradle 的 git describe 对 Android 构建进行自动版本控制,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17097263/