Lint considers Kotlin MutableMap.forEach () to be java.util.Map.forEach ()

I am using Kotlin MutableMap in my Android project. And an attempt to do some action for each item. So here is my code.

private val uris: MutableMap<String, Uri> = mutableMapOf()
// ... Fill the items here ...
uris.forEach {
    val ref = FirebaseFirestore.getInstanse().reference
    uploadFile(ref, it.value)
}

Everything works fine at runtime, but my CI build failed with an error below lint:

MyActivity.kt:142: Error: Call requires API level 24 (current min is 16): java.util.Map#forEach [NewApi]
              uris.forEach {
                   ~~~~~~~

I know that we cannot use the JDK-8 Map for Android projects until the min sdk is 24. But why does lint treat this as a JDK-8 map?

For more information, I tried to get Java code from the Kotlin Bytecode option in AS and found that forEach is being replaced by a while loop and an iterator, as expected.

So what could be the reason and how to solve it? Any guidance would be appreciated.

+4
source share
3 answers

foreach Map:

Kotlin one:

uris.forEach { (key, value) -> // Do some Action }

Java8 one:

uris.forEach { key, value -> // Do some Action }

, ?

+1

, . , JDK 8 Map.forEach.

kotlin.collections.MutableMap.forEach

:

http://blog.danlew.net/2017/03/16/kotlin-puzzler-whose-line-is-it-anyways/

+1

, forEach Kotlin? forEach, Kotlin Map.Entry Java : . , . forEach , , Kotlin one ( it ).

Kotlin forEach , Android Issue Tracker

0
source

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


All Articles