Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
231 views
in Technique[技术] by (71.8m points)

android - Kotlin Extension Functions suddenly require api level 24

I just noticed this lint error:

Call requires API Level 24 (current min is 19) java.util.map#foreach

when I use the extension function forEach on a MutableMap in Kotlin. This didn't happen when I wrote the line, but its there now. And I'm not seeing this error on my other machine.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

What you are using is not kotlin.collections.MutableMap.forEach.

What you are using seems to be Map.forEach in Java 8.

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

This seems to be a common mistake.

Java 8 is well-supported from Android API level 24.

For short, do not use forEach like map.forEach { t, u -> Log.i("tag", t + u) } (this is using Java 8 API, which does not work for Android API level <= 23).
Use it like map.forEach { (t, u) -> Log.i("tag", t + u) } (this is using Kotlin API).
(Not two parameters. Just one parameter that is a pair.)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...