android studio中使用Lambda

我們不是代碼的生產者,只是代碼的搬運工。java

 

「Lambda 表達式」(lambda expression)是一個匿名函數,Lambda表達式基於數學中的λ演算得名,直接對應於其中的lambda抽象(lambda abstraction),是一個匿名函數,即沒有函數名的函數。Lambda表達式能夠表示閉包(注意和數學傳統意義上的不一樣)。android

JAVA中Lambda表達式

Java 8的一個大亮點是引入Lambda表達式,使用它設計的代碼會更加簡潔。當開發者在編寫Lambda表達式時,也會隨之被編譯成一個函數式接口。下面這個例子就是使用Lambda語法來代替匿名的內部類,代碼不只簡潔,並且還可讀。express

 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或更高版本app

修改build.gradle文件,enable jack和設置sourceCompatibility,targetCompatibility爲jdk 8ide

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。gradle

 1 Android Gradle插件2.2版本發佈後,官方(Android)提供了annotationProcessor來代替android-apt。同時android-apt做者宣佈再也不維護,目前android-apt還能正常運行,若是你沒支持 jack 編譯方式的話,能夠繼續使用 android-apt。
 2 
 3 一、android-apt
 4 
 5 1.1、概述
 6 
 7 APT(Annotation Processing Tool)是一種處理註釋的工具,它對源代碼文件進行檢測找出其中的Annotation,根據註解自動生成代碼。 Annotation處理器在處理Annotation時能夠根據源文件中的Annotation生成額外的源文件和其它的文件(文件具體內容由Annotation處理器的編寫者決定),APT還會編譯生成的源文件和原來的源文件,將它們一塊兒生成class文件。
 8 1.二、 android-apt的使用
 9 
10 1.2.一、在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.二、 在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 三、android-apt 轉 annotationProcessor
69 
70 3.一、去掉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.二、去掉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 四、annotationProcessor 轉 android-apt
91 
92 這就比較簡單了,就是第3小節的逆轉。
相關原文: android-apt和annotationProcessor的使用以及互換

解決辦法總結: ui

去掉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

相關文章
相關標籤/搜索