Retrieving Android API version programmatically

AndroidAndroid Api-Levels

Android Problem Overview


Is there any way to get the API version that the phone is currently running?

Android Solutions


Solution 1 - Android

As described in the Android documentation, the SDK level (integer) the phone is running is available in:

android.os.Build.VERSION.SDK_INT

The class corresponding to this int is in the android.os.Build.VERSION_CODES class.

Code example:

if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP){
    // Do something for lollipop and above versions
} else{
    // do something for phones running an SDK before lollipop
}

Edit: This SDK_INT is available since Donut (android 1.6 / API4) so make sure your application is not retro-compatible with Cupcake (android 1.5 / API3) when you use it or your application will crash (thanks to Programmer Bruce for the precision).

Corresponding android documentation is here and here

Solution 2 - Android

Very easy:

   String manufacturer = Build.MANUFACTURER;
   String model = Build.MODEL;
   int version = Build.VERSION.SDK_INT;
   String versionRelease = Build.VERSION.RELEASE;

Log.e("MyActivity", "manufacturer " + manufacturer
            + " \n model " + model
            + " \n version " + version
            + " \n versionRelease " + versionRelease
    );
Output:
E/MyActivity:   manufacturer ManufacturerX
                model SM-T310 
                version 19 
                versionRelease 4.4.2

Solution 3 - Android

Build.VERSION.RELEASE;

That will give you the actual numbers of your version; aka 2.3.3 or 2.2. The problem with using Build.VERSION.SDK_INT is if you have a rooted phone or custom rom, you could have a non standard OS (aka my android is running 2.3.5) and that will return a null when using Build.VERSION.SDK_INT so Build.VERSION.RELEASE will work no matter using standard Android version or not !

To use it, you could just do this;

String androidOS = Build.VERSION.RELEASE;

Solution 4 - Android

try this:

 if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.GINGERBREAD) {
     // only for gingerbread and newer versions
 }

Solution 5 - Android

Taking into account all said, here is the code I use for detecting if device has Froyo or newer Android OS (2.2+):

public static boolean froyoOrNewer() {
	// SDK_INT is introduced in 1.6 (API Level 4) so code referencing that would fail
	// Also we can't use SDK_INT since some modified ROMs play around with this value, RELEASE is most versatile variable
	if (android.os.Build.VERSION.RELEASE.startsWith("1.") ||
		android.os.Build.VERSION.RELEASE.startsWith("2.0") ||
		android.os.Build.VERSION.RELEASE.startsWith("2.1"))
		return false;

	return true;
}

Obviously, you can modify that if condition to take into account 1.0 & 1.5 versions of Android in case you need generic checker. You will probably end up with something like this:

// returns true if current Android OS on device is >= verCode 
public static boolean androidMinimum(int verCode) {
	if (android.os.Build.VERSION.RELEASE.startsWith("1.0"))
		return verCode == 1;
	else if (android.os.Build.VERSION.RELEASE.startsWith("1.1")) {
		return verCode <= 2;
	} else if (android.os.Build.VERSION.RELEASE.startsWith("1.5")) {
		return verCode <= 3;
	} else {
		return android.os.Build.VERSION.SDK_INT >= verCode;
	}
}

Let me know if code is not working for you.

Solution 6 - Android

android.os.Build.VERSION.SDK should give you the value of the API Level. You can easily find the mapping from api level to android version in the android documentation. I believe, 8 is for 2.2, 7 for 2.1, and so on.

Solution 7 - Android

SDK.INT is supported for Android 1.6 and up

SDK is supported for all versions

So I do:

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

Credits to: CommonsWare over this answer

Solution 8 - Android

Got it. Its using the getApplicationInfo() method of the Context class.

Solution 9 - Android

I generally prefer to add these codes in a function to get the Android version:

int whichAndroidVersion;

whichAndroidVersion= Build.VERSION.SDK_INT;
textView.setText("" + whichAndroidVersion); //If you don't use "" then app crashes.

For example, that code above will set the text into my textView as "29" now.

Solution 10 - Android

i prefer have the version as number to be handeled more easyway than i wrote this:

  public static float getAPIVerison() {

	Float f = null;
	try {
		StringBuilder strBuild = new StringBuilder();
		strBuild.append(android.os.Build.VERSION.RELEASE.substring(0, 2));
		f = new Float(strBuild.toString());
	} catch (NumberFormatException e) {
		Log.e("", "error retriving api version" + e.getMessage());
	}

	return f.floatValue();
}

Solution 11 - Android

I improved code i used

public static float getAPIVerison() {

	float f=1f;
	try {
		StringBuilder strBuild = new StringBuilder();
		strBuild.append(android.os.Build.VERSION.RELEASE.substring(0, 2));
		f= Float.valueOf(strBuild.toString());
	} catch (NumberFormatException e) {
		Log.e("myApp", "error retriving api version" + e.getMessage());
	}

	return f;
}

Solution 12 - Android

Like this:

String versionRelease = BuildConfig.VERSION_NAME;

versionRelease :- 2.1.17

Please make sure your import package is correct ( import package your_application_package_name, otherwise it will not work properly).

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionParth MehtaView Question on Stackoverflow
Solution 1 - Androidol_v_erView Answer on Stackoverflow
Solution 2 - AndroidCommonSenseCodeView Answer on Stackoverflow
Solution 3 - AndroidFalcon165oView Answer on Stackoverflow
Solution 4 - AndroidKhalid TahaView Answer on Stackoverflow
Solution 5 - Androidnikib3roView Answer on Stackoverflow
Solution 6 - AndroidjvdnesteView Answer on Stackoverflow
Solution 7 - AndroidPYKView Answer on Stackoverflow
Solution 8 - AndroidParth MehtaView Answer on Stackoverflow
Solution 9 - AndroidBayView Answer on Stackoverflow
Solution 10 - AndroidalexView Answer on Stackoverflow
Solution 11 - AndroidalexView Answer on Stackoverflow
Solution 12 - AndroidKeshav GeraView Answer on Stackoverflow