Most of us have heard by now of the cool features Java 8 will bring, but Android will not support it. This is because of Google using Apache Harmony for Android. This is wha
Do you know of any way to use Java 8 classes on current Android systems?
There are a few libraries which backport parts of Java 8 API (see the update section below for native support of these APIs in the latest Android versions):
java.util.function (functional interfaces) and java.util.stream (streams) API for users of Java 6 or 7 supplemented with selected additions from java.util.concurrent which didn't exist back in Java 6.And you can use retrolambda (along with gradle-retrolambda plugin) to utilize lambdas in Android development.
UPDATE
Android Studio 3.0 started to provide built-in support for some of Java 8 language features, which are:
Also starting from API level 24 the following Java 8 API are available:
java.util.streamjava.util.functionjava.lang.FunctionalInterfacejava.lang.annotation.Repeatablejava.lang.reflect.AnnotatedElement.getAnnotationsByType(Class)java.lang.reflect.Method.isDefault()API level 26 (Android O) added java.time API support.
UPDATE 2020/01/17
Android Studio 4.0 includes support for using a number of Java 8 language APIs, by using technique called desugaring, without requiring a minimum API level for your app:
https://developer.android.com/studio/preview/features#j8-desugar
The following set of APIs is supported in this release:
- Sequential streams (
java.util.stream)- A subset of
java.timejava.util.function- Recent additions to
java.util.{Map,Collection,Comparator}- Optionals (
java.util.Optional,java.util.OptionalIntandjava.util.OptionalDouble) and some other new classes useful with the above APIs- Some additions to
java.util.concurrent.atomic(new methods onAtomicInteger,AtomicLongandAtomicReference)ConcurrentHashMap(with bug fixes for Android 5.0)To support these language APIs, D8 compiles a separate library DEX file that contains an implementation of the missing APIs and includes it in your app. The desugaring process rewrites your app’s code to instead use this library at runtime.
To enable support for these language APIs, include the following in your module’s
build.gradlefile:android { defaultConfig { // Required when setting minSdkVersion to 20 or lower multiDexEnabled true } 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 } } dependencies { coreLibraryDesugaring 'com.android.tools:desugar_jdk_libs:1.0.4' }
More technical details on how desugaring is implemented, can be found in Jake Wharton's articles: