我进行了广泛的搜索,但可能是由于 Android Studio 和 Gradle 的新颖性。我还没有找到任何关于如何执行此操作的描述。我想做基本上完全按照中描述的这个帖子 https://stackoverflow.com/questions/14895123/auto-version-numbering-your-android-app-using-git-and-eclipse-on-linux,但使用 Android Studio、Gradle 和 Windows,而不是 Eclipse 和 Linux。
将以下内容放入项目的 build.gradle 文件中。无需直接修改清单:Google 在其配置中提供了必要的挂钩。
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 清单中的内容。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)