“Why Apache Harmony” or “How to use Java 8 on Android”

前端 未结 3 1151
悲&欢浪女
悲&欢浪女 2020-12-12 22:42

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

3条回答
  •  感动是毒
    2020-12-12 23:07

    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

提交回复
热议问题