androidandroid-support-librarykotlinandroid-annotations

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...


Solution

  • @RequiresApi - Denotes that the annotated element should only be called on the given API level or higher.

    @TargetApi - Indicates that Lint should treat this type as targeting a given API level, no matter what the project target is.