2018 Update: jack is no longer supported. Current Java 8 support howto is available here: https://developer.android.com/studio/write/java8-support.html - Especially useful is the table that shows which parts of Java8 are available at which API Levels.
INFO BELOW IS DEPRECATED
See
Key parts of build.gradle:
android {
compileSdkVersion 24
buildToolsVersion "24.0.1"
...
defaultConfig {
minSdkVersion 24
targetSdkVersion 24
...
jackOptions {
enabled true
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
}
When using Stream apis, got an error "Call requires API Level 24". You can hide that behind @TargetApi(24)
, but then will need an alternative implementation for lower API levels. To use without restrictions, minSdkVersion has to be to 24
After updating build.gradle, make sure in the Project view under external libraries, "Android API 24 Platform" shows up. If not, do Tools->Android->Sync Project with Gradle Files
Was then able to get the following test to pass
@Test
public void testStream() throws Exception {
assertThat(Arrays.asList(1, 3, 4).stream().reduce(0, (i, j) -> i + j), is(8));
}