I am writing an Android library. The vast majority of the interface in the lbirary supports Android API level 10 or above. Some functionality, though, requires a higher API
I have recently done this on a custom view class, which needed special constructor for some api levels.
I have done it with the @TargetApi
annotation.
If a method is available only since api level 16:
@TargetApi(16)
public void someMethod () {}
This should do the trick, including lint errors.
There is no need to create your own annotation, the android support library's @RequiresApi annotation is what you are looking for. For example:
@RequiresApi(Build.VERSION_CODES.LOLLIPOP)
public void someMethod() {}
This annotation tells lint to warn if someMethod()
is used in a context that may have a lower API level.
Note that @TargetApi
is different: It's used to assure the linter that the annotated method will only be called with the targeted API, in a situation where it would otherwise warn you not to use that method. So @TargetApi
can be used to silence the lint warning triggered by @RequiresApi
.