AndroidAnnotations库的使用

AndroidAnnotations(Code Diet)
android高速开发框架简单介绍:
项目地址:https://github.com/excilys/androidannotations
文档介绍:https://github.com/excilys/androidannotations/wiki
官网网址:http://androidannotations.org/
特点:(1) 依赖注入:包含view。extras。系统服务。资源等等
(2) 简单的线程模型。通过annotation表示方法执行在ui线程还是后台线程
(3) 事件绑定:通过annotation表示view的响应事件,不用在写内部类
(4) RESTclient:定义client接口,自己主动生成REST请求的实现
(5) 没有你想象的复杂:AndroidAnnotations仅仅是在在编译时生成对应子类
(6) 不影响应用性能:仅50kb,在编译时完毕,不会对执行时有性能影响。

PS:与roboguice的比較:roboguice通过执行时读取annotations进行反射,所以可能影响应用性能。而AndroidAnnotations在编译时生成子类,所以对性能没有影响

笔者是在Android Studio下进行配置开发的。在配置的过程中,除了一点问题,解决方式记录在此:

问题1:Error:(2, 0) Plugin with id 'android-apt' not found.

这个问题出如今在Module中配置build.gradle例如以下出现的:

dependencies {
    def AAVersion = '3.2'
    apt "org.androidannotations:androidannotations:$AAVersion"
    compile "org.androidannotations:androidannotations-api:$AAVersion"
}
解决方法:在Project的build.gradle加入例如以下配置:
buildscript {
    repositories {
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:1.2.3'
        classpath 'com.neenbedankt.gradle.plugins:android-apt:1.4'//Added line
        
    }
}
问题2:Error:(33, 1) 错误: The AndroidManifest.xml file contains the original component, and not the AndroidAnnotations generated component. Please register MainActivity_ instead of MainActivity

出现错误定位代码例如以下:

@EActivity(R.layout.activity_main)
public class MainActivity extends AppCompatActivity{
    //......

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initViews();
    }
依照提示的思维修复错误,例如以下:

好吧,在执行前,先点击编译..


OK,Run.

參考:

https://github.com/Trinea/android-open-project

原文地址:https://www.cnblogs.com/yxysuanfa/p/7387562.html