When your code requires a certain API level, why should the check for Build.Version be the same as the required level?

I was watching Android developer videos ( https://www.youtube.com/watch?v=3UbJhmkeSig ) on property animations when I ran into a problem that is easy to fix but difficult for I figured out and hoped someone could light up its for me ...

I have a project that is targeting API level 8 in the manifest, so when I used the code below, I got an error stating that the code being used requires a higher API level than the current API level. While working, I checked the Build version at runtime ( http://developer.android.com/training/basics/supporting-devices/platforms.html ) and only executed the code segment if the API level was sufficient.

        ObjectAnimator rotateAnimation = ObjectAnimator.ofFloat(resp1, View.ROTATION, 270);


My confusion came when I added the first, outer outer if statement. Line of code

 ObjectAnimator rotateAnimation = ObjectAnimator.ofFloat(resp1, View.ROTATION, 270);


API level 14 is required hence my reason for checking Build.Version.ICS. However, the "setRepeatCout ()" method from the line of code



requires API 11 and although it is placed inside the first outer outer operator statement with parentheses to enforce locking and not a string, my IDE (IntelliJ IDEA) was still giving me errors stating that a string of code requires API 11 and my project is targeting API 8. My two solutions were to put the code in two nested if statements checking API 14 and another API 11, or to use



at the beginning of the method. However, I do not fully understand the annotation as to how it affects older devices, and I do not understand why I need two if statements to check the assembly version.

-Does @TargetAPI just gets rid of the string validation, or does it somehow affect the whole method and whether it compiles / works or works?

-Why did I have to use two if statements? Why wasn't the first if statement sufficient for both lines of code?

- Is there a reason to check the assembly using both if statements, or is this possibly a bug? I tried to find known bugs at https://youtrack.jetbrains.com/issues/IDEA and google search results.

Many thanks


source to share

1 answer

Could it @TargetAPI

get rid of the lint check or somehow affect the whole method and whether or not it compiles / executes?

This indicates to Lint that for the scope of the annotation (method or class), Lint should be treated minSdkVersion

as the value specified in the annotation (for example HONEYCOMB

), not what you have specified in your manifest or file build.gradle


You apply @TargetAPI

when you, as a developer, have confirmed correct backward compatibility handling for the specified API level. Then, some time in the future, if you add more code to a class or method that is higher than that API level, this Lint should yell at you again, so you realize that you need to add additional backward compatibility checks.

In this case, the correct solution was @TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH)

, since your code is set to be backward compatible with the current one minSdkVersion

up to API level 14, but not higher (since you are not using anything of the above).

Why did I need to use two if statements? Why wasn't the first if statement sufficient for both lines of code?

Possibly because of your choice HONEYCOMB

for annotation @TargetAPI

. If we are on a device with API level 14 or higher, we are on a device with API level 11 or higher by definition. You don't need an internal review if


Is there a need to check the assembly in both locations or is this possibly a bug?

I don't know what you think "both places". Do you need annotation @TargetAPI

and external validation if

? Yes. Do you need both checks if

? Not. And if you install @TargetAPI


, remove the internal check if

and get Lint complaints, possibly a bug in the Android plugin.



All Articles