AndroidAnnotations一个可以让你的android代码更加简洁的框架

简介

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
            ]
}

















  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值