Using an ‘Unusual’ Timestamp as an Auto-generated Version Code for Android Apps

You might encounter an error of conflicting version code when you try to automate a deploy process for an Android application. You’re reading this post, and I’m writing this post. It means that we experienced the same problem, mate!

Google Api Error: apkUpgradeVersionConflict: APK specifies a version code that has already been used.

According to the official document, we can use any positive number and don’t have to increase the number sequentially. At first, I was going to use Unix time as a version code, but I changed my mind after I read this warning.

Warning: The greatest value Google Play allows for versionCode is 2100000000.

Fair enough, this is a limitation of a 32bit signed-integer. However, it means my versioning system will expire at 18 July 2036. Is it the far future for a personal app? Everything will change in 18 years? I think so, but if we can avoid this problem easily, it’s better!

Two popular approaches

One of the solutions is to use a saved number for the version code. Read it from a file or storage, increment it, and write it back. It produces a clean version code, and this is quite easy to understand. I thought it’s a good idea since I like a simple solution, but I don’t like making a dependency to an external file.

Another solution is to calculate a version code from the version name. For example, majorVersion * 10000 + minorVersion * 100 + patchVersion. We have enough digits to apply this method. It’s also simple, practical, and no dependency! However, I decided not to use this because I don’t want to change the version name for each deployment. Especially for internal test track, I’d love to deploy my beta app with just changing the code itself.

Just postpone the expiration date

set_version_script:
- U=$(expr $(date +%s) / 10) # increase by 1 in each 10 seconds
- echo "versionCode=${U}" >> version.properties

and reading it in build.gradle.

defaultConfig {
...
versionCode versionProperties['versionCode'].toInteger()
...
}

It works well because deployments happen not so frequently and I believe everything will change in 600 years!

Conclusion

Thanks reading! If you have any other ideas, please comment to this post.

usually jiggy, similar to a cuboid.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store