Home » Android » Programmatically obtain the Android API level of a device?

Programmatically obtain the Android API level of a device?

Posted by: admin November 29, 2017 Leave a comment

Questions:

How can I find out which API level my device is using?

The firmware version is 1.6. Does that mean it uses API Level 4?

Answers:

You can obtain API level programatically by the system constant (Build.VERSION.SDK_INT). For example you can run some piece of code which requires newer API in the following way (it will execute if the current device’s API level is at least 4):

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.DONUT) {

}

To obtain user visible Android Version use:

Build.VERSION.RELEASE

Questions:
Answers:

What you need: http://developer.android.com/reference/android/os/Build.VERSION_CODES.html

 Code Name             Version      Api level

(no code name)         1.0          API level 1

(no code name)         1.1          API level 2

Cupcake                1.5          API level 3, NDK 1

Donut                  1.6          API level 4, NDK 2

Eclair                 2.0          API level 5

Eclair                 2.0.1        API level 6

Eclair                 2.1          API level 7, NDK 3

Froyo                  2.2.x        API level 8, NDK 4

Gingerbread         2.3 - 2.3.2     API level 9, NDK 5

Gingerbread         2.3.3 - 2.3.7   API level 10

Honeycomb              3.0          API level 11

Honeycomb              3.1          API level 12, NDK 6

Honeycomb             3.2.x         API level 13

Ice Cream Sandwich 4.0.1 - 4.0.2    API level 14, NDK 7

Ice Cream Sandwich 4.0.3 - 4.0.4    API level 15, NDK 8

Jelly Bean           4.1.x          API level 16

Jelly Bean           4.2.x          API level 17

Jelly Bean           4.3.x          API level 18

KitKat         4.4 - 4.4.2          API level 19

KitKat (for wearable)4.4            API level 20

Lollipop             5.0            API level 21

Lollipop             5.1            API level 22

Marshmallow          6.0            API level 23

Nougat               7.0            API level 24

Nougat               7.1.1          API level 25

Oreo                 8.0            API level 26

Questions:
Answers:

For API Level >= 4 you can use Build.VERSION.SDK_INT

private String getAndroidVersion(int sdk) {
    switch (sdk) {
        case 1: return "Android 1.0";
        case 2: return "Petit Four (Android 1.1)";
        case 3: return "Cupcake (Android 1.5)";
        case 4: return "Donut (Android 1.6)";
        case 5: return "Eclair (Android 2.0)";
        case 6: return "Eclair (Android 2.0.1)";
        case 7: return "Eclair (Android 2.1)";
        case 8: return "Froyo (Android 2.2)";
        case 9: return "Gingerbread (Android 2.3)";
        case 10: return "Gingerbread (Android 2.3.3)";
        case 11: return "Honeycomb (Android 3.0)";
        case 12: return "Honeycomb (Android 3.1)";
        case 13: return "Honeycomb (Android 3.2)";
        case 14: return "Ice Cream Sandwich (Android 4.0)";
        case 15: return "Ice Cream Sandwich (Android 4.0.3)";
        case 16: return "Jelly Bean (Android 4.1)";
        case 17: return "Jelly Bean (Android 4.2)";
        case 18: return "Jelly Bean (Android 4.3)";
        case 19: return "KitKat (Android 4.4)";
        case 20: return "KitKat Watch (Android 4.4)";
        case 21: return "Lollipop (Android 5.0)";
        case 22: return "Lollipop (Android 5.1)";
        case 23: return "Marshmallow (Android 6.0)";
        case 24: return "Nougat (Android 7.0)";
        case 25: return "Nougat (Android 7.1.1)";
        case 26: return "Oreo (Android 8.0)";
        default: return "";
    }
}

Usage

String android_os = getAndroidVersion(android.os.Build.VERSION.SDK_INT);

Questions:
Answers:

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 samsung 
                model SM-T310 
                version 19 
                versionRelease 4.4.2

Questions:
Answers:

To get your device current API :

int MyDeviceAPI = Build.VERSION.SDK_INT;

Questions:
Answers:

in first versions was used Build.VERSION.SDK

 String myAPI= Build.VERSION.SDK;

but now is deprecated!.

SDK This field was deprecated in API level 4. Use SDK_INT to easily
get this as an integer.

now you have to use: SDK_INT Added in API level 4 :

  int myAPI = Build.VERSION.SDK_INT;

SDK_INT The user-visible SDK version of the framework; its possible
values are defined in Build.VERSION_CODES.

more info:

http://developer.android.com/reference/android/os/Build.VERSION.html

Questions:
Answers:

You can get device’s information, as following.

Log.d(TAG, "RELEASE : " + Build.VERSION.RELEASE);
Log.d(TAG, "SDK     : " + Build.VERSION.SDK_INT);

and can get the Android version code history from OS.

for (Field field : Build.VERSION_CODES.class.getDeclaredFields()) {
  field.setAccessible(true);
  Log.d(TAG, field.get(this) + " -> " + field.getName());
}

This is output.

RELEASE : 5.1.1
SDK     : 22

1 -> BASE
2 -> BASE_1_1
3 -> CUPCAKE
10000 -> CUR_DEVELOPMENT
4 -> DONUT
5 -> ECLAIR
6 -> ECLAIR_0_1
7 -> ECLAIR_MR1
8 -> FROYO
9 -> GINGERBREAD
10 -> GINGERBREAD_MR1
11 -> HONEYCOMB
12 -> HONEYCOMB_MR1
13 -> HONEYCOMB_MR2
14 -> ICE_CREAM_SANDWICH
15 -> ICE_CREAM_SANDWICH_MR1
16 -> JELLY_BEAN
17 -> JELLY_BEAN_MR1
18 -> JELLY_BEAN_MR2
19 -> KITKAT
20 -> KITKAT_WATCH
21 -> L
21 -> LOLLIPOP
22 -> LOLLIPOP_MR1

so, combine these and official version list.

API VERSION_CODE           VERSION       CODE_NAME 
1   BASE                   1             (no code name)
2   BASE_1_1               1.1           (no code name)
3   CUPCAKE                1.5           Cupcake
4   DONUT                  1.6           Donut
5   ECLAIR                 2             Eclair
6   ECLAIR_0_1             2.0.1         Eclair
7   ECLAIR_MR1             2.1           Eclair
8   FROYO                  2.2.x         Froyo
9   GINGERBREAD            2.3 - 2.3.2   Gingerbread
10  GINGERBREAD_MR1        2.3.3 - 2.3.7 Gingerbread
11  HONEYCOMB              3             Honeycomb
12  HONEYCOMB_MR1          3.1           Honeycomb
13  HONEYCOMB_MR2          3.2.x         Honeycomb
14  ICE_CREAM_SANDWICH     4.0.1 - 4.0.2 Ice Cream Sandwich
15  ICE_CREAM_SANDWICH_MR1 4.0.3 - 4.0.4 Ice Cream Sandwich
16  JELLY_BEAN             4.1.x         Jelly Bean
17  JELLY_BEAN_MR1         4.2.x         Jelly Bean
18  JELLY_BEAN_MR2         4.3.x         Jelly Bean
19  KITKAT                 4.4 - 4.4.4   KitKat
20  KITKAT_WATCH           4.4W          KitKat Watch
21  LOLLIPOP               5             Lollipop
22  LOLLIPOP_MR1           5.1           Lollipop
23  M                      6             Marshmallow
24  N                      7             Nougat
25  N_MR1                  7.1           Nougat

This says, a version_code of API25 is N_MR1.