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

Android Studio 添加 AndroidAnnotations 配置指南

編輯:關於 Android 程式設計

本文說明如何在 Android Studio 專案中整合 AndroidAnnotations 框架。整個設定分為兩個部分:專案層級與模組層級的 Gradle 配置。

1. 專案層級 Gradle 設定

在 project 的 build.gradle 檔案中,於 dependencies 區塊加入 android-apt 外掛:

dependencies {
    classpath 'com.android.tools.build:gradle:1.0.0'
    classpath 'com.neenbedankt.gradle.plugins:android-apt:1.4'

    // 注意:不要把應用程式本身的依賴放在這裡,
    // 它們屬於個別模組的 build.gradle 檔案。
}

2. 模組層級 Gradle 設定

在 module 的 build.gradle 檔案中,加入 AndroidAnnotations 相關依賴,並設定 apt 參數:

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.android.support:appcompat-v7:22.0.0'
    compile files('libs/wmqtt.jar')
    compile files('libs/gson-2.2.4.jar')
    compile files('libs/org.eclipse.paho.client.mqttv3-1.0.2.jar')

    apt "org.androidannotations:androidannotations:3.2"
    compile 'org.androidannotations:androidannotations-api:3.2'

    compile 'com.android.support:recyclerview-v7:21.0.3'
    compile 'com.android.support:cardview-v7:21.0.3'
    compile 'com.facebook.fresco:fresco:0.2.0'
}

apt {
    arguments {
        androidManifestFile variant.outputs[0].processResources.manifestFile
        resourcePackageName 'com.example'
    }
}

完成以上兩處設定後,同步 Gradle 即可在專案中使用 AndroidAnnotations 的註解功能,大幅簡化 View 綁定與事件處理等常見操作。

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