Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Auto increment versioncode only on releases

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)
like image 567
Vince V. Avatar asked May 29 '15 07:05

Vince V.


Video Answer


1 Answers

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.

like image 154
Vince V. Avatar answered Oct 13 '22 10:10

Vince V.