One of many issues I’m confused about in Android improvement is these lint instrument annotations. These annotations don’t sound like lint instrument annotations to me, however they’re.
So, I name them lint instrument annotations as a result of it makes them clear to me on their functions which offers you with the correct lint warning/error messages to you in Android Studio.
@RequiresAPI()
For instance you have got a perform that calls an Android API that requires min SDK 23, something under 23 will not work. You annotate @RequiresAPI(23)
in your perform to let the caller is aware of that it will not work if the Android system is operating under SDK 23.
@RequiresApi(23)
enjoyable callFunctionThatRequiresAPI23() {
}
So, you name it from one other perform.
enjoyable callerFunction(){
callFunctionThatRequiresAPI23()
}
You’re going to get this lint error should you’re operating on minSdk under 23. This error message is coming from the @RequiresApi(23)
annotation that I’ve within the earlier perform.
Name requires API stage 23 (present min is 21):
callFunctionThatRequiresAPI23
If you happen to’re on minSdk 23, you will not get this error message, and this is sensible as a result of SDK 23 has no drawback operating the code. Please observe that there is no construct error right here, it’s a lint error.
Based mostly on the recommended repair, you annotate the perform with @RequiresApi(23)
to eliminate the error message.
@RequiresApi(23)
enjoyable callerFunction(){
callFunctionThatRequiresAPI23()
}
Effectively, that is improper! You should not do that as a result of calling this perform on a tool that operating under 23 will not work. It defeats the unique objective of the error message.
As a substitute, you must implement the code for SDK < 23. With out the necessity to add @RequiresApi(23)
, the error message goes away too if the next conditional test.
enjoyable callerFunction(){
if (Construct.VERSION.SDK_INT >= 23) {
callFunctionThatRequiresAPI23()
} else {
}
}
@ChecksSdkIntAtLeast()
If you happen to extract out Construct.VERSION.SDK_INT >= 23
to a perform,
enjoyable checkSDK23Version():Boolean = Construct.VERSION.SDK_INT >= 23
and name it within the callerFunction(),
enjoyable callerFunction(){
if(checkSDK23Version()) {
callFunctionThatRequiresAPI23()
} else {
}
}
nothing occurs. The lint instrument is sensible sufficient to determine the checkSDK23Version()
is certainly checking for minimal SDK 23 model.
Let’s modify checkSDK23Version()
to be barely not simple.
enjoyable checkSDK23Version(): Boolean {
Log.d("vtsen", "do one thing right here")
return Construct.VERSION.SDK_INT >= 23
}
The lint instrument in Android Studio now complains of the identical error message once more.
Name requires API stage 23 (present min is 21):
callFunctionThatRequiresAPI23
On this situation, you annotate the perform with @ChecksSdkIntAtLeast(23)
to inform the lint instrument that this checkSDK23Version()
is certainly checking for the SDK model.
@ChecksSdkIntAtLeast(23)
enjoyable checkSDK23Version():Boolean {
Log.d("vtsen", "do one thing right here")
return Construct.VERSION.SDK_INT >= 23
}
The error message is now gone!
Abstract
As a finest apply, do not merely add @RequiresAPI()
to eliminate the lint warnings/errors. As a substitute, add the conditional logic to deal with the unsupported SDK model. On the hand, you’ll be able to safely add @ChecksSdkIntAtLeast()
to eliminate the lint errors.
If you happen to do not wish to laborious code the construct model code, you’ll be able to test this BuiltUtils Android library out. It’s nothing fancy right here and doubtless lacking some checks that you simply want. If that’s the case, be at liberty to contribute. It is a very beginner-friendly Android library for contribution anyway.