How to get current Android API level in code

I’m working on an Android app and need to check what API level the device is running. This is important because I want to use certain features that are only available in newer versions of Android. I tried looking through the documentation but couldn’t find a clear way to do this programmatically. Does anyone know how to retrieve the current Android API version from within my application? I need this information to make decisions about which features to enable or disable based on the device’s capabilities. Any code examples would be really helpful since I’m still learning Android development.

You can use Build.VERSION.SDK_INT to retrieve the current Android API level, which returns an integer value. To determine if certain features are supported, compare this value with the constants defined in Build.VERSION_CODES. For instance, use the condition if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) to check if the API level is 21 or higher. Also, ensure to include import android.os.Build at the beginning of your code.

quick tip - you can also use Build.VERSION.CODENAME to get the development codename like “REL” for release builds. i always create a helper method for api level checks instead of scattering them throughout my code. way cleaner and easier to maintain.

Adding to JackWolf69’s point - I’ve been building Android apps for years and there’s another option worth considering. Use Build.VERSION.RELEASE when you need the actual version string (“11” or “12”) instead of the numeric API level. But for feature detection, stick with SDK_INT.

Here’s what I learned the hard way: don’t just check API levels. Always handle backwards compatibility gracefully and wrap newer API calls in try-catch blocks when you can. Manufacturers love customizing Android in weird ways, so even when the API level says a feature exists, it might not work as expected.