This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Initializes versionName and versionCode from version.properties file. | |
* | |
* Just create version.properties file in the project's or root project's folder with the next | |
* properties: | |
* | |
* major=1 | |
* minor=2 | |
* patch=3 | |
* | |
* Optional command line parameters: | |
* | |
* versionMetadata - metadata that will be added at the end of versionName | |
* | |
* Example: | |
* | |
* gradlew assembleDebug -PversionMetadata=0df74f19 | |
*/ | |
final String VERSION_PROPERTIES_FILENAME = "version.properties" | |
final int MAX_MINOR_DIGITS = 2 | |
final int MAX_PATCH_DIGITS = 2 | |
static int readIntegerProperty(Properties properties, String key) { | |
String value = properties.get(key) | |
if (!value) { | |
throw new GradleException("Cannot read '${key}' property") | |
} | |
if (!value.isInteger()) { | |
throw new GradleException("'${key}' property is not integer") | |
} | |
return value.toInteger() | |
} | |
File[] versionPropertiesLocations = [ | |
project.file(VERSION_PROPERTIES_FILENAME), | |
project.rootProject.file(VERSION_PROPERTIES_FILENAME) | |
] | |
File versionPropertiesFile = versionPropertiesLocations.find { it.exists() } | |
if (!versionPropertiesFile) { | |
throw new GradleException("Cannot find ${VERSION_PROPERTIES_FILENAME} file") | |
} | |
Properties versionProperties = new Properties() | |
versionProperties.load(versionPropertiesFile.newReader()) | |
int major = readIntegerProperty(versionProperties, 'major') | |
int minor = readIntegerProperty(versionProperties, 'minor') | |
int patch = readIntegerProperty(versionProperties, 'patch') | |
if (minor >= 10**MAX_MINOR_DIGITS) { | |
throw new GradleException("Minor version cannot contain more than ${MAX_MINOR_DIGITS} digits." + | |
" Current minor version is ${minor}.") | |
} | |
if (patch >= 10**MAX_PATCH_DIGITS) { | |
throw new GradleException("Patch version cannot contain more than ${MAX_PATCH_DIGITS} digits." + | |
" Current patch version is ${patch}.") | |
} | |
int versionCode = (major * 10**MAX_MINOR_DIGITS + minor) * 10**MAX_PATCH_DIGITS + patch | |
String versionName = "${major}.${minor}.${patch}" | |
if (project.hasProperty('versionMetadata') && project.versionMetadata) { | |
versionName = "${versionName}+${project.versionMetadata}" | |
} | |
android.defaultConfig.versionName = versionName | |
android.defaultConfig.versionCode = versionCode | |
println "versionName " + android.defaultConfig.versionName | |
println "versionCode " + android.defaultConfig.versionCode |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment