Does Java 8 work on Android api 24 and above Or you can use in lower api?

冷暖自知 提交于 2019-11-30 01:50:34

问题


In android based on this page Java 8 Languages Features does android work only in api 24 and above or you can use in api lower than api 24 and if you can use this features, which smallest version of api in android support these features


回答1:


Update: Beginning with Android Studio 2.4, the Jack compiler will be deprecated and Java 8 Support will be integrated in the default build chain. Some Java 8 features are available on any API level, some are still limited to API >= 24, see:

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

Old answer:

The Java 8 features are available beginning from API level 9, but only if you use Android Studio 2.1 (preview) and the Android N Preview SDK

http://android-developers.blogspot.de/2016/03/first-preview-of-android-n-developer.html

Improved Java 8 language support - We’re excited to bring Java 8 language features to Android. With Android's Jack compiler, you can now use many popular Java 8 language features, including lambdas and more, on Android versions as far back as Gingerbread. The new features help reduce boilerplate code. For example, lambdas can replace anonymous inner classes when providing event listeners. Some Java 8 language features --like default and static methods, streams, and functional interfaces -- are also now available on N and above. With Jack, we’re looking forward to tracking the Java language more closely while maintaining backward compatibility.




回答2:


Update
The Jack toolchain is deprecated. Java8 features are coming to the standard toolchain if you use the android plugin version 2.4.0-alpha4 (or higher). More info here.

Original answer The Java 8 features are available on API N and newer with exception of lambdas. Lambdas are back-ported (using anonymous classes) back to Gingerbread.

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 this you need Android Studio 2.1 preview, JDK 8 installed and the latest build tools.

Example build config:

android {
    compileSdkVersion 23
    buildToolsVersion "24.0.0 rc1"

    defaultConfig {
        minSdkVersion 23
        targetSdkVersion 23
        versionCode 1
        versionName "1.0"

        jackOptions {
            enabled true
        }
    }

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



回答3:


Update
Beginning with Android Studio 2.4, the Jack compiler will be deprecated.
Android Studio 3.0 and later supports all Java 7 language features and a subset of Java 8 language features that vary by platform version.

All info in the official doc: https://developer.android.com/studio/write/java8-support

You can also configure it directly in the corresponding build.gradle file:

Just configure:

android {
  ...
  // Configure only for each module that uses Java 8
  // language features (either in its source code or
  // through dependencies).
  compileOptions {
    sourceCompatibility JavaVersion.VERSION_1_8
    targetCompatibility JavaVersion.VERSION_1_8
  }
  // For Kotlin projects
  kotlinOptions {
    jvmTarget = "1.8"
  }
}

OLD (answer)
To compile your app against the Android N platform you have to use JAVA 8.

To compile your app against the Android N platform, you need to use the Java 8 Developer Kit (JDK 8), and in order to use some tools with Android Studio 2.1, you need to install the Java 8 Runtime Environment (JRE 8).



来源:https://stackoverflow.com/questions/35934528/does-java-8-work-on-android-api-24-and-above-or-you-can-use-in-lower-api

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!