RequiresApi 与 TargetApi android 注释
RequiresApi vs TargetApi android annotations
RequiresApi
和TargetApi
有什么区别?
kotlin 示例:
@RequiresApi(api = Build.VERSION_CODES.M)
@TargetApi(Build.VERSION_CODES.M)
class FingerprintHandlerM() : FingerprintManager.AuthenticationCallback()
注意:FingerprintManager.AuthenticationCallback
需要 api M
注意 2:如果我不使用 TargetApi lint 会失败并出现错误 class requires api level 23...
来自 https://developer.android.com/reference/android/support/annotation/RequiresApi.html 中的 JavaDocs:
[@RequiresApi] This is similar in purpose to the older @TargetApi annotation, but more clearly expresses that this is a requirement on the caller, rather than being used to "suppress" warnings within the method that exceed the minSdkVersion.
我想它们在功能上是等同的,但 @RequiresApi
似乎更新并且更有可能被扩展以包含更多功能。
与Mike所说的类似,如您在文档中所见:
Denotes that the annotated element should only be called on the given API level or higher.
This is similar in purpose to the older @TargetApi annotation, but more clearly expresses that this is a requirement on the caller, rather than being used to "suppress" warnings within the method that exceed the minSdkVersion.
正如你在这里看到的,这实际上是强制调用者验证调用此方法时使用的 API,而不是仅仅从你的 IDE/LINT 中删除警告。
您可以将其与@NonNull 或@Null 注释进行比较,它们强制调用者can/can不向函数发送空值。
@RequiresApi
- 表示带注释的元素只能在给定的 API 级别或更高级别上调用。
@TargetApi
- 表示 Lint 应将此类型视为针对给定的 API 级别,无论项目目标是什么。
它们都用于处理添加到新 android API 级别的功能,而不影响其他 API 级别。
RequiresApi
@RequiresApi(api = Build.VERSION_CODES.*api_code*)
这里说注释的元素只能在给定的 API 级别或更高级别上调用。给定 API 级别以下的注释元素将不会调用。
TargetApi
@TargetApi(Build.VERSION_CODES.*api_code*)
表示 Lint 应将此类型视为针对给定的 API 级别,无论项目目标是什么。仅适用于指定的 API 级别。
不会调用其他 API 级别。
我首先假设您的最低 api 版本低于您要调用的 api,因为这就是这些注释有意义的地方
@RequiresApi(Build.VERSION_CODES.N_MR1)
public void hello() { // codes that call system apis introduced in android N_MR1}
当一个方法用这个注释时,无论何时调用该方法,您都会收到一个漂亮的红色警告,提示此调用需要 api 版本高于您的最低 api 版本,但它不会阻止您编译和构建您的 apk,它只会在我测试的 android 的较低版本上崩溃。
@TargetApi
这根本没有帮助,它抑制了在你的方法中调用新 apis 的警告,但是当你从其他地方调用这个方法时,根本没有 lint 警告,你可以仍然构建和安装你的 apk 只是为了在调用该方法时遇到崩溃。
RequiresApi
和TargetApi
有什么区别?
kotlin 示例:
@RequiresApi(api = Build.VERSION_CODES.M)
@TargetApi(Build.VERSION_CODES.M)
class FingerprintHandlerM() : FingerprintManager.AuthenticationCallback()
注意:FingerprintManager.AuthenticationCallback
需要 api M
注意 2:如果我不使用 TargetApi lint 会失败并出现错误 class requires api level 23...
来自 https://developer.android.com/reference/android/support/annotation/RequiresApi.html 中的 JavaDocs:
[@RequiresApi] This is similar in purpose to the older @TargetApi annotation, but more clearly expresses that this is a requirement on the caller, rather than being used to "suppress" warnings within the method that exceed the minSdkVersion.
我想它们在功能上是等同的,但 @RequiresApi
似乎更新并且更有可能被扩展以包含更多功能。
与Mike所说的类似,如您在文档中所见:
Denotes that the annotated element should only be called on the given API level or higher.
This is similar in purpose to the older @TargetApi annotation, but more clearly expresses that this is a requirement on the caller, rather than being used to "suppress" warnings within the method that exceed the minSdkVersion.
正如你在这里看到的,这实际上是强制调用者验证调用此方法时使用的 API,而不是仅仅从你的 IDE/LINT 中删除警告。
您可以将其与@NonNull 或@Null 注释进行比较,它们强制调用者can/can不向函数发送空值。
@RequiresApi
- 表示带注释的元素只能在给定的 API 级别或更高级别上调用。
@TargetApi
- 表示 Lint 应将此类型视为针对给定的 API 级别,无论项目目标是什么。
它们都用于处理添加到新 android API 级别的功能,而不影响其他 API 级别。
RequiresApi
@RequiresApi(api = Build.VERSION_CODES.*api_code*)
这里说注释的元素只能在给定的 API 级别或更高级别上调用。给定 API 级别以下的注释元素将不会调用。
TargetApi
@TargetApi(Build.VERSION_CODES.*api_code*)
表示 Lint 应将此类型视为针对给定的 API 级别,无论项目目标是什么。仅适用于指定的 API 级别。 不会调用其他 API 级别。
我首先假设您的最低 api 版本低于您要调用的 api,因为这就是这些注释有意义的地方
@RequiresApi(Build.VERSION_CODES.N_MR1)
public void hello() { // codes that call system apis introduced in android N_MR1}
当一个方法用这个注释时,无论何时调用该方法,您都会收到一个漂亮的红色警告,提示此调用需要 api 版本高于您的最低 api 版本,但它不会阻止您编译和构建您的 apk,它只会在我测试的 android 的较低版本上崩溃。
@TargetApi
这根本没有帮助,它抑制了在你的方法中调用新 apis 的警告,但是当你从其他地方调用这个方法时,根本没有 lint 警告,你可以仍然构建和安装你的 apk 只是为了在调用该方法时遇到崩溃。