RequiresApi vs TargetApi android annotations

前端 未结 5 1421
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-12-02 09:48

Whats the difference between RequiresApi and TargetApi?

Sample in kotlin:

@RequiresApi(api = Build.VERSION_CODES.M)
@Target         


        
5条回答
  •  -上瘾入骨i
    2020-12-02 10:08

    Both of them are for handling feature added to new android API levels without affecting the other API levels.

    RequiresApi

    @RequiresApi(api = Build.VERSION_CODES.*api_code*)
    

    Here it says that the annotated element should only be called on the given API level or higher. The annotated element below the given API level won't call.

    TargetApi

    @TargetApi(Build.VERSION_CODES.*api_code*)
    

    Indicates that Lint should treat this type as targeting a given API level, no matter what the project target is. Only meant for specified API level. Won't called on other API level.

提交回复
热议问题