How to Use the Java 8 Stream API on Android API < 24

Is it possible to use the Java 8 Stream API on Android API 24?

[original answer]

You can not use Java8 streams on API level < 24.

However, there are some libraries that backport some of the stream functionality

https://github.com/aNNiMON/Lightweight-Stream-API

https://github.com/konmik/solid

https://sourceforge.net/projects/streamsupport/ (mentioned by @sartorius in comment)

[update k3b 2019-05-23]

https://github.com/retrostreams/android-retrostreams is a spinoff from streamsupport which takes advantage of Android Studio 3.x D8 / desugar toolchain's capability to use interface default & static methods across Jar file boundaries. There are also links to other android-retroXXX ie for CompletableFuture.

[update aeracode 2020-07-24]

Good news, now we can use Java 8 Stream API and more without requiring a minimum API level.

How to use Java 8 Stream API under Android 6.0

Update 2017-04-04

Jack is deprecated, Google is replacing it with something called desugar. It is now available with Android Studio 2.4 preview 4 and later.

Java 8 language/library feature availability is still dependent on the device API level and Android Studio version so make sure to double check what you can and can't use.

To use it you'd just set the source and target language levels to Java 8.

android {
...
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
}

Note that if you're already using retrolambda or jack, you will need to disable them for desugar to be used. You find more info on how to use it here.

I have not yet tried it myself since I prefer IntelliJ IDEA and quick research into how to use it with IDEA wasn't fruitful. I will update this answer again once I figure out how to use it in IDEA.

Old answer

Some Java 8 language features are available when using Jack compiler.

to enable Jack, edit your build.gradle like so

android {
...
defaultConfig {
...
jackOptions {
enabled true
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
}

More info can be found here

However, I would like to add that I've had pretty bad experience with jack so far, especially when debugging. I would recommend using streamsupport and retrolambda instead for now until jack's sister Jill is released.

Why are Java 8 Streams only available from API level 24?

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.

How use java 8 in Android api 21?

Comments by @leonardkraemer and @gabe-sechan cover most of the topic.

For most of the features you pretty much just have to use desugaring and Android Studio 3+. After you set java version, like shown below, Android Studio will start suggesting Java 8 things in your code.

compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}

Streams are not a language feature, but a library, so there's a good port of it: https://github.com/aNNiMON/Lightweight-Stream-API. This library covers java.util.stream and java.util.function.

Is there a way to use Java 8 functional interfaces on Android API below 24?

Not sure if you still need an answer to this question, but others (like myself) might.

As version 3.0, Android Studio natively supports lambda functions and many other Java 8 functions on all API levels, but some (like Functional Interfaces and java.util.function) are still restricted to APIs 24+.

Until that support is expanded, android-retrostreams provides backport support for most of it. This project is an 'upgraded port' of the streamsupport library, which you can also use and has many of the functionalities in android-retrostreams. The streamsupport library supports down to Java 6/7, so you can use it even if you don't have AS 3.0+ or aren't targeting Java 8, but you're probably better off using android-retrostreams in most cases, if you can. You can go through the project's javadocs to see exactly what's offered, but highlights that I've used are java.util.function and java.util.Comparator.

Note that java in the package names is replaced with java9, and some of the class and/or method names may have been changed slightly. For example:

java.util.function becomes java9.util.function,

while

java.util.Comparator becomes java9.util.Comparators (and with slightly different method names and call patterns - but the same functionality).

Java 8 Stream API in Android N

It's not yet in the current preview, but it has already been merged into the AOSP Git master. See here https://android.googlesource.com/platform/libcore/+/916b0af2ccdd1bdfc0283b1096b291c40997d05f

EDIT:

Just to avoid possible confusion: in March 2016, when this question has been asked by the OP, Android N developer preview-1 was the only publicly available build of what is now known as Android 7.0 "Nougat".

The preview-1 build did in fact not include the Java 8 Stream API, but it was added shortly after (one month later) in the developer preview-2 build.

So, Android 7.0 API level 24 does support the Java 8 Stream API now.



Related Topics



Leave a reply



Submit