在 Android Studio 中使用 Lambda 表达式的

2017-04-13  本文已影响0人  熊er

方法一:RetroLambda

  1. 在项目根目录下的 build.gradle 中加入
classpath 'me.tatarka:gradle-retrolambda:3.6.0'
  1. 在 module 目录下的 build.gradle 中使用插件,加入
apply plugin: 'me.tatarka.retrolambda'
  1. 在 module 目录下的 buidle.gradle 的 android 中加入
compileOptions {
    sourceCompatibility JavaVersion.VERSION_1_8
    targetCompatibility JavaVersion.VERSION_1_8
}
  1. 最终结果
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

  1. 在 module 目录下的 buidle.gradle 的 defaultConfig 中加入
jackOptions {
      enabled true
}
  1. 在 module 目录下的 buidle.gradle 的 android 中加入
compileOptions {
    sourceCompatibility JavaVersion.VERSION_1_8
    targetCompatibility JavaVersion.VERSION_1_8
}
  1. 最后结果
android {
    ...
    defaultConfig {
        jackOptions {
            enabled true
        }
    }
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
}

比较

目前来说 Jack 是 Google 官方给出的支持Java 8特性的方法,但是编译速度很慢非常慢,且暂不支持Instant Run

上一篇 下一篇

猜你喜欢

热点阅读