我正在使用这里描述的答案来破坏我的android项目的版本号:
基本上,我所拥有的是我的build.gradle文件中的另一个任务,它读取(然后写入)包含版本名称和版本代码的属性文件:
// Task designed to bump version numbers. This should be the first task run
// after a new release branch is created.
task bumpVersion() {
description = 'Bumps the version number of the current Android release. Should be used as a standalone task, and should only be the first task called after creating a release branch.'
group = 'Build Setup'
Properties props = new Properties();
File propsFile = new File('gradle.properties');
props.load(propsFile.newDataInputStream());
def currentVersionCode = props.getProperty("VERSION_CODE") as int;
def currentVersionName = props.getProperty("VERSION_NAME") as String;
def intPortionsOfVersionName = currentVersionName.tokenize('.').toArray();
def leastSignificantPortion = intPortionsOfVersionName[intPortionsOfVersionName.length - 1] as int;
def newVersionCode = currentVersionCode + 1;
def newVersionName = "";
if (!project.hasProperty('newVersion')) {
leastSignificantPortion = leastSignificantPortion + 1;
intPortionsOfVersionName[intPortionsOfVersionName.length - 1] = leastSignificantPortion;
newVersionName = intPortionsOfVersionName.collect{ it }.join(".");
} else {
newVersionName = project.getProperty('newVersion');
}
props.setProperty("VERSION_NAME", newVersionName as String);
props.setProperty("VERSION_CODE", newVersionCode as String);
props.store(propsFile.newWriter(), null);
}
这很好用,但我遇到的问题是我希望它只在我专门执行./gradlew bumpVersion时运行,并且它每次执行gradle任务时都在运行,例如当我跑./gradlew assembleDebug
当我运行另一个(不相关的)任务时,如何将此任务限制为不运行?
解决方法:
所以,我发现了我做错了什么.我需要让任务实际使用doLast()语法(注意<<):
// Task designed to bump version numbers. This should be the first task run
// after a new release branch is created.
task bumpVersion() << {
description = 'Bumps the version number of the current Android release. Should be used as a standalone task, and should only be the first task called after creating a release branch.'
group = 'Build Setup'
Properties props = new Properties();
File propsFile = new File('gradle.properties');
props.load(propsFile.newDataInputStream());
def currentVersionCode = props.getProperty("VERSION_CODE") as int;
def currentVersionName = props.getProperty("VERSION_NAME") as String;
def intPortionsOfVersionName = currentVersionName.tokenize('.').toArray();
def leastSignificantPortion = intPortionsOfVersionName[intPortionsOfVersionName.length - 1] as int;
def newVersionCode = currentVersionCode + 1;
def newVersionName = "";
if (!project.hasProperty('newVersion')) {
leastSignificantPortion = leastSignificantPortion + 1;
intPortionsOfVersionName[intPortionsOfVersionName.length - 1] = leastSignificantPortion;
newVersionName = intPortionsOfVersionName.collect{ it }.join(".");
} else {
newVersionName = project.getProperty('newVersion');
}
props.setProperty("VERSION_NAME", newVersionName as String);
props.setProperty("VERSION_CODE", newVersionCode as String);
props.store(propsFile.newWriter(), null);
}
不幸的是,这也意味着当我运行gradlew任务时无法识别描述和组,所以为了缓解这一点,我使用以下作为我的最终任务定义:
// Task designed to bump version numbers. This should be the first task run
// after a new release branch is created.
task bumpVersion(description: 'Bumps the version number of the current Android release. Should be used as a standalone task, and should only be the first task called after creating a release branch.', group: 'Build Setup') << {
Properties props = new Properties();
File propsFile = new File('gradle.properties');
props.load(propsFile.newDataInputStream());
def currentVersionCode = props.getProperty("VERSION_CODE") as int;
def currentVersionName = props.getProperty("VERSION_NAME") as String;
def intPortionsOfVersionName = currentVersionName.tokenize('.').toArray();
def leastSignificantPortion = intPortionsOfVersionName[intPortionsOfVersionName.length - 1] as int;
def newVersionCode = currentVersionCode + 1;
def newVersionName = "";
if (!project.hasProperty('newVersion')) {
leastSignificantPortion = leastSignificantPortion + 1;
intPortionsOfVersionName[intPortionsOfVersionName.length - 1] = leastSignificantPortion;
newVersionName = intPortionsOfVersionName.collect{ it }.join(".");
} else {
newVersionName = project.getProperty('newVersion');
}
props.setProperty("VERSION_NAME", newVersionName as String);
props.setProperty("VERSION_CODE", newVersionCode as String);
props.store(propsFile.newWriter(), null);
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。