5

to suppress API level warning, I usually prefer to use @RequiresApi, than @TargetApi.

As, @RequresApi seems newer and better than @TargetApi, according to RequiresApi vs TargetApi android annotations

But, is there any real use case, where we can solve using @RequiresApi, but not using @TargetApi?

Cheok Yan Cheng
  • 47,586
  • 132
  • 466
  • 875

1 Answers1

1

@RequiresApi is more clearly, as described in its doc.


Moreover, @RequiresApi has FILED Target:

@Retention(CLASS)
@Target({TYPE,METHOD,CONSTRUCTOR,FIELD})
public @interface RequiresApi {
...

while @TargetApi not:

@Target({TYPE, METHOD, CONSTRUCTOR})
@Retention(RetentionPolicy.CLASS)
public @interface TargetApi {
...

So, @RequiresApi can be used like:

@RequiresApi(api = xxx)
private Foo bar;
zwh
  • 172
  • 2
  • 12