天天看點

【FastDev4Android架構開發】AndroidAnnnotations注入架構介紹和Android Studios基本配置(七)

 轉載請标明出處: 

http://blog.csdn.net/developer_jiangqq/article/details/49468923

 本文出自:【江清清的部落格】

一.簡介(Introduction):

          【好消息】個人網站已經上線運作,後面部落格以及技術幹貨等精彩文章會同步更新,請大家關注收藏:http://www.lcode.org

        AndroidAnnotations是一個能夠讓你快速進行Android開發的開源架構,它可以讓我更加專注于業務功能開發。并且使代碼更加精簡,使項目更加容易維護,它的目标就是“Fast Android Development.Easymaintainance”。相信采用清晰意圖且簡單的代碼來實作我們的功能目标。

它的首頁位址是(http://androidannotations.org/)大家如果有興趣可以去官網看一下,并且首頁他們提供了一個例子,進行原生開發以及使用注入架構開發的代碼對比。例子也可以看到代碼比之前幾乎少寫了一半。這樣看來其中一個優點就不言而喻了直接代碼量減少了很多,便于我們項目開發以及維護。項目架構介紹wiki位址:https://github.com/excilys/androidannotations/wiki。項目源代碼Github位址:https://github.com/excilys/androidannotations.。大家都可以去下載下傳下來學習一下。

Robert C. Martin寫到:

Theratio of time spent reading [code] versus writing is well over 10 to 1[therefore] making it easy to read makes it easier to write.

     官網這樣寫到:當我們在進行開發Android程式的時候,我們常常在想:為什麼我們總是需要一遍又一遍的寫重複的代碼?為什麼我們的應用項目越來越難維護?Context,Activity和對象Object,複雜的線程,難以調用的API,加載的匿名listener,大量不需要的casts….難道我們不能改善這些?

      基于如下項目開發中遇到的各種各樣的問題,我們可以使用今天我們要講得東西。使用java annotations,開發者可以更加專注業務功能開發。讓AndroidAnnotations在編譯的時候來生成運作的代碼。

二.架構特點(Features):

1、使用依賴注入(Dependency Injection)可以注入views,extras,system service,resource等等

2、簡化的線程模型(Simplified  threadingmodel)  ,可以進行annotate方法,讓他們在UI線程上執行或在一個背景線程。

3、事件綁定(Event binding),annotate方法來處理views的事件,這樣我們就不用再寫很多醜陋的匿名監聽器類

4、REST Client  建立一個用戶端接口,AndroidAnnotations生成實作。

5、No Magic  AndroidAnnotations在編譯的時候會産生一個子類(接下來你會明白),你檢視這個子類,可以看到它是如何工作的]

AndroidAnnotations提供了這樣的超強功能,但是整個項目都不超過159KB并且對運作時的性能沒有任何影響。

三.官方代碼示例(CodeSample):

      朋友你寫得Android代碼容易寫,閱讀和維護嗎?來看一下下面的例子:

@EActivity(R.layout.translate) // Sets content view toR.layout.translate
public class TranslateActivity extends Activity{
@ViewById // Injects R.id.textInput
    EditText textInput;
@ViewById(R.id.myTextView) // Injects R.id.myTextView
    TextView result;
@AnimationRes // Injects android.R.anim.fade_in
    Animation fadeIn;
@Click //When R.id.doTranslate button is clicked 
    void doTranslate() {
         translateInBackground(textInput.getText().toString());
    }
@Background // Executed in a background thread
    void translateInBackground(String textToTranslate) {
         String translatedText = callGoogleTranslate(textToTranslate);
        showResult(translatedText);
    }
@UiThread // Executed in the ui thread
    void showResult(String translatedText) {
         result.setText(translatedText);
         result.startAnimation(fadeIn);
    }
// [...]
}
           

看了以上的例子,對于控件初始化,資源擷取,點選事件處理,背景任務處理,線程處理都全部有相關的Annotations,有沒有感覺要實作同樣的功能,以前寫代碼的方式是不是非常的蛋疼,來吧一起跟我來學習Android Annotations的使用。

四.項目配置(Android Studio為例Configuration):

       我們這邊采用Android Studio進行項目開發是以這邊講解Grade配置build方法

配置Android-apt

4.1.在項目全局build.grade進行如下配置

【FastDev4Android架構開發】AndroidAnnnotations注入架構介紹和Android Studios基本配置(七)

4.2.在moudle内部build.grade中進行如下配置

apply plugin: 'com.android.application'
apply plugin: 'android-apt'
defAAVersion = 'XXX'  這邊填寫版本号,現在AndroidAnnotations的釋出版本已經到了3.3.2
dependencies {
    apt "org.androidannotations:androidannotations:$AAVersion"
    compile "org.androidannotations:androidannotations-api:$AAVersion"
}
apt {
    arguments {
        androidManifestFile variant.outputs[0].processResources.manifestFile
        // if you have multiple outputs (whenusing splits), you may want to have other index than 0
// you should set your package namehere if you are using different application IDs
        // resourcePackageName"your.package.name"
// You can set optional annotationprocessing options here, like these commented options:
        // logLevel 'INFO'
        // logFile '/var/log/aa.log'
    }
}
           

     完成以上的步驟操作,那我們項目的AndroidAnnotations環境就已經配置搭建好了,下一篇我們着重進行将注入架構的基本使用。

本注入架構使用例子會加入到正在開發的架構項目中,可以可以去github站點clone下載下傳

https://github.com/jiangqqlmj/FastDev4Android 

同時歡迎大家star和fork整個開源快速開發架構項目~如果有什麼意見和回報,歡迎留言,必定第一時間回複。也歡迎有同樣興趣的童鞋加入到該項目中來,一起維護該項目。

繼續閱讀