android studio中使用Lambda

咱们不是代码的生产者,只是代码的搬运工。

“Lambda 表达式”(lambda expression)是一个匿名函数,Lambda表达式基于数学中的λ演算得名,直接对应于其中的lambda抽象(lambda abstraction),是一个匿名函数,即没有函数名的函数。Lambda表达式可以表示闭包(注意和数学传统意义上的不同)。

JAVA中Lambda表达式

Java 8的一个大亮点是引入Lambda表达式,使用它设计的代码会更加简洁。当开发者在编写Lambda表达式时,也会随之被编译成一个函数式接口。下面这个例子就是使用Lambda语法来代替匿名的内部类,代码不仅简洁,而且还可读。

 1 //没有使用Lambda
 2 button.addActionListener(new ActionListener(){
 3 public void actionPerformed(ActionEvent ae){
 4 System.out.println("Actiondetected");
 5 }
 6 })
 7 //使用Lambda:
 8 button.addActionListener(()->{ 
 9 System.out.println("Actiondetected");
10 });
11 
12 //不采用Lambda的老方法:
13 Runnable runnable1=new Runnable(){
14 @Override
15 public void run(){
16 System.out.println("RunningwithoutLambda");
17 }
18 };
19 //使用Lambda:
20 Runnable runnable2=()->{
21 System.out.println("RunningfromLambda");
22 };

Java 8 Lambda使用更多使用参考

Android Studio使用Lambda

Android Studio项目使用的jdk要为jdk 8或更高版本

修改build.gradle文件,enable jack和设置sourceCompatibility,targetCompatibility为jdk 8

1   defaultConfig {
2     jackOptions {
3       enabled=true
4     }
5   }
6   compileOptions {
7     sourceCompatibility JavaVersion.VERSION_1_8
8     targetCompatibility JavaVersion.VERSION_1_8
9   }

 Note: 插入一个碰到问题:

场景描述: 由于现有项目已经集成APT, 报错: Error:Cannot get property 'options' on null object 

经过查阅资料知: Android Gradle插件2.2版本发布后,官方(Android)提供了annotationProcessor来代替android-apt。同时android-apt作者宣布不再维护,目前android-apt还能正常运行,如果你没支持 jack 编译方式的话,可以继续使用 android-apt。

 1 Android Gradle插件2.2版本发布后,官方(Android)提供了annotationProcessor来代替android-apt。同时android-apt作者宣布不再维护,目前android-apt还能正常运行,如果你没支持 jack 编译方式的话,可以继续使用 android-apt。
 2 
 3 1、android-apt
 4 
 5 1.1、概述
 6 
 7 APT(Annotation Processing Tool)是一种处理注释的工具,它对源代码文件进行检测找出其中的Annotation,根据注解自动生成代码。 Annotation处理器在处理Annotation时可以根据源文件中的Annotation生成额外的源文件和其它的文件(文件具体内容由Annotation处理器的编写者决定),APT还会编译生成的源文件和原来的源文件,将它们一起生成class文件。
 8 1.2、 android-apt的使用
 9 
10 1.2.1、在Project下的build.gradle中添加android-apt配置
11 
12 buildscript {
13  dependencies {
14         classpath 'com.android.tools.build:gradle:2.2.3'
15         classpath 'com.neenbedankt.gradle.plugins:android-apt:1.8'
16     }
17 }
18 
19 1.2.2、 在Module中build.gradle中添加android-apt配置
20 
21 apply plugin: 'com.neenbedankt.android-apt'
22 //23 // apply plugin: 'android-apt'
24 
25 使用apt
26 
27 dependencies {
28      compile 'com.google.dagger:dagger:2.0'
29      apt 'com.google.dagger:dagger-compiler:2.0'
30  }
31 
32 Moudle中完整的配置
33 
34 apply plugin: 'com.neenbedankt.android-apt'
35 
36 dependencies {
37      compile 'com.google.dagger:dagger:2.0'
38      apt 'com.google.dagger:dagger-compiler:2.0'
39  }
40 
41 《android-apt 配置结束》
42 
43 2、annotationProcessor
44 
45 2.1、概述
46 
47 Android Gradle插件2.2版本发布后,Android 官方提供了annotationProcessor来代替android-apt,annotationProcessor支持 javac 和 jack 编译方式,而android-apt只支持 javac 编译方式。
48 2.2、annotationProcessor的使用
49 
50 1、不需要在Project的build.gradle中配置
51 
52  dependencies {
53         classpath 'com.android.tools.build:gradle:2.2.3' // 2.2+
54     }
55 }
56 
57 2、直接在Module中使用
58 
59 dependencies {
60      compile 'com.google.dagger:dagger:2.0'
61      annotationProcessor 'com.google.dagger:dagger-compiler:2.0'
62  }
63 
64 注:annotationPorcessor比起android-apt使用要简单方便些,javac 和 jack 编译方式,而android-apt只支持 javac 方式。
65 
66 《annotationProcessor配置结束》
67 
68 3、android-apt 转 annotationProcessor
69 
70 3.1、去掉Project中build.gradle中的android-apt配置
71 
72 buildscript {
73  dependencies {
74         classpath 'com.android.tools.build:gradle:2.2.3'
75         // classpath 'com.neenbedankt.gradle.plugins:android-apt:1.8'
76     }
77 }
78 
79 3.2、去掉Module中build.gradle中的android-apt的配置(必须)
80 
81 // apply plugin: 'com.neenbedankt.android-apt
82 // apply plugin: 'android-apt'
83 
84 dependencies {
85      compile 'com.google.dagger:dagger:2.0'
86     // apt 'com.google.dagger:dagger-compiler:2.0'
87     annotationProcessor 'com.google.dagger:dagger-compiler:2.0'
88  }
89 
90 4、annotationProcessor 转 android-apt
91 
92 这就比较简单了,就是第3小节的逆转。
相关原文: android-apt和annotationProcessor的使用以及互换

解决办法总结: 

去掉Project和module 中build.gradle 中的android-apt 配置, 将dependencies 中的apt "xxx" 换成 annotationProcessor "xxx"
1 //例子:
2         Runnable runnable = () -> Toast.makeText(this, "abcd", Toast.LENGTH_SHORT).show();
3         runnable.run();
4         btn = (Button) findViewById(R.id.btn);
5         btn.setOnClickListener(v -> Toast.makeText(getApplicationContext(), "test",    Toast.LENGTH_LONG).show());

RxJava使用Lambda

1 Observable.just("this is first string", "this is second")
2                 .compose(this.bindUntilEvent(ActivityEvent.DESTROY)).subscribe(s -> Log.e(TAG, s));
3 
4  Observable.from(Arrays.asList(1, 2, 3, 4, 5))
5                 .filter(integer -> integer % 2 == 0)
6                 .map(integer -> integer * integer)
7                 .subscribeOn(Schedulers.io())
8                 .observeOn(AndroidSchedulers.mainThread())
9                 .subscribe(integer -> Toast.makeText(this, String.valueOf(integer), Toast.LENGTH_SHORT).show());

附(最终代码以及解决思路)

 1     defaultConfig {
 2       jackOptions {
 3         enabled=true
 4       }
 5     }
 6     compileOptions {
 7       sourceCompatibility JavaVersion.VERSION_1_8
 8       targetCompatibility JavaVersion.VERSION_1_8
 9     }
10 
11 若是项目中已配置APT: 去掉Project和module 中build.gradle 中的android-apt 配置, 将dependencies 中的apt "xxx" 换成 annotationProcessor "xxx"
最终成果

致谢: @参考原文 https://www.jianshu.com/p/06935ae67bd3 

@重点问题致谢:  https://blog.csdn.net/u013361668/article/details/73611340

@问题解决1  https://bitbucket.org/hvisser/android-apt/issues/33/support-for-jack-and-jill#comment-None

@问题解决2  https://www.v2ex.com/t/310926

以一颗童心善待生活
原文地址:https://www.cnblogs.com/lizhilin2016/p/9237598.html