1. 程式人生 > >Android studio正確配置AndroidAnnotation註解

Android studio正確配置AndroidAnnotation註解

新建一個工程,然後在外部的build.gradle配置檔案新增如下程式碼:

buildscript {
    repositories {
      mavenCentral()
    }
    dependencies {
        // replace with the current version of the Android plugin
        classpath 'com.android.tools.build:gradle:1.5.0'
        // replace with the current version of the android-apt plugin
      <strong>  classpath 'com.neenbedankt.gradle.plugins:android-apt:1.8'</strong>
    }

然後在app目錄下有個build.gradle檔案,在這檔案中新增
//在開始的地方
apply plugin: 'android-apt'
def AAVersion = '3.3.2'

在依賴中新增下面這兩行。需要編譯這個
apt "org.androidannotations:androidannotations:$AAVersion"
compile "org.androidannotations:androidannotations-api:$AAVersion"

在最後的地方新增下面的程式碼
//配置Android annotations
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'
    }
}


最後重寫編譯後,就可以使用android annotation註解,通過註解可以簡寫很多程式碼。