android Lambda使用入门

今天偶然发现了lambda,以前只是听说过但不知道具体的有点,现在对某国外网友的文章进行简单翻译,公共英语水平请见谅并指正!

原文如下,(或点这里, 需要使用google chrome 并科学上网
1、After Party tricks with RxJava, RxAndroid and Retrolambda, a lot of you folks reached out on an easy and fast way to setup Retrolambda on an android project.
4 easy steps
(1、 体验了RxJava、RxAndroid、Lambda诀窍之后,你许多的分支接触到一个简单迅速的在android项目设置Lambda的套路。简单4步下载导入jdk8,添加如下内容到你的project的build文件中!

Download and install jdk8.

2、Add the following to your project’s main build.gradle
(2、 将如下内容添加到你的(project)级别build文件中

buildscript {
  repositories {
     mavenCentral()
  }

  dependencies {
     classpath 'me.tatarka:gradle-retrolambda:3.2.3'
  }
}

3、Now add this to your application module’s build.gradle
(3、将如下内容添加到你的module级别build文件中

apply plugin: ‘com.android.application’ // or apply plugin: ‘java’
apply plugin: ‘me.tatarka.retrolambda’

4、 Add these lines to your application module’s build.gradle to inform the IDE of the language level:
(4、添加这些行内容到project级别gradle中去制定语言级别(编者注:使用的jdk版本))

android {
        compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
    }

a、You can now start writing shorter code… (a、现在你可以开始写更简短的代码,就像这样)
So things like this:

button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
    log("Clicked");
}
});

Become this:

button.setOnClickListener(v -> log("Clicked"));

b、A few Tips
Build fails when using android-apt.
This is because android-apt modifies gradle’s javaCompile task and this plugin replaces it. Since v2.4.1 this is fixed, you just need to ensure you apply retrolambda last.

(b、当使用android-apt会有几个编译失败的提示。这是因为apt改变gradle的java编译任务,并且这个插件代替了 他,从2.4.1版本修复了这个问题,你只需确保使用最新的Lambda。)
Example:

apply plugin: 'com.android.application'
apply plugin: 'com.neenbedankt.android-apt'
apply plugin: 'me.tatarka.retrolambda' // I'm the last one!

Generated code is incompatible with Retrolambda

c、The conversion can fail if a library contains bytecode that is incompatible with retrolambda (pretty rare). See orfjackal/retrolambda#25 for more information.
If you add this to your application module’s build.gradle you should be fine:
( 如果库包含与lambda不兼容的字节码这个转换可能失败。查看。。。获取更多信息。如果添加如下内容到module的gradle文件中你应该会搞定的。)

retrolambda {
  jvmArgs '-noverify'
}

d、(I personally add this to all my projects as a precaution)
Hit me up on @cesarmcferreira if you have any questions ☺

((我亲自将这些添加到我所有项目中)
如果你有任何问题欢迎在@cesarmcferreira上找我 ☺ )


译者附:
出现 Lambda expressions are not supported at this language level 的解决方案 :http://stackoverflow.com/questions/22703412/java-lambda-expressions-not-supported-at-this-language-level
以上任何方案都没完全解决我的问题,但我通过在报错行使用ALT+ENTER,有两个提示:

我进入了第一个,然后将里面最下方两个版本号都手动输入为1.8,编译后解决问题!用第二个推荐解决应该也是可以的!
关于更多使用方法,我还没进行进一步学习,在androidstudio中配置成功后,写完的代码如果能用lambda表达式代替,他会有提示的,先带大家走到这里 ,谢谢!

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值