1.前言
java jdk升级到1.8以上以后就可以使用lambda表达式了,其优点就是 高逼格 更简洁,
需要注意的是 使不使用lambda 要看项目需求是否允许。
本文简单介绍 Android Studio gradle-retrolambda插件的安装.
2.在根build.gradle中进行如下配置
buildscript {
repositories {
jcenter()
google()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.3.0'
classpath 'me.tatarka:gradle-retrolambda:3.2.5'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
3.在app/build.gradle中进行如下配置
android {
defaultConfig {
...
}
//lambda 必须 java 1.8
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
}
在以前的版本中 app/build.gradle 还添加了如下配置
apply plugin: 'me.tatarka.retrolambda'
目前会报如下警告
One of the plugins you are using supports Java 8 language features. To try the support built into the Android plugin, remove the following from your build.gradle:
apply plugin: 'me.tatarka.retrolambda'
因此 apply plugin: 'me.tatarka.retrolambda' 是可以省略的,不必再添加到配置中。
备注:配置lambda的其它方式