Freelancing for Pale Blue

Looking for flexible work opportunities that fit your schedule?

Auto version code for Android: a simple approach

Android Apr 23, 2020

A consider myself a decently experienced Android developer. But I stumbled when recently a friend, an iOS-developer-turning-into-Android-developer, asked me if there's a way to automate the boring task of updating the version code before a new cut.

I mean I knew that Gradle, the build system Android Studio uses by default, uses a  Groovy-based DSL that is quite powerful. So this should be possible.

I went for a search online, but I had in mind that whatever I found should satisfy the following:

  • automatic version code every time I run / release
  • no dependencies on external files
  • (optional) human-readable version name

Auto-increment version code (literally)

I found that is possible to add a new Gradle task, in the correct order, that will do exactly what I manually do: increase the build version code.

task('increaseVersionCode') {
    def buildGradle = file("build.gradle")
    def buildGradleContents = buildGradle.getText()
    def matcher =
    def versionCode = android.defaultConfig.versionCode
    buildGradle.write(matcher.replaceAll("versionCode " + ++versionCode))

tasks.whenTaskAdded { task ->
    if ( == 'generateReleaseBuildConfig') {
        task.dependsOn 'increaseVersionCode'
Add this to the bottom of your build.gradle (app)

As you can see, using regex you can find the part where versionCode is defined, increase the version code, and replace the contents of the file.

Although this works, it's quite brittle. Any future change to the way versionCode is defined will break this. If you decide to change the versionName as well, this can become more complicated (especially if you have multiple Build Variants).

Date-derived version code

Another approach I found is creating a new version code based on date/time.

The highest version code you can have is 2100000000. You can print the date in yyMMddHHmm but this only allows the latest year to be 2021, which is quite limiting. Going with yyMMddHH, on the other hand, allows a different version every hour until 2099. I don't plan to release my apps more often than hourly, or after 2099.  

def getVersionCode(offset = 0) {
    def formattedDate = new Date().format('yyMMddHH')
    return formattedDate.toInteger() + offset
Add this to the bottom of your build.gradle (app)

The offset is there in the unlikely event that I do want to create another versionCode in the same hour.

Then, in your defaultConfig section, you need to replace the static versionCode with this dynamic one.

defaultConfig {        
    versionCode getVersionCode()
    versionName "1.3-b${getVersionCode()}"
build.gradle (app)

I find this approach less brittle and less invasive. Also, this works well if you are defining Build Variants and using the versionNameSuffix to distinguish between them.

Hopefully, my simple approach in automating versioning for Android might be useful in your projects :)


Great! You've successfully subscribed.
Great! Next, complete checkout for full access.
Welcome back! You've successfully signed in.
Success! Your account is fully activated, you now have access to all content.