Auto increment versioncode versions only
I am currently trying to improve our buildscript by automatically incrementing the version code so that our QA team understands which build they are testing and can log in with a specific version code. We have 3 productFlavors (stage, qa, production) and 2 signature configs (debug, release).
I have looked at different solutions:
How to auto-increment version Code in Android Gradle
How to auto-increment version Code in Android Gradle
Autoincrement VersionCode with additional gradle properties
Based on these answers, I built a code version increment using the version.properties file.
Now the problem is that for EVERY combination of productFlavor and SigningConfig (+ gradle sync in android studio) new version code is generated. I want the version code to increment every time I hit the play button to create the qaRelease assembly. So our construction cycle will be:
- development (never change version code)
- qual (Update version code)
- production (never change version code)
You can try something like this
import java.util.regex.Pattern
task('increaseVersionCode') << {
... // You could code in your increment system, for eg
// Using build.gradle (recommended)
def buildFile = file("build.gradle")
def pattern = Pattern.compile("versionCode\\s+(\\d+)")
def manifestText = buildFile.getText()
def matcher = pattern.matcher(manifestText)
matcher.find()
def versionCode = Integer.parseInt(matcher.group(1))
def manifestContent = matcher.replaceAll("versionCode " + ++versionCode)
buildFile.write(manifestContent)
// Using manifest
def manifestFile = file('AndroidManifest.xml')
def matcher = Pattern.compile('versionCode=\"(\\d+)\"')
.matcher(manifestFile.getText())
matcher.find()
def manifestContent = matcher.replaceAll('versionCode=\"' +
++Integer.parseInt(matcher.group(1)) + '\"')
manifestFile.write(manifestContent)
}
tasks.whenTaskAdded { task ->
if (task.name == 'assembleQaRelease') {
task.dependsOn 'increaseVersionCode'
}
}
You can adapt 'assembleQaRelease' to expand the version code to the required task.
How I solved it:
-
I created a version.properties file in the root of my Android project and added the version code to it.
-
Then I created a method called getCurrentVersionCode () in the build.gradle file that reads the current version code.
code:
def getCurrentVersionCode() {
def versionPropsFile = file('version.properties')
def Properties versionProps = new Properties()
versionProps.load(new FileInputStream(versionPropsFile))
return versionProps['version_code'].toInteger()
}
I also created a method to generate new code:
code:
// http://stackoverflow.com/questions/30523393/auto-increment-versioncode-only-on-releases
//
def setUpdatedVersionCode() {
def versionPropsFile = file('version.properties')
def Properties versionProps = new Properties()
def code = getCurrentVersionCode() + 1
versionProps['version_code'] = code.toString()
versionProps['version_name'] = generateVersionName()
versionProps.store(versionPropsFile.newWriter(), null)
}
and then I created a task that starts the QA build.
code:
task('increaseVersionCode') << {
setUpdatedVersionCode()
}
tasks.whenTaskAdded { task ->
if (task.name == 'assembleQaRelease') {
task.dependsOn 'increaseVersionCode'
}
}
This way it keeps the version code in a separate file so I don't have to edit the build.gradle file.