226

Possible Duplicate:
Programmatically obtain the Android API level of a device?

How do I get the Api level of the phone curently running my application? I am sure its simple but I can not find it as all my searches bring up tons of junk.

divibisan
  • 11,659
  • 11
  • 40
  • 58
Tegra Detra
  • 24,551
  • 17
  • 53
  • 78

4 Answers4

437

Check android.os.Build.VERSION, which is a static class that holds various pieces of information about the Android OS a system is running.

If you care about all versions possible (back to original Android version), as in minSdkVersion is set to anything less than 4, then you will have to use android.os.Build.VERSION.SDK, which is a String that can be converted to the integer of the release.

If you are on at least API version 4 (Android 1.6 Donut), the current suggested way of getting the API level would be to check the value of android.os.Build.VERSION.SDK_INT, which is an integer.

In either case, the integer you get maps to an enum value from all those defined in android.os.Build.VERSION_CODES:

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 Oreo MR1
   28                  P                         Android 9 Pie
   29                  Q                         Android 10
  10000                CUR_DEVELOPMENT           Current Development Version

Note that some time between Android N and O, the Android SDK began aliasing CUR_DEVELOPMENT and the developer preview of the next major Android version to be the same SDK_INT value (10000).

wkl
  • 77,184
  • 16
  • 165
  • 176
  • `android.os.Build.VERSION.SDK` is deprecated in lollipop which means "You can still use this, but don't count on it, because we will probably replace it with something else (or remove it entirely) in future software releases." – Durai Amuthan.H Apr 16 '15 at 14:07
  • 3
    @Duraiamuthan.H - I already mention that if you're using API version 4 or newer, you should switch to using `android.os.Build.VERSION.SDK_INT`. – wkl Apr 16 '15 at 15:23
  • Isn't the update 5.1.1 missing? – JacksOnF1re Nov 04 '15 at 19:12
  • 2
    @JacksOnF1re Android 5.1.1 doesn't have its own API level, it is just a patch version atop version 22 (Lollipop MR1). – wkl Nov 04 '15 at 19:23
  • Oh okay. But how could I possibly distinguish between 5.1 and 5.1.1? Because unfortunately they "patched" functionallity away. – JacksOnF1re Nov 04 '15 at 19:46
  • 2
    @JacksOnF1re If you need the specific release, then you use [`android.os.Build.VERSION.Release`](http://developer.android.com/reference/android/os/Build.VERSION.html#RELEASE) which specifies the exact release you're dealing with. – wkl Nov 04 '15 at 20:24
143
Integer.valueOf(android.os.Build.VERSION.SDK);

Values are:

Platform Version   API Level
Android 9.0        28
Android 8.1        27
Android 8.0        26
Android 7.1        25
Android 7.0        24
Android 6.0        23
Android 5.1        22
Android 5.0        21
Android 4.4W       20
Android 4.4        19
Android 4.3        18
Android 4.2        17
Android 4.1        16
Android 4.0.3      15
Android 4.0        14
Android 3.2        13
Android 3.1        12
Android 3.0        11
Android 2.3.3      10
Android 2.3        9
Android 2.2        8
Android 2.1        7
Android 2.0.1      6
Android 2.0        5
Android 1.6        4
Android 1.5        3
Android 1.1        2
Android 1.0        1

CAUTION: don't use android.os.Build.VERSION.SDK_INT if <uses-sdk android:minSdkVersion="3" />.

You will get exception on all devices with Android 1.5 and lower because Build.VERSION.SDK_INT is since SDK 4 (Donut 1.6).

invisbo
  • 3,399
  • 1
  • 16
  • 20
rude
  • 2,340
  • 2
  • 14
  • 19
  • 1
    There is another trick, you can use it anyway and use reflection to see if SDK_INT exists. If it does not then version is <= 3 and, at least for my purposes, that was good enough. – Robert Massaioli Oct 27 '11 at 07:57
  • Works perfectly. Meanwhile there are a "few" new ones: http://developer.android.com/reference/android/os/Build.VERSION_CODES.html – Stefan Hoth Oct 02 '12 at 13:18
  • 3
    `android.os.Build.VERSION.SDK` is deprecated in lollipop which means "You can still use this, but don't count on it, because we will probably replace it with something else (or remove it entirely) in future software releases." – Durai Amuthan.H Apr 16 '15 at 14:06
  • 2
    @Duraiamuthan.H you are totally right. I don't know anybody who still support devices with API 3 so `android.os.Build.VERSION.SDK_INT` the best option to check version IMO. – rude Apr 22 '15 at 07:16
  • @rude - You are correct.I thought so. – Durai Amuthan.H Apr 22 '15 at 08:15
47

android.os.Build.VERSION.SDK_INT

Here you can find the possible values: VERSION_CODES.

Cristian
  • 198,401
  • 62
  • 356
  • 264
  • 3
    This solution is not so good because this constant is since API Level 4. – Felipe Oct 05 '11 at 00:32
  • @Felipe Android no longer maintains data on devices below API level 15 because it's such a small percentage of devices. Unless you're explicitly targeting very old devices, this solution is fine. – James Riordan Jan 25 '21 at 14:51
-14

try this :Float.valueOf(android.os.Build.VERSION.RELEASE) <= 2.1

Tsung.W
  • 289
  • 1
  • 4
  • 8