RequiresApi vs TargetApi android android
What is the difference between RequiresApi
and TargetApi
?
Sample in kotlin:
@RequiresApi(api = Build.VERSION_CODES.M)
@TargetApi(Build.VERSION_CODES.M)
class FingerprintHandlerM() : FingerprintManager.AuthenticationCallback()
NOTE: FingerprintManager.AuthenticationCallback
api requiredM
NOTE 2: if I don't use TargetApi lint with error class requires api level 23...
source to share
Similar to what Mike said, as you can see in the documentation:
Indicates that the annotated element should only be called at this API level or higher.
This is similar to the older @TargetApi annotation, but more clearly states that this is a requirement for the caller, rather than being used to "suppress" warnings in a method that exceeds minSdkVersion.
As you can see here, this actually causes the caller to check the API that was used when calling that method, instead of just removing the warning from your IDE / LINT.
You can compare this with the @NonNull or @Null annotations, they ensure that the caller can / cannot send null values ββto the function.
source to share
From JavaDocs at https://developer.android.com/reference/android/support/annotation/RequiresApi.html :
[@RequiresApi] This is similar to the old @TargetApi annotation, but makes it more clear that this is a requirement for the caller and not for "suppressing" warnings in a method exceeding minSdkVersion.
I believe they are functionally equivalent, but @RequiresApi
seem newer and more likely to be extended to include more functionality.
source to share
Both are designed to handle functionality added to new Android API levels without affecting other API levels.
RequiresApi
@RequiresApi(api = Build.VERSION_CODES.*api_code*)
It says here that the annotated element should only be called at the given API level or higher. An annotated element below this API level will not call.
TargetApi
@TargetApi(Build.VERSION_CODES.*api_code*)
Indicates that Lint should treat this type as targeting a specific API level, regardless of the project's purpose. For the specified API level only. Will not be called on another API level.
source to share