方法一:RetroLambda
- 在项目根目录下的 build.gradle 中加入
classpath 'me.tatarka:gradle-retrolambda:3.6.0'
- 在 module 目录下的 build.gradle 中使用插件,加入
apply plugin: 'me.tatarka.retrolambda'
- 在 module 目录下的 buidle.gradle 的 android 中加入
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
- 最终结果
buildscript {
repositories {
jcenter()
}
dependencies {
...
classpath 'me.tatarka:gradle-retrolambda:3.6.0'
}
}
apply plugin: 'me.tatarka.retrolambda'
...
android {
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
}
方法二:Jack
- 在 module 目录下的 buidle.gradle 的 defaultConfig 中加入
jackOptions {
enabled true
}
- 在 module 目录下的 buidle.gradle 的 android 中加入
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
- 最后结果
android {
...
defaultConfig {
jackOptions {
enabled true
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
}
比较
目前来说 Jack 是 Google 官方给出的支持Java 8特性的方法,但是编译速度很慢非常慢,且暂不支持Instant Run
网友评论