Automagical version code and name
Automagical version code and name

I stated before: I dislike tasks that are not automated. Very much.

When developing a Android application one of the item to maintain is the version code (a integer) and the version name (a string). The version code should be a unique number of each new release of the application and the version name should allow users to clearly state what release they are using.

Here is my very simple solution to automate this version control: have the build system define the version code and post fix it to the version name using a formatted time stamp. This way the constant incrementation of the version code is guaranteed and the version name has a easy reference to that code. One small detail: I prefix the version code with a fixed digit number that is a reference to the major version (release) of the application.

So here relevant part of the application manifest:

    1: <?xml version="1.0" encoding="utf-8"?>
    2: <manifest xmlns:android=""
    3:  package=""
    4:   android:versionCode="0019999999"
    5:  android:versionName="1.0 beta 1 - 0019999999">
    6:  <!-- version code is redefined during build to a number with the following format:
    7:    3 digits for versioning,
    8:       4 digits for the year,
    9:       3 digits for the day of the year
   10:       IMPORTANT: don't change the number in the version code and name as it's used as the token to be replaced -->

In the build.xml (I'm using ant as the build system for android applications) a uncomment and implemented the targets -pre-build and -post-compile.

The -pre-build target:

    1:     <target name="-pre-build">
    2:  <tstamp>
    3:   <format property="timeStamp" pattern="yyyyDDD"/>
    4:  </tstamp>
    5:  <echo level="info" message="Changing the version code and name to timestamp value ${timeStamp}"/>
    6:  <replace file="AndroidManifest.xml" token="9999999" value="${timeStamp}"/>
    7:     </target>

The -post-compile target:

    1:     <target name="-post-compile">
    2:  <echo level="info" message="Returning version code and name to predefined values"/>
    3:  <replace file="AndroidManifest.xml" token="${timeStamp}" value="9999999"/>
    4:     </target>

Now every build (as long as done in different days) will have it's own version code and name. Builds within eclipse will always have the same version code and name. Each major release of the application will lead to the creation of a maintenance branch where the version code first four digits will be incremented.