Android version check

TacB0sS picture TacB0sS · Dec 23, 2014 · Viewed 59.1k times · Source

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?

Answer

Rohit5k2 picture Rohit5k2 · Dec 23, 2014

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