Bump version number of android project but not for every build
I am using the answer described here to bring up the version number of my Android project:
Basically, I have another task in my file build.gradle
that reads (and then writes) a properties file containing the version name and version code:
// 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);
}
It works very well, but the problem that I faced is that I want it to be launched only when I specifically perform ./gradlew bumpVersion
, and it is currently running every time I perform gradle tasks, such as when I run./gradlew assembleDebug
How can I restrict this task not to run when I start another (unrelated) task?
source to share
So I found out what I was doing wrong. I needed to force the task to actually use the syntax doLast()
(note on <<
):
// 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);
}
Unfortunately, this also means that the description and group are not recognized at startup gradlew tasks
, so to facilitate this, I use the following as my endpoint definition:
// 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);
}
source to share