牛骨文教育服务平台(让学习变的简单)

一.简介(Introduction):

AndroidAnnotations是一个能够让你快速进行Android开发的开源框架,它可以让我更加专注于业务功能开发。并且使代码更加精简,使项目更加容易维护,它的目标就是“Fast Android Development.Easymaintainance”。相信采用清晰意图且简单的代码来实现我们的功能目标。

它的首页地址是(http://androidannotations.org/)大家如果有兴趣可以去官网看一下,并且首页他们提供了一个例子,进行原生开发以及使用注入框架开发的代码对比。例子也可以看到代码比之前几乎少写了一半。这样看来其中一个优点就不言而喻了直接代码量减少了很多,便于我们项目开发以及维护。项目框架介绍wiki地址:https://github.com/excilys/androidannotations/wiki。项目源代码Github地址:https://github.com/excilys/androidannotations.。大家都可以去下载下来学习一下。

Robert C. Martin写到:

Theratio of time spent reading [code] versus writing is well over 10 to 1[therefore] making it easy to read makes it easier to write.

官网这样写到:当我们在进行开发Android程序的时候,我们常常在想:为什么我们总是需要一遍又一遍的写重复的代码?为什么我们的应用项目越来越难维护?Context,Activity和对象Object,复杂的线程,难以调用的API,加载的匿名listener,大量不需要的casts….难道我们不能改善这些?

基于如下项目开发中遇到的各种各样的问题,我们可以使用今天我们要讲得东西。使用java annotations,开发者可以更加专注业务功能开发。让AndroidAnnotations在编译的时候来生成运行的代码。

二.框架特点(Features):

1、使用依赖注入(Dependency Injection)可以注入views,extras,system service,resource等等

2、简化的线程模型(Simplified  threadingmodel)  ,可以进行annotate方法,让他们在UI线程上执行或在一个后台线程。

3、事件绑定(Event binding),annotate方法来处理views的事件,这样我们就不用再写很多丑陋的匿名监听器类

4、REST Client  创建一个客户端接口,AndroidAnnotations生成实现。

5、No Magic  AndroidAnnotations在编译的时候会产生一个子类(接下来你会明白),你查看这个子类,可以看到它是如何工作的]

AndroidAnnotations提供了这样的超强功能,但是整个项目都不超过159KB并且对运行时的性能没有任何影响。

三.官方代码示例(CodeSample):

朋友你写得Android代码容易写,阅读和维护吗?来看一下下面的例子:

@EActivity(R.layout.translate) // Sets content view toR.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);
    }
// [...]
}

看了以上的例子,对于控件初始化,资源获取,点击事件处理,后台任务处理,线程处理都全部有相关的Annotations,有没有感觉要实现同样的功能,以前写代码的方式是不是非常的蛋疼,来吧一起跟我来学习Android Annotations的使用。

四.项目配置(Android Studio为例Configuration):

我们这边采用Android Studio进行项目开发所以这边讲解Grade配置build方法

配置Android-apt

4.1.在项目全局build.grade进行如下配置

4.2.在moudle内部build.grade中进行如下配置

apply plugin: "com.android.application"
apply plugin: "android-apt"
defAAVersion = "XXX"  这边填写版本号,现在AndroidAnnotations的发布版本已经到了3.3.2
dependencies {
    apt "org.androidannotations:androidannotations:$AAVersion"
    compile "org.androidannotations:androidannotations-api:$AAVersion"
}
apt {
    arguments {
        androidManifestFile variant.outputs[0].processResources.manifestFile
        // if you have multiple outputs (whenusing splits), you may want to have other index than 0
// you should set your package namehere if you are using different application IDs
        // resourcePackageName"your.package.name"
// You can set optional annotationprocessing options here, like these commented options:
        // logLevel "INFO"
        // logFile "/var/log/aa.log"
    }
}

     完成以上的步骤操作,那我们项目的AndroidAnnotations环境就已经配置搭建好了,下一篇我们着重进行将注入框架的基本使用。

本注入框架使用例子会加入到正在开发的框架项目中,可以可以去github站点clone下载

https://github.com/jiangqqlmj/FastDev4Android 

同时欢迎大家star和fork整个开源快速开发框架项目~如果有什么意见和反馈,欢迎留言,必定第一时间回复。也欢迎有同样兴趣的童鞋加入到该项目中来,一起维护该项目。