More specifically I have a few build configs:
signingConfigs {
debug {
keyAlias ''
keyPassword ''
storeFile file('')
}
release {
keyAlias ''
keyPassword ''
storeFile file('')
storePassword ''
}
}
....
defaultConfig {
applicationId ""
minSdkVersion 21
targetSdkVersion 23
versionCode code
}
I want the gradle to autoincrement code version every time the 'release' is run.
What I have so far:
def code = 1;
//Get all the gradle task names being run
List<String> runTasks = gradle.startParameter.getTaskNames();
for (String item : runTasks) {
//Get the version.properties file. Its our custom file for storing a code version, please don't remove it
def versionPropsFile = file('version.properties')
def Properties versionProps = new Properties()
//This will prevent the gradle from exploding when there's no file yet created
if (versionPropsFile.exists())
versionProps.load(new FileInputStream(versionPropsFile))
//It will insert the "0" version in case the file does not exist
code = (versionProps['VERSION_CODE'] ?: "0").toInteger()
if (item.contains("release")) {
// If we're building up on Jenkins, increment the version strings
code++
versionProps['VERSION_CODE'] = code.toString()
//It will overwrite the file even if it doesn't exist
versionProps.store(versionPropsFile.newWriter(), null)
}
}
The problem:
I can't seem to get inside if (item.contains("release"))
. Its always false but I definitely see that gradle runs this taks. How can I fix it or at least output in console all the tasks (their names) being run by gradle?
My implementation of this problem:
I have a version file which contains the version number. From this file we get the version. When the build contains the task "publishRelease" (it can be any other task) we increase the version in the version file. I like this sollution because it keeps the defaultConfig clean of coding logic.
The version.properties
VERSION_CODE=45
and in the android config part
defaultConfig {
applicationId "..."
minSdkVersion 16
targetSdkVersion 23
versionCode getVersion()
versionName "0.2." + versionCode
}
and the getVersion()
def getVersion() {
def versionPropertiesFile = file('version.properties')
def appVersion = -1;
if (versionPropertiesFile.canRead()) {
def Properties versionProps = new Properties()
versionProps.load(new FileInputStream(versionPropertiesFile))
appVersion = versionProps['VERSION_CODE'].toInteger()
def runTasks = gradle.startParameter.taskNames
if ('publishRelease' in runTasks) {
print("Increase version to " + appVersion + '\n')
appVersion += 1
versionProps['VERSION_CODE'] = appVersion.toString()
versionProps.store(versionPropertiesFile.newWriter(), null)
}
} else {
throw new GradleException("Could not read version.properties!")
}
return appVersion;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With