Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use Java 8 Stream API under Android 6.0

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

Here 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 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 "Usage of API documented as @since 1.8+ " Gradle is compiling everything but why do I get this message? I thought this feature is introduced with Java 8 and available under Android API Leve 24 and above?

How can I solve this mess?

Thanks in advance

like image 932
Radinator Avatar asked Mar 07 '17 12:03

Radinator


People also ask

Is stream API introduced in Java 8?

Introduced in Java 8, the Stream API is used to process collections of objects. A stream is a sequence of objects that supports various methods which can be pipelined to produce the desired result.

Does Java 8 support streams?

Java 8 offers the possibility to create streams out of three primitive types: int, long and double. As Stream<T> is a generic interface, and there is no way to use primitives as a type parameter with generics, three new special interfaces were created: IntStream, LongStream, DoubleStream.

In which of the following below packages stream API of Java 8 is available?

All the Java Stream API interfaces and classes are in the java. util. stream package.

Can I use Java 8 in Android Studio?

Java 8 language features are now supported by the Android build system in the javac/dx compilation path. Android Studio's Gradle plugin now desugars Java 8 class files to Java 7-compatible class files, so you can use lambdas, method references and other features of Java 8.


Video Answer


1 Answers

Update 2017-04-04

Jack is deprecated, Google is replacing it with something called desugar. It is now available with Android Studio 2.4 preview 4 and later.

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

To use it you'd just set the source and target language levels to Java 8.

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

Note that if you're already using retrolambda or jack, you will need to disable them for desugar to be used. You find more info on how to use it here.

I have not yet tried it myself since I prefer IntelliJ IDEA and quick research into how to use it with IDEA wasn't fruitful. I will update this answer again once I figure out how to use it in IDEA.

Old answer

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

to enable Jack, edit your build.gradle like so

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

More info can be found here

However, I would like to add that I've had pretty bad experience with jack so far, especially when debugging. I would recommend using streamsupport and retrolambda instead for now until jack's sister Jill is released.

like image 145
Zharf Avatar answered Oct 05 '22 02:10

Zharf