Android Annotation 使用

这里将介绍大家怎么在Android Studio下使用著名的Android Annotation框架

Android Studio配置

1.修改应用的build.gradle文件 注意是应用的不是整个项目的

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
apply plugin: 'com.android.application'
//Copy Begin---从这里开始复制----
apply plugin: 'android-apt'//添加使用apt插件
def AAVersion = '3.0.1'//这里设置AA的版本号
buildscript {
repositories {
mavenCentral()
}
dependencies {
//这里设置gradle的版本
classpath 'com.android.tools.build:gradle:0.12.2'
//这里设置apt的版本
classpath 'com.neenbedankt.gradle.plugins:android-apt:1.+'
}
}
repositories {
mavenCentral()
mavenLocal()
}
apt {
arguments {
androidManifestFile variant.processResources.manifestFile
resourcePackageName 'com.houny.androidannotations'//TODO 注意这里要设置当前项目的包名
// logLevel 'ERROR'
// logFile '/var/log/houny.log'
}
}
//Copy End---一直复制到这里,注意上面的TODO处的包名----
android {
compileSdkVersion 19
buildToolsVersion "19.1.0"
defaultConfig {
applicationId "com.houny.androidannotations"
minSdkVersion 14
targetSdkVersion 19
versionCode 1
versionName "1.0"
}
buildTypes {
release {
runProguard false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
//Copy Begin-------从这里开始复制--------------
apt "org.androidannotations:androidannotations:$AAVersion"
compile "org.androidannotations:androidannotations-api:$AAVersion"
//Copy End--------一直复制到这里-------------
}

2.AndroidManifest.xml中注册Activity的时候在最后加上_符号

android:name=”.activity.MainActivity_”

可能会显示红色报错,只要点击BuildRebuild Project就可以了