Android API level annotation for Android libraries

坚强是说给别人听的谎言 提交于 2019-11-30 08:39:29

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.

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!