My implementation of this problem:
I have a version file that contains the version number. From this file we get the version. When the assembly contains the publishRelease task (this can be any other task), we increase the version in the version file. I like this solution because it keeps Config clean by default from coding logic.
Parameters version.properties
VERSION_CODE=45
and in the Android configuration section
defaultConfig { applicationId "..." minSdkVersion 16 targetSdkVersion 23 versionCode getVersion() versionName "0.2." + versionCode }
and 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; }
source share