Is it possible to use Java 8 for Android development?

后端 未结 26 2028
礼貌的吻别
礼貌的吻别 2020-11-21 22:50

Searching the web, it is not clear if Java 8 is supported for Android development or not.

Before I download/setup Java 8, can some one point me at any \"official\" d

26条回答
  •  無奈伤痛
    2020-11-21 23:15

    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'
    }
    

    ORIGINAL POST FROM 2017

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

    Besides that, the try-with-resources support was extended to all Android API levels.

    More Java 8 features are promised to be added in the future.

    To start using supported Java 8 language features, update the Android plugin to 3.0.0-alpha1 (or higher) and add the following to your module’s build.gradle file:

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

    For more details visit:
    https://developer.android.com/studio/write/java8-support.html

提交回复
热议问题