A while back I was preparing an app to upload tothe Android MarketGoogle 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:versionCodeandversionName.

versionCodeis 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.versionNameis 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 themanifestelement like this on your first app version:

<manifest
    xmlns:android=”http://schemas.android.com/apk/res/android”
    package=”com.acme.app”
    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:

<manifest
    xmlns:android=”http://schemas.android.com/apk/res/android”
    package=”com.acme.app”
    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 aPackageInfoclass for the app as that can give us information about the Android package gleaned from the application manifest file – specifically the class exposesversionCodeandversionNamemembers that return the values from the manifest discussed above.

How do we get aPackageInfoclass? That can be arranged by aPackagManagerobject that offers agetPackageInfo()method (that we can access as aPackageInfoproperty). Pass it the package name and it will return the goods for us.

TheContextclass is whatActivityinherits from, and it defines two useful methods:getPackageManager()andgetPackageName()– hopefully it will be patently clear these will finish the job off.

In the case of my plasma app I have layout resource containing aTextViewcalledaboutText. 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 theTextViewcontrol and give it a formatted string containing the version information looks like this:

var aboutTextView: TextView :=
 TextView(findViewById(R.id.aboutText));
var versionName :=
  PackageManager.PackageInfo[PackageName, 0].versionName;
aboutTextView.Text :=
  String[R.string.plasma_about_text, versionName];
Title := String[R.string.plasma_about_title, 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 :=
  TextView(findViewById(R.id.aboutText));
var versionName :=
  getPackageManager().getPackageInfo(
    getPackageName(), 0).versionName;
aboutTextView.setText(getString(
  R.string.plasma_about_text, versionName));
setTitle(getString(R.string.plasma_about_title, versionName));

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