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

Android Studio 配置 AndroidAnnotations 詳解

編輯:關於 Android 程式設計

AndroidAnnotations 是相當好用的注解框架,能大幅精簡樣板程式碼。要在 Android Studio(Gradle)中啟用它,需要修改兩個地方的 build.gradle,以下是我的完整配置。

參考官方文件:
https://github.com/excilys/androidannotations/wiki/Building-Project-Gradle

一、Project 的 build.gradle

在 buildscript 的 dependencies 中加入 android-apt 插件:

buildscript {
    repositories { jcenter() }
    dependencies {
        classpath 'com.android.tools.build:gradle:1.2.2'
        // 應用依賴請放在各模組的 build.gradle,不要寫在這裡
        classpath 'com.neenbedankt.gradle.plugins:android-apt:1.4+'
    }
}
allprojects {
    repositories { jcenter() }
}

二、Module 的 build.gradle

套用 android-apt 插件、加入依賴,並配置 apt 參數:

apply plugin: 'com.android.application'
apply plugin: 'android-apt'

def AAVersion = '3.2+'

android {
    compileSdkVersion 22
    buildToolsVersion "22.0.1"

    defaultConfig {
        applicationId "com.sondon.mayi.hii"
        minSdkVersion 15
        targetSdkVersion 22
        versionCode 1
        versionName "1.0"
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    apt "org.androidannotations:androidannotations:$AAVersion"
    compile "org.androidannotations:androidannotations-api:$AAVersion"
    compile 'com.android.support:appcompat-v7:22.1.1'
    compile 'com.android.support:support-v4:22.1.1'
}

apt {
    arguments {
        androidManifestFile variant.outputs[0].processResources.manifestFile
        // 若使用 splits 有多個輸出,可調整索引
        // 也可設定其他注解處理選項,例如:
        // logLevel 'INFO'
        // logFile '/var/log/aa.log'
    }
}

完成這兩處配置後,重新編譯即可開始享受注解開發的便利。apt 負責編譯期生成程式碼,而 androidannotations-api 則是執行期所需的依賴,兩者缺一不可。

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