Is it possible to use Java 8 for Android development?

后端 未结 26 2033
礼貌的吻别
礼貌的吻别 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条回答
  •  旧时难觅i
    2020-11-21 23:16

    Native Java 8 arrives on android! Finally!

    remove the Retrolambda plugin and retrolambda block from each module's build.gradle file:

    To disable Jack and switch to the default toolchain, simply remove the jackOptions block from your module’s build.gradle file

    To start using supported Java 8 language features, update the Android plugin to 3.0.0 (or higher)

    Starting with Android Studio 3.0 , Java 8 language features are now natively supported by android:

    • Lambda expressions
    • Method references
    • Type annotations (currently type annotation information is not available at runtime but only on compile time);
    • Repeating annotations
    • Default and static interface methods (on API level 24 or higher, no instant run support tho);

    Also from min 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()

    Add these lines to your application module’s build.gradle to inform the project of the language level:

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

    Disable Support for Java 8 Language Features by adding the following to your gradle.properties file:

    android.enableDesugar=false
    

    You’re done! You can now use native java8!

提交回复
热议问题