17

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 these features, which most miniature version of API in android support these features

2
  • As of 2016/9/20 these features support in api 15 and above Commented Sep 20, 2016 at 13:51
  • Until 2016/9/20 Android Data binding and Lambda can't work together Commented Nov 4, 2016 at 10:24

3 Answers 3

22

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.

Sign up to request clarification or add additional context in comments.

5 Comments

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.
No problem, I updated the answer to make it clear which features are available under what circumstances :)
Thanks tknell, in reality i can use java 8 with lambda in projects with minSdkVersion is api 15
Why do I get an error: cannot resolve method stream(), even after enabling Jack compiler?
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
6

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 } } 

Comments

-5

Update
Starting 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" } } 

1 Comment

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.