app module中使用Lambda
Android项目中使用lambda表达式或Java8新特性,需要在app/build.gradle中添加如下配置
android {
...
defaultConfig {
...
jackOptions.enabled = true
}
compileOptions{
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
}
注:Jack特性只能在
app module中引用,不能在library modlue中enable
如果将下面代码写在modlue/build.gradle中:
jackOptions {
enabled true
}
会报如下错误:
Error:Library projects cannot enable Jack. Jack is enabled in default config.
library module中使用Lambda
// Java8 not fully supported in library projects yet, https://code.google.com/p/android/issues/detail?id=211386
// this is a temporary workaround to get at least lambdas compiling
gradle.projectsEvaluated {
tasks.withType(JavaCompile) {
options.compilerArgs << "-Xbootclasspath/a:" + System.properties.get("java.home") + "/lib/rt.jar"
}
}
android {
...
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
}