[Tips] Auto-increment versionCode using Gradle

credit : http://blog.iteedee.com/2014/01/android-gradle-tutorial/

In this article, I will show you we have no need to increase versionCode for each release manually.

Actually, there are many ways to increase versionCode automatically, and here is one.

First, create version.properties file and store your versionCode, versionBuild and versionName like this.

VERSION_NAME=1.0.6
VERSION_BUILD=12
VERSION_CODE=3

Then, add a few lines of code to build.gradle file in app module.

android {
compileSdkVersion 25
buildToolsVersion "25.0.2"

def versionPropsFile = file('version.properties')

if (versionPropsFile.canRead()) {
def Properties versionProps = new Properties()

versionProps.load(new FileInputStream(versionPropsFile))
        def name = versionProps['VERSION_NAME']
def code = versionProps['VERSION_CODE'].toInteger() + 1
        versionProps['VERSION_CODE']=code.toString()
versionProps.store(versionPropsFile.newWriter(), null)

defaultConfig {
applicationId "com.bsupits.example.autoincrement"
versionName name
versionCode code
minSdkVersion 15
targetSdkVersion 25
}
}
else {
throw new GradleException("Could not read version.properties!")
}
}

That’s enough and it works! Let’s try building your application.
…. And press spacebar then click sync now. Repeat again.

Look at the value of VERSION_CODE ……

Yeah!! You realize every time you sync Gradle, the value of VERSION_CODE has been increased.

Question : I would like to increase only versionCode on release, How could I do?
Answer : You can determine which build type you’re building from taskGraph.

tasks list

From the image above you will see many tasks that could be produced each time you build. If you want to increase only versionCode on release, just create some method like this.

def increaseVersionCode() {
gradle.taskGraph.whenReady { taskGraph ->
if (taskGraph.hasTask(assembleRelease)) {
/* when run release task */
if (versionPropsFile.canRead()) {
def Properties versionProps = new Properties()

versionProps.load(new FileInputStream(versionPropsFile))

def build = versionProps['VERSION_BUILD'].toInteger() + 1
def code = versionProps['VERSION_CODE'].toInteger() + 1

versionProps['VERSION_BUILD'] = build.toString()
versionProps['VERSION_CODE'] = code.toString()
versionProps.store(versionPropsFile.newWriter(), null)
} else {
throw new GradleException("Could not read version.properties!")
}
}
}
}

And change some lines of previous code to …

android {
compileSdkVersion 25
buildToolsVersion "25.0.2"

def versionPropsFile = file('version.properties')

if (versionPropsFile.canRead()) {
def Properties versionProps = new Properties()

versionProps.load(new FileInputStream(versionPropsFile))
        def name = versionProps['VERSION_NAME']
def code = versionProps['VERSION_CODE'].toInteger()
        versionProps['VERSION_CODE']=code.toString()
versionProps.store(versionPropsFile.newWriter(), null)

defaultConfig {
applicationId "com.bsupits.example.autoincrement"
versionName name
versionCode code
minSdkVersion 15
targetSdkVersion 25
}
}
else {
throw new GradleException("Could not read version.properties!")
}

// add this line
increaseVersionCode()
}
Try it !!

Wrap-Up

This solution is one of many solutions to increase version of code automatically and in my opinion this is the simplest and easy way to understand for beginners. You can try other ways like “using manifest and regex” yourself. Hopefully, this article will be helpful to you and apologize for my poor English.