3
我使用這裏描述的答案,撞我的Android項目的版本號: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
當我運行另一個(不相關的)任務時,如何限制此任務不能運行?