问题描述
我正在试验基于 Gradle 的新 Android 构建系统,我在想,用它自动增加 versionCode 的最佳方法是什么.我在考虑两种选择
I'm experimenting with new Android build system based on Gradle and I'm thinking, what is the best way to autoincrease versionCode with it. I am thinking about two options
- 创建versionCode文件,从中读取数字,增加它并将其写回文件
- 解析AndroidManifest.xml,从中读取versionCode,增加它并写回AndroidManifest.xml
有没有更简单或更合适的解决方案?
Is there any more simple or suitable solution?
有没有人使用过一种mentiod选项并且可以与我分享?
Has anyone used one of mentiod options and could share it with me?
推荐答案
我决定了第二个选项 - 解析 AndroidManifest.xml
.这是工作片段.
I have decided for second option - to parse AndroidManifest.xml
. Here is working snippet.
task('increaseVersionCode') << {
def manifestFile = file("AndroidManifest.xml")
def pattern = Pattern.compile("versionCode="(\d+)"")
def manifestText = manifestFile.getText()
def matcher = pattern.matcher(manifestText)
matcher.find()
def versionCode = Integer.parseInt(matcher.group(1))
def manifestContent = matcher.replaceAll("versionCode="" + ++versionCode + """)
manifestFile.write(manifestContent)
}
tasks.whenTaskAdded { task ->
if (task.name == 'generateReleaseBuildConfig') {
task.dependsOn 'increaseVersionCode'
}
}
在这种情况下,
versionCode
是为发布版本发布的.要为调试版本增加它,请在 task.whenTaskAdded
回调中更改 task.name 方程.
versionCode
is released for release builds in this case. To increase it for debug builds change task.name equation in task.whenTaskAdded
callback.
这篇关于如何在 Android Gradle 中自动增加 versionCode的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!