article

Amelia@Amazon avatar image
Amelia@Amazon posted

How to use Android Version Targeting to determine what APIs to use.

Summary

Allow the version of Android to determine the APIs that are used.

Issue

Some APIs from Lollipop are incompatible with Kitkat and some APIs from Kitkat are incompatible with Lollipop.

Resolution

Set the compileSdkVersion, minSdkVersion, and targetSdkVersion in the build.gradle file of your Android Studio project, then determine which APIs should be used by checking the Android version at runtime.

Sample Code // build.gradle file

android { compileSdkVersion 21 defaultConfig { minSdkVersion 15 targetSdkVersion 21 } } [/pre] [pre] // java file

public void checkAndroidVersion() { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP){ // Code targeting LOLLIPOP and above goes here } else { // Code targeting KITKAT and below goes here } } [/pre]

Keywords: Code Targeting, Manifests, Gradle

KB_0028

androidlollipop
10 |5000

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Article

Contributors

brizzlebrazzle contributed to this article