@RequiresApi() and @ChecksSdkIntAtLeast() Annotations

One of the most issues I’m perplexed about in Android building is those lint instrument annotations. Those annotations don’t sound like lint instrument annotations to me, however they’re.

So, I name them lint instrument annotations as it makes them transparent to me on their functions which offers you the right kind lint caution/error messages to you in Android Studio.

@RequiresAPI()

Let’s consider you’ve gotten a serve as that calls an Android API that calls for min SDK 23, anything else underneath 23 would possibly not paintings. You annotate @RequiresAPI(23) to your serve as to let the caller is aware of that it would possibly not paintings if the Android instrument is working underneath SDK 23.

@RequiresApi(23)
a laugh callFunctionThatRequiresAPI23() {
   
}

So, you name it from any other serve as.

a laugh callerFunction(){
   callFunctionThatRequiresAPI23()
}

You’ll get this lint error in case you are working on minSdk underneath 23. This mistake message is coming from the @RequiresApi(23) annotation that I’ve within the earlier serve as.

Name calls for API degree 23 (present min is 21): callFunctionThatRequiresAPI23

In case you are on minSdk 23, you will not get this mistake message, and this is sensible as a result of SDK 23 has no drawback working the code. Please word that there isn’t a construct error right here, this can be a lint error.

In line with the instructed repair, you annotate the serve as with @RequiresApi(23) to eliminate the mistake message.

@RequiresApi(23)
a laugh callerFunction(){
   callFunctionThatRequiresAPI23()
}

Neatly, that is fallacious! You should not do that as a result of calling this serve as on a tool that working underneath 23 would possibly not paintings. It defeats the unique function of the mistake message.

As a substitute, you will have to enforce the code for SDK < 23. With out the want to upload @RequiresApi(23), the mistake message is going away too if the next conditional test.

a laugh callerFunction(){
    if (Construct.VERSION.SDK_INT >= 23) {
        callFunctionThatRequiresAPI23()
    } else {
        
    }
}

@ChecksSdkIntAtLeast()

When you extract out Construct.VERSION.SDK_INT >= 23 to a serve as,

a laugh checkSDK23Version():Boolean = Construct.VERSION.SDK_INT >= 23

and speak to it within the callerFunction(),

a laugh callerFunction(){
    if(checkSDK23Version()) {
        callFunctionThatRequiresAPI23()
    } else {
        
    }
}

not anything occurs. The lint instrument makes sense sufficient to determine the checkSDK23Version() is certainly checking for minimal SDK 23 model.

Let’s regulate checkSDK23Version() to be quite no longer easy.

a laugh checkSDK23Version(): Boolean {
    Log.d("vtsen", "do one thing right here")
    go back Construct.VERSION.SDK_INT >= 23
}

The lint instrument in Android Studio now complains of the similar error message once more.

Name calls for API degree 23 (present min is 21): callFunctionThatRequiresAPI23

On this state of affairs, you annotate the serve as with @ChecksSdkIntAtLeast(23) to inform the lint instrument that this checkSDK23Version() is certainly checking for the SDK model.

@ChecksSdkIntAtLeast(23)
a laugh checkSDK23Version():Boolean {
    Log.d("vtsen", "do one thing right here")
    go back Construct.VERSION.SDK_INT >= 23
}

The mistake message is now long past!

Abstract

As a absolute best follow, do not merely upload @RequiresAPI() to eliminate the lint warnings/mistakes. As a substitute, upload the conditional common sense to take care of the unsupported SDK model. At the hand, you’ll safely upload @ChecksSdkIntAtLeast() to eliminate the lint mistakes.

If you do not love to onerous code the construct model code, you’ll test this BuiltUtils Android library out. It’s not anything fancy right here and most likely lacking some assessments that you want. If that is so, be happy to give a contribution. This can be a very beginner-friendly Android library for contribution anyway.

You May Also Like

More From Author

+ There are no comments

Add yours