Programmatically obtain the Android API level of a device?
Asked Answered
H

7

112

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?

Hunley answered 4/5, 2010 at 20:37 Comment(0)
P
114

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
Pyoid answered 2/11, 2014 at 11:1 Comment(0)
G
40

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

SDK_INT value        Build.VERSION_CODES        Human Version Name       
    1                  BASE                      Android 1.0 (no codename)
    2                  BASE_1_1                  Android 1.1 Petit Four
    3                  CUPCAKE                   Android 1.5 Cupcake
    4                  DONUT                     Android 1.6 Donut
    5                  ECLAIR                    Android 2.0 Eclair
    6                  ECLAIR_0_1                Android 2.0.1 Eclair                  
    7                  ECLAIR_MR1                Android 2.1 Eclair
    8                  FROYO                     Android 2.2 Froyo
    9                  GINGERBREAD               Android 2.3 Gingerbread
   10                  GINGERBREAD_MR1           Android 2.3.3 Gingerbread
   11                  HONEYCOMB                 Android 3.0 Honeycomb
   12                  HONEYCOMB_MR1             Android 3.1 Honeycomb
   13                  HONEYCOMB_MR2             Android 3.2 Honeycomb
   14                  ICE_CREAM_SANDWICH        Android 4.0 Ice Cream Sandwich
   15                  ICE_CREAM_SANDWICH_MR1    Android 4.0.3 Ice Cream Sandwich
   16                  JELLY_BEAN                Android 4.1 Jellybean
   17                  JELLY_BEAN_MR1            Android 4.2 Jellybean
   18                  JELLY_BEAN_MR2            Android 4.3 Jellybean
   19                  KITKAT                    Android 4.4 KitKat
   20                  KITKAT_WATCH              Android 4.4 KitKat Watch
   21                  LOLLIPOP                  Android 5.0 Lollipop
   22                  LOLLIPOP_MR1              Android 5.1 Lollipop
   23                  M                         Android 6.0 Marshmallow
   24                  N                         Android 7.0 Nougat
   25                  N_MR1                     Android 7.1.1 Nougat
   26                  O                         Android 8.0 Oreo
   27                  O_MR1                     Android 8.1 Oreo MR1
   28                  P                         Android 9 Pie
   29                  Q                         Android 10 Q
  10000                CUR_DEVELOPMENT           Current Development Version
Graig answered 4/5, 2010 at 22:44 Comment(0)
L
12

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)";
        case 27: return  "Oreo"                + "(Android 8.1)";
        case 28: return  "Pie"                 + "(Android 9.0)";
        case 29: return  "Q"                   + "(Android 10.0)";
        case 30: return  "R"                   + "(Android 11.0)";
        default: return  "";
    }
}

Usage

String android_os = getAndroidVersion(android.os.Build.VERSION.SDK_INT);
Lulalulea answered 23/5, 2016 at 10:8 Comment(1)
It's pointless, first you specify a Build limit but provide the case for previous api ... Build.VERSION.RELEASE will give you the numeric version, Build.Version.SDK_INT will give you a better analityc number. For the name, I guess you can use this reflection solution : https://mcmap.net/q/121322/-how-to-find-the-android-version-name-programmaticallyVoiced
C
6

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
Chaste answered 28/1, 2016 at 20:7 Comment(0)
S
4

To get your device current API :

int MyDeviceAPI = Build.VERSION.SDK_INT;
Skepticism answered 7/4, 2015 at 1:20 Comment(0)
H
4

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.

Heterotrophic answered 25/11, 2016 at 16:39 Comment(0)
E
3

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

Elyse answered 19/5, 2015 at 23:21 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.