Automatically Incrementing Android Bundle Version Code

When uploading Android bundles (.aab) file to the Playstore, it's important that the Version code be higher than a previously uploaded bundle.

If it isn't, you get the error "Version code has already been used. Try another version code". Pretty frustrating when that happens, as you have to restart the build to upload.


Here's an approach to automatically increasing the Version code, so we don't have to worry about manually increasing the version code before building the Android bundle.

We'll be using this awesome service: increment.build.

What the service does is pretty simple - it returns an incremented number on demand.

Getting Started

So, to get started, the first step would be to:

  • Set up the service to our current version code number. Say the current version code of the app is 22, we want the service to start incrementing from that number. We do that by running the following command in the terminal (where edwardsmoses-app is a unique name you'll want to use for your app):
Copy
 curl https://increment.build/edwardsmoses-app/set/22



When that's done, we want to update our build.gradle file in the android/app directory to auto-increment the version code.


To do, we'll add the below code snippet to the gradle file:

Copy
def getIncrementedVersionCode () {
    def link = 'https://increment.build/edwardsmoses-app'

    def request =  ['curl', '-X', 'GET', link].execute()
    request.waitFor()

    def response = request.text
    println("Incremented Version Code")
    println(response)

    return response.toInteger()
}

What the above does is pretty simple, it runs a curl request, waits for the response (a new incremented number), and then converts that number to an integer (without converting to an integer, we'll get a build error).

Then, the final step would be to replace the versionCode line with the below:

Copy
versionCode getIncrementedVersionCode()


And boom 😎, we're done. Every-time the Android bundle is built, we get a .aab file that has a new version code, and we don't have to manually increase the version code.


As always, I'll love to know how this can be improved, or alternative ways this can be done.
Looking forward to comments and suggestions.

Comments

Edwards Moses - Web & Mobile — React & React Native Consultant

Edwards Moses
Web & Mobile — React & React Native Consultant

I'm Edwards, based in Lagos, Nigeria.
Freelancer Software Developer — collaborating with teams to craft extraordinary products.

From conception through to completion, I find immense joy in witnessing the evolution of an idea into a fully realized product in the hands of users. Check out my projects and articles to see what I've been up to lately.

Ready to bring your ideas to life?