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
- As of 2016/9/20 these features support in api 15 and aboveuser2577907– user25779072016-09-20 13:51:42 +00:00Commented Sep 20, 2016 at 13:51
- Until 2016/9/20 Android Data binding and Lambda can't work togetheruser2577907– user25779072016-11-04 10:24:54 +00:00Commented Nov 4, 2016 at 10:24
3 Answers
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.
5 Comments
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
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" } }