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

JavaAndroidApacheJava 8

Java Problem Overview


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 what is keeping us (the android app developers) from improving our code with lambdas, default implementations in interfaces, switching on a String and a lot more. Surely, we will handle for some time, but what if some libraries we use in our apps start to use Java 8 features? As far as I know, this will not work (please correct me if wrong). This creates incompatibility between Standard-Java and Android-Java. It cannot be what Google intends, or at least I can't think of a reason why a company, developing a widely adopted operating system, would wan't to permanently stay with an old Java version.

Questions:

  • Why do they use Apache Harmony?
  • Why can't they adopt a newer version of java?
  • If they don't want Oracle's Java, why can't they use a subset of OpenJDK (licensing noob here)?
  • Do you know of any plans to update the used Java version?
  • Do you know of any way to use Java 8 classes on current Android systems?

Java Solutions


Solution 1 - Java

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

Solution 2 - Java

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

Solution 3 - Java

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestiontilpnerView Question on Stackoverflow
Solution 1 - JavaStephen CView Answer on Stackoverflow
Solution 2 - JavaIdolonView Answer on Stackoverflow
Solution 3 - JavaNikhil KuriakoseView Answer on Stackoverflow