How to use Java 8 Stream API under Android 6.0

Just set up your project using Android Studio 2.2.3 with Java 1.8 and Android 7 (API Level 24) trying to test the "new" java 8 Stream feature.

Here is my gradle file:

 apply plugin: 'com.android.application' android { compileSdkVersion 25 buildToolsVersion "25.0.2" defaultConfig { applicationId "com.example.radinator.myproject" minSdkVersion 24 targetSdkVersion 24 versionCode 1 versionName "1.0" testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" } buildTypes { release { minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' } } } dependencies { compile fileTree(dir: 'libs', include: ['*.jar']) androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', { exclude group: 'com.android.support', module: 'support-annotations' }) compile 'com.android.support:appcompat-v7:25.2.0' testCompile 'junit:junit:4.12' } 

Here is the code:

 import java.util.stream.*; public class MainActivity extens Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_mainactivity); } void foo(){ List<String> myList = Arrays.asList("one", "two", "three", "four", "five", "six"); myList.stream() .filter(s -> s.length() > 0) .collect(Collectors.toList()) } } 

Android Studio underlines the line myList.stream()... telling me that the " Usage of API documented as @since 1.8+ " gradle compiles everything, but why am I getting this message? I thought this feature was introduced with Java 8 and is available under the Android API Leve 24 and above?

How can I solve this mess?

Thank you in advance

+6
source share
1 answer

Update 2017-04-04

Jack is out of date ; Google is replacing it with what is called desugar. Now it is available with Android Studio 2.4 preview 4 and later.

The availability of the Java 8 language / library is still dependent on the level of the API device and the version of Android Studio, so make sure to double check what you can and can’t use .

To use it, you simply set the source and target language levels in Java 8.

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

Please note: if you are already using retrolambda or jack, you need to disable them to use desugar. You will find more information on how to use it here .

I have not tried this myself yet, since I prefer IntelliJ IDEA, and quick research on how to use it with IDEA has not been fruitful. I will update this answer again when I figure out how to use it in IDEA.

Old answer

Some Java 8 language features are available using the Jack compiler.

to enable jack, edit your build.gradle so

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

More information can be found here.

However, I would like to add that so far I have had pretty poor experience with jack, especially when debugging. I would recommend using streamsupport and retrolambda instead until jack jill is released.

+8
source

Source: https://habr.com/ru/post/1015562/


All Articles