Android version check

androidandroid-version

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?

Best Solution

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