8

I was very excited hearing about Google's desugar project since I have to support minSdkVersion 17.

I went ahead and tried a simple Java 8 example:

List<String> myList = Arrays.asList("element1","element2","element3");
myList.forEach(element -> System.out.println(element));

However, Android Studio says Call requires API level 24 (current min is 17): java.lang.Iterable#forEach

Google issued a table about what features are supported. Does their documentation mention whether or not Iterable.forEach() is supported in any minSdkVersion?

OneWorld
  • 17,512
  • 21
  • 86
  • 136

2 Answers2

7

forEach uses a type from java.util.function which is only supported (with desugaring) on API level 24 or higher.

There are libraries that add support for streams. See this answer for more info.

Sartorius
  • 499
  • 5
  • 12
JensV
  • 3,997
  • 2
  • 19
  • 43
  • 1
    Ah ok, [java.util.stream](https://docs.oracle.com/javase/8/docs/api/java/util/stream/package-summary.html) is part of their [list](https://developer.android.com/studio/write/java8-support#supported_features) where it says "requires minSdkVersion >=24". – OneWorld Nov 12 '19 at 14:15
0

Iterable#forEach is supported via desugaring. There's a reference table you can search Java 8+ APIs supported by desugaring: https://developer.android.com/studio/write/java8-support-table.

Just follow the documentation on Java 8+ API desugaring support to set it up.

Aaron He
  • 5,509
  • 3
  • 34
  • 44
  • That seems to be added in Android Gradle Plugin 4.0.0+ according to https://developer.android.com/studio/write/java8-support#supported_features. However, I haven't tested it yet. – OneWorld Mar 25 '21 at 10:32
  • @OneWorld yes! it's AGP 4.0+ added more desugaring. thanks for pointing it out! – Aaron He Mar 25 '21 at 16:21
  • 2
    Yes, according to Google, it appears to be supported with Gradle 4+ but Android Studio is still flagging that it requires at least API 24. – ichalos Jun 19 '21 at 13:09
  • 1
    @ichalos I'm finding the same. I've raised a bug: https://issuetracker.google.com/issues/232808529 – Adam Burley May 16 '22 at 18:01