美文网首页
在 Android Studio 中使用 Lambda 表达式的

在 Android Studio 中使用 Lambda 表达式的

作者: 熊er | 来源:发表于2017-04-13 11:14 被阅读0次

方法一: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

相关文章

网友评论

      本文标题:在 Android Studio 中使用 Lambda 表达式的

      本文链接:https://www.haomeiwen.com/subject/azfbattx.html