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

12,378

Solution 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.

Solution 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
    }
}
Share:
12,378
Admin
Author by

Admin

Updated on July 13, 2022

Comments

  • Admin
    Admin almost 2 years

    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

  • tknell
    tknell over 8 years
    The features linked by ProfessorMLM are available beginning from API level 9 if one uses Android Studio 2.1 (preview) and the Android N Preview SDK.
  • tknell
    tknell over 8 years
    No problem, I updated the answer to make it clear which features are available under what circumstances :)
  • Admin
    Admin over 8 years
    Thanks tknell, in reality i can use java 8 with lambda in projects with minSdkVersion is api 15
  • Jules
    Jules over 7 years
    Requiring the Java 8 JDK is not the same thing as supporting Java 8 language features. The quoted text could simply mean that one of the build tools relies on a Java 8 feature, for example.
  • IgorGanapolsky
    IgorGanapolsky over 7 years
    Why do I get an error: cannot resolve method stream(), even after enabling Jack compiler?
  • tknell
    tknell over 7 years
    Did you set the java version in compileOptions and the compileSdkVersion and targetSdkVersion to at least 24 like described here? developer.android.com/guide/platform/j8-jack.html