5

Currently I'm trying to improve our buildscript with auto incrementing the versioncode so our QA team has got a clue what build they are testing and can log against a specific versioncode. We have got 3 productFlavors (staging, qa, production) and 2 signing configs (debug, release).

I looked into different solutions:

How to autoincrement versionCode in Android Gradle

How to autoincrement versionCode in Android Gradle

Autoincrement VersionCode with gradle extra properties

Based on those answers I've built a versioncode incrementor using a version.properties file.

Now the problem arises that for EVERY productFlavor and SigningConfig combination (+ the gradle sync in android studio) a new versioncode is generated. I want the versioncode to increment whenever I hit the play button to create a qaRelease build. So our buildcycle would be:

  • development (never change the versioncode)
  • qual (Update the versioncode)
  • production (never change the versioncode)
Community
  • 1
  • 1
Vince V.
  • 3,115
  • 3
  • 30
  • 45

2 Answers2

8

How I solved it:

  • I created a file version.properties in the root of my android project and added the versioncode in it.

  • Then I created method called getCurrentVersionCode() in my build.gradle file that reads the current versioncode.

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 a 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 triggers on a QA build.

Code:

task('increaseVersionCode') << {
    setUpdatedVersionCode()
}

tasks.whenTaskAdded { task ->
    if (task.name == 'assembleQaRelease') {
        task.dependsOn 'increaseVersionCode'
    }
}

That way it saves the versioncode in a separate file so I don't need to edit my build.gradle.

Vince V.
  • 3,115
  • 3
  • 30
  • 45
4

You could try something like that

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 the 'assembleQaRelease' to increment version code to the wanted task.

Médéric
  • 938
  • 4
  • 12
  • With `file("build.gradle")` you mean the android manifest file instead of the build.gradle? – Vince V. May 29 '15 at 07:55
  • That's depend when you set your version code. It's a good practice to define it in the build.gradle, which override the manifest one. – Médéric May 29 '15 at 07:57
  • I edited my answer. Just pick the solution adapted to your project ;) – Médéric May 29 '15 at 07:59
  • I'll take yours as an accepted answer because you set me on the right track. But I did it somewhat else and I'm going to add that also in a solution :) – Vince V. May 29 '15 at 08:09