Why is this Kotlin extension a failed Android Studio Lint validation?

As part of Android development, there is often a time when a block of code must be executed if and only if the device’s SDK is greater than or equal to the minimum version.

To avoid the often ugly and repetitive nature of this code, I created the following extension.

inline fun <T> runWithMinSdk (minVersion: Int, func: () -> T) {
    if (android.os.Build.VERSION.SDK_INT >= minVersion) { func() }
}

Nothing special, and seems to do the trick on paper.

However, when I use this code as such:

fun myFun {
    runWithMinSdk(Build.VERSION_CODES.O) {
        exampleMethodThatRequiresOreo()
    }
}

Android Studio throws hissy seizure and it does not check Lint asking either to suppress the 'NewApi' warnings myFun()or wrapping the runWithMinSdkblock withif android.os.Build.VERSION.SDK_INT ... etc

Is there a better way to achieve this functionality, get it to pass the Lint test, or is it not possible to use extensions?

+4
source share
1

Kotlin, , - , . ,

val func = @TargetApi(26) { exampleMethodThatRequiresOreo() }
runWithMinSdk(Build.VERSION_CODES.0, func)

, . OP , Anko, .

0

Source: https://habr.com/ru/post/1693262/


All Articles