"Why Apache Harmony" or "How to use Java 8 on Android"

Update.

Android is planning to use the OpenJDK. Possibly because they are thinking like you and wants to use the Java 8 features. See this


Why do they use Apache Harmony?

Because Sun refused to provide Google with a license for Sun (now Oracle) Java under terms that were acceptable. Google and Sun negotiated, but they ended up walking away from the deal.

Nitpick: in fact, the Android libraries are not Apache Harmony. They started out as based on Harmony, but the two codebases have diverged. Furthermore, the Apache Harmony project was officially "retired" in November 2011.

Why can't they adopt a newer version of java?

Firstly, Android does not run Java(tm). It runs a language that is identical to Java with a class library that is functionally equivalent to a subset of the Java class library (+ Android-specific libraries), on a virtual machine with a different instruction set.

From a technical pespective, they could ... but only if they put in a lot of work into implementing Java 7 and Java 8 language features, library features, etc, for the Android platform.

UPDATE - As of Android 19 (KitKat) and Eclipse ADT 22.6, Android does now support the Java 7 language extensions; see http://tools.android.com/recent/eclipseadt226preview

If they don't want Oracle's Java, why can't they use a subset of OpenJDK?

I don't think that switching to OpenJDK would change anything. Oracle Java and OpenJDK are 99.9% the same.

Anyway, there may be licensing and related legal issues. (And to get a feel for that, read up on the Oracle vs Google lawsuit ... that is going to appeal.)

More likely, Google doesn't see enough commercial value to counteract the (massive) effort it would take to change, and the disruption it would cause to the Android ecosystem ... which already suffers from problems with fragmentation.

Do you know of any plans to update the used Java version?

No I don't. It doesn't mean that there aren't plans, but if there are, they are not public.

Do you know of any way to use Java 8 classes on current Android systems?

You could port them. Or at least, you could try to port them. (Some Java APIs have an intimate relationship with the native code side of the JVM ... and that could make porting problematic.)


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):

  • ThreeTenABP backport of Java 8 date and time API optimised for Android
  • Stream support is a backport of the Java 8 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:

  • Lambda expressions
  • Method references
  • Type Annotations (information is available at compile time, but not at runtime)
  • Repeating annotations
  • Default and static interface methods

Also starting from API level 24 the following Java 8 API are available:

  • java.util.stream
  • java.util.function
  • java.lang.FunctionalInterface
  • java.lang.annotation.Repeatable
  • java.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.time
  • java.util.function
  • Recent additions to java.util.{Map,Collection,Comparator}
  • Optionals (java.util.Optional, java.util.OptionalInt and java.util.OptionalDouble) and some other new classes useful with the above APIs
  • Some additions to java.util.concurrent.atomic (new methods on AtomicInteger, AtomicLong and AtomicReference)
  • 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.gradle file:

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:

  • Android's Java 8 Support
  • Android's Java 9, 10, 11, and 12 Support
  • D8 Library Desugaring