NewApi error : Finds API accesses to APIs that are not supported in all targeted API versions


❗️ NewApi error: Finds API accesses to APIs that are not supported in all targeted API versions

The above lint error message is displayed for your Android project when you use a method form an API that is not supported by all the Android APIs levels that you target.

Solution

You can either change the minSdkVersion and targetSdkVersion in your AndroidManifest.xml for unsupported APIs.

If you want to support all versions that you have stated in AndroidManifest then you need to use "Conditional execution" i.e. add conditions in your code based on the API level.

if (android.os.Build.VERSION.RELEASE.startsWith("2.")  {
  //Some code
}
else if (android.os.Build.VERSION.RELEASE.startsWith("3.")  {
  //Some code
}
 else {
  //Some code
 }

You can also use @TargetApi annotation to set the SDK level to apply to your code, example

@TargetApi(18)
public class DoSomething {
   //You Code
}

The above code will consider API level 18 rather than what is being defined in the AndroidManifest.xml file. But not that you cannot use @TargetApi for a method.

✌️ You can use tools:targetApi="" in the Android XML files to support certain API levels.