Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Auto increment version number on release builds... You can change the build type on versionCode.gradle, you also will need to commit and push gradle.properties on your CI
apply from: 'versionCode.gradle'
android {
defaultConfig {
versionName VERSION_NAME
versionCode Integer.parseInt(VERSION_CODE)
}
}
VERSION_NAME=1.0.0
VERSION_CODE=10
task('increaseVersionCode') << {
def versionCode = Integer.parseInt(VERSION_CODE) + 1
ant.propertyfile(file: "../gradle.properties") {
entry(key: "VERSION_CODE", value: versionCode)
}
}
tasks.whenTaskAdded { task ->
if (task.name == 'generateReleaseBuildConfig) {
task.dependsOn 'increaseVersionCode'
}
}
@evrenozturkhurriyet

This comment has been minimized.

Copy link

@evrenozturkhurriyet evrenozturkhurriyet commented Sep 26, 2016

Thank you for the information.
We have a missing character here <if (task.name == 'generateReleaseBuildConfig) {>
<'> character is missing :P

@tuanchauict

This comment has been minimized.

Copy link

@tuanchauict tuanchauict commented Oct 13, 2016

My increaseVersionCode task :

task('increaseVersionCode') << {
    def versionCode = Integer.parseInt(VERSION_CODE) + 1
    def versionName = VERSION_NAME.split("\\.")
    def last = Integer.parseInt(versionName[versionName.length - 1])
    versionName[versionName.length - 1] = Integer.toString(last + 1)
    versionName = versionName.join(".")

    ant.propertyfile(file: "../gradle.properties") {
        entry(key: "VERSION_CODE", value: versionCode)
        entry(key: "VERSION_NAME", value: versionName)
    }
}

The VERSION_NAME will auto increase along with VERSION_CODE (worth to maven dependency)

@svrolanski

This comment has been minimized.

Copy link

@svrolanski svrolanski commented Jan 27, 2017

This is nice, but the versionCode in gradle.properties is incremented after gradle script is run, so VERSION_CODE equals 100 while the generated APK has versionCode 99. Very confusing imho.

@ArthurSav

This comment has been minimized.

Copy link

@ArthurSav ArthurSav commented Feb 15, 2018

Based on this i wrote another implementation that solves the problem @svrolanski has and also updates version name based on the last commit tag(you can delete that part if it doesn't work for you)

@Stormwind99

This comment has been minimized.

Copy link

@Stormwind99 Stormwind99 commented Jul 6, 2018

I wrote a simple non-Android version that only increments a build number - hope someone finds it useful.

@SUPERCILEX

This comment has been minimized.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.