57

I'm not new to Android and I'm well used to the version handling and how to condition it, but when I see this it troubles me...

// Check if we're running on Android 5.0 or higher
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
    // Call some material design APIs here
} else {
    // Implement this feature without material design
}

On any device pre lollipop this line would crash the app because the Build.VERSION_CODES.LOLLIPOP field does not exists... so why is this in the recommended solution in the documentation?

I'm really wondering what am I missing?

TacB0sS
  • 10,106
  • 12
  • 75
  • 118
  • You should try this: http://stackoverflow.com/a/3995388/1182823 – Adil Malik Dec 23 '14 at 16:12
  • 1
    That variable is available because you are building with a build-tool level that does have it regardless of device level. Those all resolve to ints at compile time anyway – Shooky Dec 23 '14 at 16:12
  • or this: http://stackoverflow.com/a/3993940/1182823 – Adil Malik Dec 23 '14 at 16:13
  • 1
    i think the answer your looking for is this http://stackoverflow.com/a/26941714/2778376 – Spurdow Dec 23 '14 at 16:14
  • So this would not crash, based on other posts your static variables will be replaced with the proper integer when the code is compiled. – Brian S Dec 23 '14 at 16:24

4 Answers4

60

Well in that case use this

// Check if we're running on Android 5.0 or higher
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
    // Call some material design APIs here
} else {
    // Implement this feature without material design
}

Build.VERSION_CODES.LOLLIPOP = 21

Rohit5k2
  • 17,948
  • 8
  • 45
  • 57
  • 2
    @Alexander Zhak answer is coreect... there is no point in the numeric value unless you compile to a lower version and want to handle higher versions – TacB0sS Dec 23 '14 at 16:41
  • is it same as? `@RequiresApi(Build.VERSION_CODES.LOLLIPOP)` – Asif Mushtaq Dec 04 '18 at 10:12
  • @AsifMushtaq: See this https://stackoverflow.com/questions/40007365/requiresapi-vs-targetapi-android-annotations – Rohit5k2 Dec 04 '18 at 14:56
29

Well, you must compile your project with the latest SDK version. Your constants are replaced with corresponding integer values during compilation. No matter what version of Android you run the application on - integers are the same

Alexander Zhak
  • 9,140
  • 4
  • 46
  • 72
16

Try this one

 if (Build.VERSION.SDK_INT > Build.VERSION_CODES.LOLLIPOP_MR1) {
       // Marshmallow+
   }else{
        //below Marshmallow
}

Note: Build.VERSION_CODES.LOLLIPOP_MR1==22

    Build.VERSION_CODES.M==23
Gautam
  • 1,345
  • 12
  • 30
0

Bit a late to answer, but, today I encountered with the same issue on Android Studio 3.4.1

So the workaround is:

Upgrade to the latest Android SDK.

And,

After Marshmallow/Android 6 Build.VERSION_CODES.xxx full names are replaced with initials and some other variations.

So, now for Marshmallow, it will be: Build.VERSION_CODES.M

And for Nougat: Build.VERSION_CODES.N

And so on.

Read more about the build version codes here: Android Developer Reference

factober
  • 1
  • 1
  • 2