11

In my android code (Kotlin) I am using forEach method of java iterable.

mandatoryViews.forEach { view ->
   // my code here
}

Below is my sdk config in build.gradle (app):

minSdkVersion 23
targetSdkVersion 30

Now my code builds fine. I also tried to run it on API 23 pixel device and everything is working fine.

But after I run gradlew lint to see lint errors, it fails and report gives me this error: Call requires API level 24 (current min is 23): java.lang.Iterable#forEach

lint error

Not able to figure why does lint think it will not work on API 23 whereas on API 23 device my code is running fine.

Nitin Verma
  • 485
  • 4
  • 19

1 Answers1

9

This issue is fixed in lint supplied with Android Gradle plugin 4.2, with 4.2.1 being the latest stable release right now.

Update your project-level build.gradle to have

classpath 'com.android.tools.build:gradle:4.2.1'

in its buildscript.dependencies block.

laalto
  • 150,114
  • 66
  • 286
  • 303
  • Just a note: that issue link requires a Google login. – Tom Jun 19 '21 at 12:23
  • 11
    This issue link indicates this is not resolved as of AGP 7.0.2 even. However, worth noting that replacing `mandatoryViews.forEach` with `mandatoryViews.iterator().forEach` does fix the issue. The root cause is a confusion between the Kotlin `Iterable.forEach` extension function and the Java8 `forEach` function. – Shavi Oct 05 '21 at 12:26
  • 6
    I'm experiencing this issue again even with AGP 7.0.4, the solution seems to be more having 'matching' kotlin and AGP versions rather than about a specific version of AGP being the solution. – Trevor Jun 13 '22 at 22:07
  • I can confirm @Trevor idea. – bmoss Dec 02 '22 at 20:28
  • @Trevor it's true in my case. Increasing AGP to 7.4.2 and Gradle to 7.5.1 make it work for Kotlin version 1.8.21. AGP 7.3.1 and Gradle 7.4 with for Kotlin version 1.8.21. gave error mentioned here – bene25 May 25 '23 at 13:08
  • same issue on agp 8.0.0 – deviant Aug 01 '23 at 13:43