android 使用AndroidAnnotations注解简化安卓开发

转自:http://blog.csdn.net/limb99/article/details/9067827

参考:http://blog.chinaunix.net/uid-25436678-id-3969860.html


做过JavaEE肯定对Spring不陌生,尤其是spring的IOC,真是太好用了。顺着这个思想,Android上有没有spring来实现IOC。搜索一下,果然spring已经推出了spring for android,不过可惜的是它并不支持IOC,但是却在官网发现了这个么一篇文章http://blog.springsource.org/2011/08/26/clean-code-with-android/,里面讲了android依赖注入(IOC)的实现思想和已经实现依赖注入的几个项目,自己感觉AndroidAnnotations最为出色,不仅jar包小,而且功能强大,极大的减少了代码量。本文将会讲到AndroidAnnotations的部署和简单应用。


先看普通代码和用AndroidAnnotations的比较:

布局文件:

[html]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. <LinearLayoutxmlns:androidLinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"  
  2.     xmlns:tools="http://schemas.android.com/tools"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     android:orientation="vertical"  
  6.     tools:context=".MainActivity">  
  7.    
  8.     <EditText  
  9.        android:id="@+id/et"  
  10.        android:layout_width="match_parent"  
  11.        android:layout_height="wrap_content"  
  12.        android:hint="Enter thecontent"/>  
  13.    
  14.     <TextView  
  15.        android:id="@+id/tv"  
  16.        android:layout_width="match_parent"  
  17.        android:layout_height="wrap_content"  
  18.        android:text="Content:"/>  
  19.    
  20.     <Button  
  21.        android:id="@+id/btn"  
  22.        android:layout_width="match_parent"  
  23.        android:layout_height="wrap_content"  
  24.        android:text="click me"/>  
  25.    
  26. </LinearLayout>  


普通代码:

[java]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. public class MainActivity extends Activity {  
  2.    
  3.     EditText et;  
  4.     TextView tv;  
  5.     Button btn;  
  6.    
  7.     @Override  
  8.     protected void onCreate(Bundle savedInstanceState) {  
  9.        super.onCreate(savedInstanceState);  
  10.        setContentView(R.layout.activity_main);  
  11.    
  12.        et = (EditText) findViewById(R.id.et);  
  13.        tv = (TextView) findViewById(R.id.tv);  
  14.        btn = (Button) findViewById(R.id.btn);  
  15.    
  16.        btn.setOnClickListener(new OnClickListener() {  
  17.    
  18.            @Override  
  19.            public void onClick(View v) {  
  20.               tv.setText("Content:" +et.getText());  
  21.            }  
  22.        });  
  23.    
  24.     }  
  25. }  


使用AndroidAnnotations的代码

[java]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. @EActivity(R.layout.activity_main)  
  2. public class MainActivity extends Activity {  
  3.    
  4.     @ViewById  
  5.     EditText et;  
  6.     @ViewById  
  7.     TextView tv;  
  8.    
  9.     @Click  
  10.     void btn() {  
  11.        tv.setText("Content:" +et.getText());  
  12.     }  
  13.    
  14. }  

超简洁有木有。


AndroidAnnotations部署

 

环境:

系统:windows 8 (64bit)

开发工具:Eclipse 3.8

JDK版本:jdk1.6

构建工具:Ant(Eclipse默认的build tool)

androidannotations:2.7

 

步骤:

1.  下载并导入jar包

2.  配置Ant

3.  配置Eclipse

 

1.  jar包官网下载地址https://github.com/excilys/androidannotations/wiki/Download;

解压后的两个jar包androidannotations-api-2.7.1.jar和androidannotations-2.7.1.jar分别放在项目的libs文件夹下和compile-libs文件夹下(compile-libs需要自己创建,创建在项目的根目录下就行。如果放在了同一文件夹下必然出错,因为两个包里存在相同的文件路径和文件名)。

 

2.  配置Ant只需要在项目的根目录下创建两个文件即可(build.xml和custom_rules.xml)

创建build.xml使用cmd命令

[plain]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. android update project --path "$PROJECT_ROOT$"  
如果没有配置android环境变量要进入到..\sdk\tools\目录下去执行,"$PROJECT_ROOT$"为项目的根路径,例如:

D:\Program Files\adt-bundle-windows-x86_64\sdk\tools>android update project --path F:\work_in_geekon\workspace\TestAA

至于custom_rules.xml手动创建即可,首先添加如下内容

[html]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. <propertynamepropertyname="generated.dir"value=".apt_generated"/>  
  2. <propertynamepropertyname="generated.absolute.dir"location="${generated.dir}"/>  
  3. <propertynamepropertyname="java.compilerargs"value="-s'${generated.absolute.dir}'"/>  
  4. <targetnametargetname="-pre-compile">  
  5.       <mkdirdirmkdirdir="${generated.absolute.dir}"/>  
  6. </target>  

打开$ANDROID_SDK_ROOT$/tools/ant/build.xml(例如我的D:\ProgramFiles\adt-bundle-windows-x86_64\sdk\tools\ant\build.xml),找到节点<target name="-compile"…

  
  
[html] view plain copy 在CODE上查看代码片 派生到我的代码片
  1. <targetnametargetname="-compile"depends="-build-setup, -pre-build, -code-gen, -pre-compile">  
  2.  ...  
  3. </target>  

将上述内容全部copy到custom_rules.xml中。找到以下节点(在custom_rules.xml文件中),并添加

[html]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. <fileset dir="compile-libs"includes="*.jar"/>  

<target name="-compile" ...>
...
            <path id="project.javac.classpath">
                ...
               <fileset dir="compile-libs" includes="*.jar"/>
            </path>
...
</target>

绿色部分为新增内容。保存文件,Ant的配置也就OK了。

 

3.  配置Eclipse。

选择项目右键,Properties >> Java Compiler ,确保编译器版本为1.6。

Properties >> Java Compiler >> Annotation Processing >> Enable annotation processing(开启)。

Properties >> Java Compiler >> Annotation Processing >> Factory Path >> 添加jar包,就是之前放在compile-libs目录下的androidannotations-2.7.1.jar。

重新编译(Clean)下项目既可以了。

 

注意:AndroidManifest.xml文件里的Activity的名字都要在原来的基础上加一个下划线(”_”)。例如

<activityandroid:name="com.example.testaa.MainActivity">

</activity>

改成

<activityandroid:name="com.example.testaa.MainActivity_"></activity>

在Activity跳转的时候也要如此new Intent().setClass(this, MainActivity_.class);

 

除了@Eactivity @ViewById@Click之外还有

@EApplication

@EBean

@EFragment

@EService

@EView

@EviewGroup

@App

@Bean

@Fullscreen

……

更多的应用请参照

官网http://androidannotations.org/

GitHubhttps://github.com/excilys/androidannotations/wiki


PS:androidannotations项目在导出的时候如果路径包含中文就会提示错误路径未找到。

 

 

更新1( 2014-8-14)

好像有不少人开始用这个东西了。有人留言说部署的时候还是有问题,我根据自己写的步骤走了一遍没有发现问题。这次我用了官方最新的3.0.1的版本部署了个项目,大家可以自己看看。其他环境和上面的介绍一致。大家导入项目后做的就是第3步,配置Eclipse。

项目demo下载http://download.csdn.net/detail/limb99/7756923


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值