Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

AndroidAnnotations:讓你的 Android 代碼更簡潔的註解框架

編輯:關於 Android 程式設計

AndroidAnnotations 是一個利用註解來簡化代碼結構、提升開發效率的開源框架。此外,針對 REST 場景,框架還提供了類似 Spring IOC 的機制,非常便捷。

一、代碼對比

使用註解後,原本冗長的 findViewById、setOnClickListener、線程切換等樣板代碼都能被大幅精簡。下面是一個簡單示例:

@EActivity(R.layout.translate) // 將內容視圖設置為 R.layout.translate
public class TranslateActivity extends Activity {

    @ViewById // 注入 R.id.textInput
    EditText textInput;

    @ViewById(R.id.myTextView) // 注入 R.id.myTextView
    TextView result;

    @AnimationRes // 注入 android.R.anim.fade_in
    Animation fadeIn;

    @Click // 當點擊 R.id.doTranslate 按鈕時觸發
    void doTranslate() {
        translateInBackground(textInput.getText().toString());
    }

    @Background // 在後台線程執行
    void translateInBackground(String textToTranslate) {
        String translatedText = callGoogleTranslate(textToTranslate);
        showResult(translatedText);
    }

    @UiThread // 在 UI 線程執行
    void showResult(String translatedText) {
        result.setText(translatedText);
        result.startAnimation(fadeIn);
    }
    // [...]
}

可以看到,視圖注入、事件綁定、線程切換都通過一行註解搞定,代碼可讀性顯著提升。

二、安裝方式

官網:https://androidannotations.org/

下載地址:https://github.com/excilys/androidannotations/wiki/Download

壓縮包裡包含兩個 jar 文件:

  • androidannotations-x.x.x.jar:提供給 Java 的註解處理插件。
  • androidannotations-api-x.x.x.jar:供 Android 工程依賴的運行時包。

在 Eclipse 中配置

以自帶的 HelloWorldEclipse 實例為例:

  1. 在 Eclipse 中導入實例工程 HelloWorldEclipse。
  2. 將 lib 文件夾改名為 libs,把 androidannotations-api-x.x.x.jar 放入,刷新工程後該 jar 會自動加入 build path。
  3. 新建一個 compilers 文件夾,把 androidannotations-x.x.x.jar 拷貝進來備用。
  4. 在工程屬性的 Java Build Path → Libraries 中刪除舊的 api jar。
  5. 在工程屬性的 Java Compiler → Annotation Processing 中啟用 Annotation Processing。
  6. 在 Factory Path 中先刪除舊的 api jar,再把 compilers 文件夾裡的 androidannotations-x.x.x.jar 添加進來。
  7. Eclipse 提示重新編譯時選擇 yes,編譯成功即可。

三、使用 Gradle 編譯

以下是一份參考自網絡的 Gradle 構建腳本:

buildscript {
    repositories {
        mavenLocal()
        mavenCentral()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:0.6.+'
        classpath 'com.googlecode.androidannotations:androidannotations:2.7.1'
        classpath 'com.googlecode.androidannotations:androidannotations-api:2.7.1'
    }
}

repositories {
    mavenCentral()
}

apply plugin: 'android'

dependencies {
    compile fileTree(dir: 'libs', include: '*.jar')
    compile 'com.squareup.dagger:dagger-compiler:1.0.1'
    compile 'com.squareup.dagger:dagger:1.0.1'
    compile 'com.squareup:otto:1.3.3'
}

android {
    compileSdkVersion 8
    buildToolsVersion "19.0.0"

    sourceSets {
        main {
            manifest.srcFile 'AndroidManifest.xml'
            res.srcDirs = ['res']
            assets.srcDirs = ['assets']
        }
        instrumentTest.setRoot('tests')
    }
}

configurations {
    androidannotations
    androidannotations.extendsFrom(compile)
}

dependencies {
    androidannotations 'com.googlecode.androidannotations:androidannotations:2.7.1'
    compile 'com.googlecode.androidannotations:androidannotations-api:2.7.1'
}

android.applicationVariants.each { variant ->
    variant.javaCompile.classpath += configurations.androidannotations
    variant.javaCompile.options.compilerArgs += [
        '-processor', 'com.googlecode.androidannotations.AndroidAnnotationProcessor',
        '-AandroidManifestFile=' + variant.processResources.manifestFile
    ]
}

透過註解處理器在編譯期生成輔助代碼,AndroidAnnotations 能在不改變運行邏輯的前提下,讓 Activity 的編寫變得更加清爽。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved