Android项目中使用lambda表达式

app module中使用Lambda

Android项目中使用lambda表达式或Java8新特性,需要在app/build.gradle中添加如下配置

1
2
3
4
5
6
7
8
9
10
11
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中:

1
2
3
jackOptions {
enabled true
}

会报如下错误:

1
Error:Library projects cannot enable Jack. Jack is enabled in default config.

library module中使用Lambda

参考:https://stackoverflow.com/questions/37975128/is-there-way-to-use-java-8-features-with-android-library-project

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
// 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
}
}

Powered by AppBlog.CN     浙ICP备14037229号

Copyright © 2012 - 2020 APP开发技术博客 All Rights Reserved.

访客数 : | 访问量 :