简体   繁体   English

Android Studio Lint检查Kotlin高阶函数

[英]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. 我已经看到以下高阶函数示例被用作包装Android API版本检查的便捷方法。

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). 但是,在Android Studio 3.0(Kotlin版本1.2.0)中,如果我尝试使用需要Lollipop的API(项目的minSdk为17),我会看到lint错误。 For example: 例如:

isLollipop { appBarLayout.elevation = 4f }

Lint complains about the API minimum on the elevation property. Lint抱怨elevation属性的API最小值。

Just as a side note it doesn't matter if you write the function as an inline fun , lint still gives the same warning. 就像旁注一样,如果你把函数写成inline fun也没关系,lint仍然会给出相同的警告。 With the inlined function the decompiled Java code is clearly surrounded with the version check. 使用内联函数,反编译的Java代码显然被版本检查所包围。

I found this discussion from a while ago, but it seems like the Kotlin team has already integrated linting support. 我刚才发现了这个讨论,但似乎Kotlin团队已经整合了linting支持。 https://youtrack.jetbrains.com/issue/KT-7729 https://youtrack.jetbrains.com/issue/KT-7729

Will the lint system support looking inside higher order functions? lint系统是否支持查看更高阶函数?

This issue seems to be resolved in the latest builds of Android Studio 3.2. 这个问题似乎在Android Studio 3.2的最新版本中得到了解决。 Tested on Canary 6 with Kotlin runtime 1.2.30, no lint warnings. 使用Kotlin运行时1.2.30在Canary 6上测试,没有lint警告。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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