How to use Java 8 Stream API under Android 6.0

2019-04-26 03:58发布

问题:

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

回答1:

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.