Is it possible to use Java 8 for Android development?

后端 未结 26 1978
礼貌的吻别
礼貌的吻别 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

    0 讨论(0)
  • 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!

    0 讨论(0)
  • 2020-11-21 23:16

    Latest news:

    Google announce that with Android N and Android Studio 2.1+, platform will support Java 8. Also stable version of studio 2.1 was released.

    At last we can use lambda expressions. No more list filter in for loop. Horeeey.

    0 讨论(0)
  • 2020-11-21 23:17

    When I asked this question almost 2 years ago the answer really was “officially” no, but as pointed out by ekcr1's answer you can get one of the most highly anticipated features (lambdas) to work if you use retrolamba. At the time I was still using eclipse, as Android Studio was in “preview” mode, so I never did pursue this path.

    Today, I think the “official” answer is still no, and while retrolamba still seems like a good way to go, there is another option for those willing to go down a somewhat “unofficial” route can take, namely Kotlin.

    Today Kotlin reached 1.0.0. For those not familiar with Kotlin, more info can be found at their website found here:

    https://kotlinlang.org

    or watch this utube video of a talk given by Jake Wharton

    https://www.youtube.com/watch?v=A2LukgT2mKc

    0 讨论(0)
  • 2020-11-21 23:19

    All the above solutions doesn't seem to work in 2019 with the latest Android Studio 3.4+.

    I figured out a perfect and up to date solution to migrate or upgrade your Android Project to Java 8.

    Solution: Click on File -> Project Structure -> Modules -> Properties tab.

    Change the Source Compatibility and Target Compatibility to 1.8 (Java 8)

    0 讨论(0)
  • 2020-11-21 23:19

    Yes, you can use Java 8 Language features in Android Studio but the version must be 3.0 or higher. Read this article for how to use java 8 features in the android studio.

    https://bijay-budhathoki.blogspot.com/2020/01/use-java-8-language-features-in-android-studio.html

    0 讨论(0)
提交回复
热议问题