问题描述
我有新的Android版本系统的基础上尝试摇篮,我在想,什么是autoincrease版本code与它的最好办法。我想两个选项
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
- 创建的版本code文件,从中读取次数,增加它,并把它写回文件
- 解析AndroidManifest.xml中,读版本code从它,增加它,并把它写回的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'
}
}
版本code
发布的发布版本在这种情况下。为了增加它的调试版本更改 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.
这篇关于如何自动增量版本code在Android的摇篮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!