简体   繁体   中英

Android Studio Lint Checks for Kotlin Higher Order Functions

I've seen the following example of a higher order function being used as a convenience method to wrap Android API version checks.

fun isLollipop(func: () -> Unit) {
  if (VERSION.SDK_INT >= VERSION_CODES.LOLLIPOP) {
    func()
  }
}

However, in Android Studio 3.0 (Kotlin version 1.2.0) I'm seeing lint errors if I attempt to use an API that requires Lollipop (the minSdk for the project is 17). For example:

isLollipop { appBarLayout.elevation = 4f }

Lint complains about the API minimum on the elevation property.

Just as a side note it doesn't matter if you write the function as an inline fun , lint still gives the same warning. With the inlined function the decompiled Java code is clearly surrounded with the version check.

I found this discussion from a while ago, but it seems like the Kotlin team has already integrated linting support. https://youtrack.jetbrains.com/issue/KT-7729

Will the lint system support looking inside higher order functions?

This issue seems to be resolved in the latest builds of Android Studio 3.2. Tested on Canary 6 with Kotlin runtime 1.2.30, no lint warnings.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM