Android API level annotation for Android libraries

前端 未结 2 2055
情深已故
情深已故 2020-12-31 05:10

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

相关标签:
2条回答
  • 2020-12-31 05:37

    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.

    0 讨论(0)
  • 2020-12-31 06:01

    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.

    0 讨论(0)
提交回复
热议问题