I am using Kotlin's MutableMap in my Android project. And trying to do some action per 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 just works fine at runtime, but my CI build fails with below lint error:
MyActivity.kt:142: Error: Call requires API level 24 (current min is 16): java.util.Map#forEach [NewApi]
uris.forEach {
~~~~~~~
I know we can't use JDK-8's Map for Android projects until the min sdk is 24. But why lint is considering it as JDK-8's Map?
For further information I tried getting the Java code from Kotlin Bytecode option in AS and found that forEach is replaced with while loop and iterator as expected.
So what could be reason and how to solve this? Any lead will be appreciated.
There are two overloaded foreach
s on Map
:
Kotlin one:
uris.forEach { (key, value) -> // Do some Action }
Java8 one:
uris.forEach { key, value -> // Do some Action }
Confusing, isn't it?
I think this is an import problem. Maybe you have used JDK 8's Map.forEach.
You have to use kotlin.collections.MutableMap.forEach
You can also check this blog:
http://blog.danlew.net/2017/03/16/kotlin-puzzler-whose-line-is-it-anyways/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With