Querying the Installed Android Version

From Android Game Development Wiki

Jump to: navigation, search

[edit] The Problem

Different Android versions offer different APIs. For example, multitouch support is available in API form starting from Android version 2.0 (API level 5). Depending on the available API level developer's might have to resort to using reflecting to access specific APIs or write proxy classes in order to make their application run on all available Android versions while exploiting all features of the latest API levels.

[edit] The Solution

Android supports a couple of different ways to query for the installed Android version. The following snippet shows you two methods that will return the major and minor version of the currently running Android system.

public int getMajorVersion( )
{
	return android.os.Build.VERSION.RELEASE.charAt(0) - '0';
}
 
public int getMinorVersion( )
{
	return android.os.Build.VERSION.RELEASE.charAt(2) - '0';
}

On a 2.0 system this would return 2 via getMajorVersion() and 0 via getMinorVersion().

As it is hard to directly correlate version numbers with API level support one can also query for the API level directly.

String apiVersion = android.os.Build.VERSION.SDK;

The returned string actually encodes the number of the API level supported. For a 2.0 device this would return "4", for a 1.5 device this would return "3" and so on. Note that this constant is deprecated. The Android API reference suggests the use of android.os.Build.VERSION.SDK_INT which directly returns the API level as an integer. However, this is only supported starting from API level 4 (Android version 1.6) so using the android.os.Build.VERSION.SDK constant is the most backward compatible way which will also work on devices with Android version 1.5 and below.

[edit] External Links

android.os.Build.VERSION

Personal tools