7

Supported Java 8 Language Features and APIs states that we can use default methods and lambda expressions in android projects with any min sdk API level.

However, the Stream library (java.util.stream) is only supported for API 24 and higher. Can somebody please shed some light on why this is the case? To me it does not make sense, because as far as I understand, the addition of stream API does not require JVM modification unlike the addition of lambdas or default methods; it simply leverages Java 8 features like default methods on collections and adds some new code to java util library?

Stefan Zobel
  • 3,182
  • 7
  • 28
  • 38
AAryz
  • 140
  • 7
  • Could it be that there is **much** more type inferencing going on with streams? – OldCurmudgeon Apr 06 '18 at 14:45
  • 5
    @OldCurmudgeon "much more type inferencing" - quite a bit, but not that much (as backports like [streamsupport](https://github.com/stefan-zobel/streamsupport) demonstrate). I believe one of the main technical hurdles is that it simply wouldn't be feasible to retrofit the java.util Collection API as an afterthought. – Sartorius Apr 06 '18 at 16:45
  • 5
    It’s exactly the opposite, since lambda expressions etc. are desugared, they do not require a special execution environment, as the table on the page you’ve linked shows (Note the “Any” on the right hand side). In contrast, the new APIs require, well, a new API version. – Holger Apr 06 '18 at 17:17
  • 2
    @Holger so essentially the problem would only be updating the devices running < 24 with new core libraries and there is no need to modify ART to use stream API? – AAryz Apr 07 '18 at 17:48
  • 4
    @AAryz *updating the devices with new core libraries*. AFAIK, essentially yes. There is already a 3rd party [library](https://github.com/retrostreams/android-retrostreams) that takes advantage of the way the lambda desugaring works in Android Studio 3.x. The only thing that is missing is an updated java.util Collection API in the core runtime. Technically it would have been possible, though not practical of course. – Sartorius Apr 07 '18 at 23:16
  • 4
    Yes, new or alternative libraries could solve this. In principle, it would also be possible to perform bytecode Instrumentation to redirect all new API uses to a 3rd party library that you ship with your application. Or you use a 3rd party library like the one Sartorius has linked in the first place. – Holger Apr 09 '18 at 06:46

2 Answers2

6

Can somebody please shed some light on why this is the case?

Because Google does not have a time machine. Or, if they are, they are not using it to "retcon" previous versions of Android.

it simply leverages Java 8 features like default methods on collections and adds some new code to java util library?

Correct. However, Google has no means of changing java.util classes on previous versions of Android. Even adding new java.util classes via a library would be a problem.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
1

This was because java.util package is part of the Android.jar i.e. the Android framework classes (like Activity) which are part of the user's phone and is not carried with every app separately.

This is why nowadays Android adds almost all the functionalities as part of the Android X library which can then be supported on all Android versions.

However, with Android Gradle Plugin 4.0+, you can use the stream APIs in older API levels as well.

You will need to add the desugaring library,

dependencies{
  coreLibraryDesugaring 'com.android.tools:desugar_jdk_libs:1.0.9'
}

and also add coreLibraryDesugaringEnabled flag in compileOptions:

compileOptions {
    // Flag to enable support for the new language APIs
    coreLibraryDesugaringEnabled true
    // Sets Java compatibility to Java 8
    sourceCompatibility JavaVersion.VERSION_1_8
    targetCompatibility JavaVersion.VERSION_1_8
  }

This will make sure that the new Java lang APIs get added through that library for APIs less than 24 in your app.

Source: https://developer.android.com/studio/write/java8-support#library-desugaring

shubhamgarg1
  • 1,619
  • 1
  • 15
  • 20