Caipivara Caipivara - 2 months ago 29
Android Question

RequiresApi vs TargetApi android annotations

Whats 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
requires api
M


NOTE 2: if I dont use TargetApi lint fail with error
class requires api level 23...

Answer Source

Similar to what Mike said, as you can see in the documentation:

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.

As you can see here, this is actually enforcing the caller to verify the API that's been used when calling into this method, instead of just removing the warning from your IDE/LINT.

You can compare this to the @NonNull or @Null annotations, they enforce that the caller can/can't send null values into the function.