Getting an Android app’s version number

A while back I was preparing an app to upload to the Android Market Google Play. One of the requirements of uploading is that the app must contain version information. As of the August update of Oxygene for Java 5.2, the default Android application template does not contain version information so you need to insert this manually, and then update it as and when your code is updated.

You add version information to your Android application by editing the manifest file. In the outermost element, the manifest element, you add in a couple of attributes: versionCode and versionName.

versionCode is an integer that can be anything you like, as long as each new version code is higher than the last – clearly an incrementing number is the simplest option here. versionName is the string that is shown to the user – it’s not used by the system so can be any test you like.

So, for example, you could open the manifest element like this on your first app version:

    android:versionCode=”1″ android:versionName=”1.00″>

Each time you want to issue a new application you simply update the two version-related attributes, maybe like this:

    android:versionCode=”2″ android:versionName=”1.01″>

With that done, what’s involved in displaying some version information to the user, say in an About screen in the app? Well that’s what I wanted to do with the app mentioned above – it was my Plasma app (a free graphics plasma effect demo written in Oxygene for Java).

To get version info we must access a PackageInfo class for the app as that can give us information about the Android package gleaned from the application manifest file – specifically the class exposes versionCode and versionName members that return the values from the manifest discussed above.

How do we get a PackageInfo class? That can be arranged by a PackagManager object that offers a getPackageInfo() method (that we can access as a PackageInfo property). Pass it the package name and it will return the goods for us.

The Context class is what Activity inherits from, and it defines two useful methods: getPackageManager() and getPackageName() – hopefully it will be patently clear these will finish the job off.

In the case of my plasma app I have layout resource containing a TextView called aboutText. I also have two string resources that define formatted strings waiting to have a version string (the version name from the manifest file) plugged in. For example, one of them is used to set up the About activity’s title and is defined thus:

About Plasma %s

The code to access the TextView control and give it a formatted string containing the version information looks like this:

var aboutTextView: TextView :=
var versionName :=
  PackageManager.PackageInfo[PackageName, 0].versionName;
aboutTextView.Text :=
  String[R.string.plasmaabouttext, versionName];
Title := String[R.string.plasmaabouttitle, versionName];

If you want to use the original method names as defined in the Google Android documentation, as opposed to the equivalent implied properties, it would look like this instead:

var aboutTextView: TextView :=
var versionName :=
    getPackageName(), 0).versionName;
  R.string.plasmaabouttext, versionName));
setTitle(getString(R.string.plasmaabouttitle, versionName));

Notice we are making use of the overload of getString() that takes a string resource id and a list of format string arguments to use when building the formatted string.