621

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

Laurel
  • 5,522
  • 11
  • 26
  • 49
Parth Mehta
  • 6,297
  • 2
  • 13
  • 5

12 Answers12

1120

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

Siarhei
  • 2,014
  • 2
  • 20
  • 53
ol_v_er
  • 25,694
  • 6
  • 44
  • 61
  • 19
    If the code is running on 1.5, then referencing SDK_INT will throw an exception, since it was introduced with API 4. (Yes, you can run an APK compiled with API 4 code on 1.5. And yes, when that API 4 code is reached, it does throw an exception.) – Programmer Bruce May 14 '11 at 06:31
  • @ProgrammerBruce how to prevent the crash? Or how to _not_ be retro-compatible? – Cœur Dec 28 '18 at 02:24
  • 2
    @Cœur I think building an app retro compatible to Android 1.5 is not really useful nowadays. Just set your project `minSdkVersion` to `4` (Android 1.6) and go on. – ol_v_er Jan 08 '19 at 10:09
  • Why not use the String **android.os.Build.VERSION.SDK** instead? It works for all Android versions – PYK Oct 30 '19 at 01:13
  • @PYK This attribute android.os.Build.VERSION.SDK is deprecated, so it should not be used. https://developer.android.com/reference/android/os/Build.VERSION.html#SDK – ol_v_er Nov 12 '19 at 16:06
163

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
CommonSenseCode
  • 18,848
  • 28
  • 112
  • 163
85
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;
matdev
  • 3,317
  • 4
  • 26
  • 42
Falcon165o
  • 2,735
  • 1
  • 18
  • 31
  • 9
    According to docs `int android.os.Build.VERSION.SDK_INT` and `public static final int SDK_INT` and `Added in API level 4` how could int (not Integer) return `NULL`? `NULL` is a state for an object so its Build or VERSION could be `null` theoretically but in such case not only `SDK_INT` but `RELEASE` as well will cause a NPE. SDK_INT could probably cause "No such method exception" or something like that but not null or NPE. If only custom ROM breaks the docs and the method declared as `public static final Integer SDK_INT`. I'm just trying to determine an issue's nature to make a workaround. – Stan Sep 05 '13 at 08:14
  • 3
    Can anyone verify if this is in fact the case? Stan's comment makes it pretty clear that null is not possible. And custom Android ROM's / OS's surely have to originate from some build of version? So surely Build.VERSION.SDK_INT should reflect this? – B T Oct 15 '14 at 09:20
  • 3
    I've downvoted this since the claim isn't really backed up and the warning about `null` doesn't make sense. – Sam Dec 25 '14 at 22:21
  • Rooted phone or Custom roms should never touch this number. They usually change the Build.VERSION.RELEASE though. – Phuah Yee Keat Aug 05 '16 at 02:16
  • There is a lot of known issues when it comes to Custom OS/Roms. The version I had on my older Android at the time (I want to say GS3) had that issue. There has been a lot of issues with hardcoded values or values there weren't just correct. Hard example, Wi-Fi Mac Addresses being hard coded. – Falcon165o Oct 14 '16 at 12:27
  • If custom ROM developers is purposely changing the Android API signatures, then they're purposely sabotaging those. – Dielson Sales May 06 '20 at 19:33
34

try this:

 if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.GINGERBREAD) {
     // only for gingerbread and newer versions
 }
Khalid Taha
  • 2,727
  • 2
  • 24
  • 40
32

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.

nikib3ro
  • 19,427
  • 21
  • 113
  • 173
9

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.

jvdneste
  • 1,567
  • 1
  • 11
  • 14
5

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

PYK
  • 2,267
  • 18
  • 13
3

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

Taryn
  • 224,125
  • 52
  • 341
  • 389
Parth Mehta
  • 6,297
  • 2
  • 13
  • 5
  • 3
    This will get you the minSdkVersion and targetSdkVersion of the APK, which describes which API versions the app supports. This is not the API version of the phone the app is currently running on. – OldSchool4664 Nov 02 '16 at 22:45
2

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.

Bay
  • 391
  • 4
  • 20
0

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();
}
alex
  • 1
  • 1
-2

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;
}
alex
  • 1
  • 1
-4

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).

desertnaut
  • 46,107
  • 19
  • 109
  • 140
Keshav Gera
  • 8,200
  • 1
  • 56
  • 43