Is it possible to use Java 8 for Android development?

后端 未结 26 2002
礼貌的吻别
礼貌的吻别 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 22:57

    A subset of Java 8 is supported now on Android Studio. Just make the Source and Target Compatibility adjustments from the window below:

    File --> Project Structure

    More information is given in the below link.

    https://developer.android.com/studio/write/java8-support.html

    0 讨论(0)
  • 2020-11-21 22:58

    Yes, Android Supports Java 8 Now (24.1.17)

    Now it is possible

    But you will need to have your device rom run on java 1.8 and enable "jackOptions" to run it. Jack is the name for the new Android compiler that runs Java 8

    https://developer.android.com/guide/platform/j8-jack.html

    add these lines to build_gradle

        android {
      ...
      defaultConfig {
        ...
        jackOptions {
          enabled true
        }
      }
      compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
      }
    }
    

    Java 8 seem to be the running java engine of Android studio 2.0, But it still does not accept the syntax of java 8 after I checked, and you cannot chose a compiler from android studio now. However, you can use the scala plugin if you need functional programming mechanism in your android client.

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

    Easy way

    You can enable java 1.8 support for android project.

    • Open Project Structure

      • Either by pressing Ctrl + Shift + Alt + S

      • Or File > Project Structure

    • Update the Source Compatibility and Target Compatibility to 1.8 in the Project Structure dialog as shown (click File > Project Structure).

    Or you can use gradle

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

    Sync project. And that's it!

    Note: Java 1.8 support can be enabled for Android Studio 3.0.0 or higher. See Documentation for further reading.

    0 讨论(0)
  • Adding the following fixed the problem for me (Android studio 2.3.2):

    build.gradle (Project)

    buildscript {
    repositories {
        ...
        jcenter()
    }
    dependencies {
        ...
        classpath 'me.tatarka:gradle-retrolambda:3.4.0' // DEPENDENCY
        ...
       }
    }
    

    build.gradle (Module: app)

    apply plugin: 'com.android.application'
    apply plugin: 'me.tatarka.retrolambda' //PLUGIN
    
    android {
        ...
        compileOptions {
            sourceCompatibility JavaVersion.VERSION_1_8
            targetCompatibility JavaVersion.VERSION_1_8
        } // SET JAVA VERSION
        ...
    }
    
    0 讨论(0)
  • 2020-11-21 23:03

    Follow this link for new updates. Use Java 8 language features

    Old Answer

    As of Android N preview release Android support limited features of Java 8 see Java 8 Language Features

    To start using these features, you need to download and set up Android Studio 2.1 and the Android N Preview SDK, which includes the required Jack toolchain and updated Android Plugin for Gradle. If you haven't yet installed the Android N Preview SDK, see Set Up to Develop for Android N.

    Supported Java 8 Language Features and APIs

    Android does not currently support all Java 8 language features. However, the following features are now available when developing apps targeting the Android N Preview:

    Default and static interface methods

    Lambda expressions (also available on API level 23 and lower)

    Repeatable annotations

    Method References (also available on API level 23 and lower)

    There are some additional Java 8 features which Android support, you can see complete detail from Java 8 Language Features

    Update

    Note: The Android N bases its implementation of lambda expressions on anonymous classes. This approach allows them to be backwards compatible and executable on earlier versions of Android. To test lambda expressions on earlier versions, remember to go to your build.gradle file, and set compileSdkVersion and targetSdkVersion to 23 or lower.

    Update 2

    Now Android studio 3.0 stable release support Java 8 libraries and Java 8 language features (without the Jack compiler).

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

    Add this config build.gradle and sync gradle:

    android {
        ...
        defaultConfig {
            ...
            jackOptions {
                enabled true
            }
        }
    
        compileOptions {
            sourceCompatibility JavaVersion.VERSION_1_8
            targetCompatibility JavaVersion.VERSION_1_8
            // or
            //sourceCompatibility 1.8
            //targetCompatibility 1.8
        }
    }
    
    0 讨论(0)
提交回复
热议问题