Build Version increment with Gradle build script
Wed, 27 Sep 2017
Check out Lines Galaxy Android game to view the results in action.
Make your Gradle build script handle the apk version for you!
Your second Google Play Console release will most certainly end at the APK upload. This would probably be caused by the fact that the APK build version was not incremented.
Yes, this was also my case. I was like whaaat? what version? ooh nooo...
Have no worries. I searched for a way which would handle the build version number automatically. I also wanted to allow the developer to manage certain properties of the version. The solution which I came to works pretty nice for my needs and probably yours if you're reading this.
The build scripts are also handling two types of releases: beta (debug) and production.
Each version has its own API keys, features enabled/disabled, application ids, icons and names. This is what I needed but after a certain point having custom properties for each release type will be a piece of cake.
Customizing the Gradle script to handle build version
First of all you'll need a way to store the current version. This file should be included in the versioning system. Let's call it build.properties. Place the file in your android folder and add these contents:
VERSION_NUMBER=1 VERSION_BUILD=0 VERSION_PATCH=0
Obviously the version_number should start with 1. This value and version_patch will be automatically incremented each time you'll do an assembleRelease.
When you upload an APK each version should be higher than the previous uploaded one.
Version_build will be incremented which each build and is mainly for debug.
Next thing we need to handle is the gradle script. The followind script is added in android task.
android { ...
// read the current state of the build version properties
def buildPropertiesFile = file('build.properties')
def value = 0
// the major and minor properties need to be manually incremented by the dev
// the resulting version could be used for identifying the apk on Google Play Console
def versionMajor = 1 def versionMinor = 0
if(!buildPropertiesFile.canRead()) {
throw new GradleException("Could not read build.properties")
}
// load the properties from the file
def Properties buildProperties = new Properties()
buildProperties.load(new FileInputStream(buildPropertiesFile ))
// we only want to increment the number and patch for release builds
// you'll need to check the exact name of the build task - it might be assembleRelease (without android:)
def runTasks = gradle.startParameter.getTaskNames()
if('android:assembleRelease' in runTasks || 'android:assembleProductionRelease' in runTasks) {
value = 1;
}
def versionNumber = buildProperties['VERSION_NUMBER'].toInteger() + value
def versionPatch = buildProperties['VERSION_PATCH'].toInteger() + value
def versionBuild = buildProperties['VERSION_BUILD'].toInteger() + 1
buildProperties['VERSION_NUMBER'] = versionNumber.toString()
buildProperties['VERSION_PATCH'] = versionPatch.toString()
buildProperties['VERSION_BUILD'] = versionBuild.toString()
// save the build state
versionProps.store(buildPropertiesFile.newWriter(), null)
defaultConfig {
applicationId "com.example.name"
minSdkVersion 15
targetSdkVersion 25
versionCode versionNumber
versionName "${versionMajor}.${versionMinor}.${versionPatch}.${versionBuild}"
}
productFlavors {
beta {
applicationIdSuffix ".beta"
versionNameSuffix "-SNAPSHOT" + getDate()
}
production {
}
}
buildTypes {
release {
minifyEnabled true
proguardFile 'proguard-project.txt'
applicationVariants.all { variant ->
variant.outputs.each { output ->
def SEP = "_"
def flavor = variant.productFlavors[0].name
def buildType = variant.variantData.variantConfiguration.buildType.name
def version = variant.versionName
def date = new Date();
def formattedDate = date.format('ddMMyy_HHmm')
def apkName= applicationId+SEP+flavor+SEP+buildType+SEP+version+SEP+formattedDate+".apk"
output.outputFile = new File(output.outputFile.parent, apkName)
}
}
}
}
...
}
That's it! Your apk build version will now be handled automatically so you can focus on what's important.
My other Android posts could be of interest to you! Check them out or check out Lines Galaxy Android game to view the results in action.
Categories:android, gradle, libgdx