简介
AndroidAnnotations是一个利用注解方式来简化代码结构,提高开发效率的开源框架。另外,针对REST的使用,框架提供了类似Spring IOC的机制,非常方便。
以下是一个块简单的代码片段:
@EActivity(R.layout.translate) // Sets content view to R.layout.translate
public class TranslateActivity extends Activity {
@ViewById // Injects R.id.textInput
EditText textInput;
@ViewById(R.id.myTextView) // Injects R.id.myTextView
TextView result;
@AnimationRes // Injects android.R.anim.fade_in
Animation fadeIn;
@Click // When R.id.doTranslate button is clicked
void doTranslate() {
translateInBackground(textInput.getText().toString());
}
@Background // Executed in a background thread
void translateInBackground(String textToTranslate) {
String translatedText = callGoogleTranslate(textToTranslate);
showResult(translatedText);
}
@UiThread // Executed in the ui thread
void showResult(String translatedText) {
result.setText(translatedText);
result.startAnimation(fadeIn);
}
// [...]
}
如何安装
压缩包里包含两个jar文件:androidannotations-x.x.x.jar和androidannotations-api-x.x.x.jar,另外还有个examples文件夹
- androidannotations-x.x.x.jar是用来提供给java的注解处理插件
- androidannotations-api-x.x.x.jar是给android工程使用的包
下面用实例里带的HelloWorldEclipse演示如何使用
1. 在eclipse里导入实例工程HelloWorldEclipse
2. 修改HelloWorldEclipse中的lib文件夹为libs,并把androidannotations-api-x.x.x.jar放入目录,回到Eclipse里刷新工程,androidannotations-api-x.x.x.jar被自动引入到了build path
3. 新建一个compilers文件夹,将androidannotations-x.x.x.jar拷贝进来待用
4. 打开工程属性,在Java Build Path-library中的androidannotations-2.6-SNAPSHOT-api.jar删除
5. 打开过程属性,在Java compiler-Annotation Processing中启用Annotation Processing
6. 打开过程属性,在Java compiler-Annotation Processing-Factory path中,先删除之前的androidannotations-2.6-SNAPSHOT-api.jar,在把androidannotations-x.x.x.jar(在之前添加的compilers文件夹里)添加进来
7. Eclipse会提示重新编译,选择yes,编译成功!
如何使用Gradle编译
官方给的Gradle插件我没有测试成功,这个编译脚本是参照了网上的一个脚本写的
buildscript {
repositories {
mavenLocal()
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:0.6.+'
classpath 'com.googlecode.androidannotations:androidannotations:2.7.1'
classpath 'com.googlecode.androidannotations:androidannotations-api:2.7.1'
}
}
repositories {
mavenCentral()
}
apply plugin: 'android'
dependencies {
compile fileTree(dir: 'libs', include: '*.jar')
compile 'com.squareup.dagger:dagger-compiler:1.0.1'
compile 'com.squareup.dagger:dagger:1.0.1'
compile 'com.squareup:otto:1.3.3'
}
android {
compileSdkVersion 8
buildToolsVersion "19.0.0"
sourceSets {
main {
manifest.srcFile 'AndroidManifest.xml'
res.srcDirs = ['res']
assets.srcDirs = ['assets']
}
instrumentTest.setRoot('tests')
}
}
configurations {
androidannotations
androidannotations.extendsFrom(compile)
}
dependencies {
androidannotations 'com.googlecode.androidannotations:androidannotations:2.7.1'
compile 'com.googlecode.androidannotations:androidannotations-api:2.7.1'
}
android.applicationVariants.each { variant ->
variant.javaCompile.classpath += configurations.androidannotations
variant.javaCompile.options.compilerArgs += [
'-processor', 'com.googlecode.androidannotations.AndroidAnnotationProcessor',
'-AandroidManifestFile=' + variant.processResources.manifestFile
]
}