Android: automatic set or update versionName from versionCode

13 Jun

In Android, version name and version code serve for different purposes, as is found in Android documentation here:


An internal version number. This number is used only to determine whether one version is more recent than another, with higher numbers indicating more recent versions. This is not the version number shown to users; that number is set by the versionName attribute. The value must be set as an integer, such as “100”. You can define it however you want, as long as each successive version has a higher number. […]


The version name shown to users. This attribute can be set as a raw string or as a reference to a string resource. The string has no other purpose than to be displayed to users. The versionCode attribute holds the significant version number used internally.

In practice, it is often the case that the version name simply maps version code in specific ways, e.g.

2016 (version code)  –>  v2.0.1.6 (version name)

It might be tedious that each time you edit both in your build.gradle or in your manifest file:


What if we keep version code only? This not only make it simple, but also consistent, you update the version code for update purposes, and the string representation, i.e. version name gets automatic updated/refreshed! Cool, right?

And here it is how, in build.gradle file, add below snippet:

buildTypes {
    release {
        applicationVariants.all { variant >
        //Version Code, e.g. 2016
        String versionCode = variant.versionCode; 
        //VersionName, “v2.0.1.6”
        variant.mergedFlavor.versionName = v + versionCodeString.split().join(.);  

This is a merely an example to transform an integer version code e.g. 2016, to a string version name: “v2.0.1.6”. It is up to you to do whatever mapping in your own implementation.

Happy coding and automation!

Leave a comment

Posted by on June 13, 2016 in Android, Programming


Tags: , , , , , , , ,

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: