gpt4 book ai didi

android - 颠簸android项目的版本号但不是每个构建

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:24:40 24 4
gpt4 key购买 nike

我正在使用此处描述的答案来增加我的 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 tasks 时无法识别描述和组,因此为了缓解这种情况,我使用以下内容作为我的最终任务定义:

// 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);
}

关于android - 颠簸android项目的版本号但不是每个构建,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25942425/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com