1. 程式人生 > >Android Studio : 配置 AndroidAnnotations

Android Studio : 配置 AndroidAnnotations

Android Studio 中配置 AndroidAnnotations 非常方便,只需要分別在專案Gradle和Module的Gradle中新增一些配置,然後重新編譯,Android Studio 會自行下載 AndroidAnnotations,之後即可直接使用。

配置專案的Gradle

  • 配置專案的build.gradle

    在build.gradle中新增上圖中的語句即可。

buildscript {
    repositories {
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:1.2.3'

        // replace
with the current version of the android-apt plugin classpath 'com.neenbedankt.gradle.plugins:android-apt:1.8' } }

配置Module的Gradle

  • 配置Module的build.gradle

    • apply plugin: 'com.android.application' 下新增:
apply plugin: 'android-apt'
def AAVersion = '4.1.0'
- 在`dependencies`中新增:
    apt "org.androidannotations:androidannotations:$AAVersion
"
compile "org.androidannotations:androidannotations-api:$AAVersion"
- 在`dependencies`後面新增:
apt {
    arguments {
        androidManifestFile variant.outputs[0]?.processResources?.manifestFile
        // if you have multiple outputs (when using splits), you may want to have other index than 0

        // you should set
your package name here if you are using different application IDs // resourcePackageName "your.package.name" // You can set optional annotation processing options here, like these commented options: // logLevel 'INFO' // logFile '/var/log/aa.log' } }
最後的結果為:
apply plugin: 'com.android.application'
apply plugin: 'android-apt'
def AAVersion = '4.1.0'

android {
    compileSdkVersion 23
    buildToolsVersion "23.0.3"

    defaultConfig {
        applicationId "com.test.test"
        minSdkVersion 14
        targetSdkVersion 23
        versionCode 10000
        versionName "1.0.0.0"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

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

apt {
    arguments {
        androidManifestFile variant.outputs[0]?.processResources?.manifestFile
        // if you have multiple outputs (when using splits), you may want to have other index than 0

        // you should set your package name here if you are using different application IDs
        // resourcePackageName "your.package.name"

        // You can set optional annotation processing options here, like these commented options:
        // logLevel 'INFO'
        // logFile '/var/log/aa.log'
    }
}

重新編譯 Sync Project With Gradle Files

使用 AndroidAnnotations

    • 元件繫結
    • 依賴注入
    • 事件繫結
    • 執行緒
    • 資源注入
    • 網路訪問(Rest)
    • SharedPreferences
    • Preference API
    • 。。。

參考: