AndroidAnnotation配置与使用
来源:互联网 发布:python 临时文件 编辑:程序博客网 时间:2024/06/08 08:02
背景
随着安卓手机在市场上的普及,安卓开发的需求也越来越大。但是在安卓开发过程中,其实很多操作都是较为繁琐,且没有必要的,虽然一天下来看看写的代码很多,但是可能也就一个界面,几个功能点。所以对于安卓开发中代码的简化和重用是十分有必要的。
昨天偶然在网易云课堂上看到了AndroidAnnotation这个第三方框架,据说能够大幅度简化安卓编程,提高编程效率。怀着一颗好东西都要尝试一下的心态,今天将之配置并尝试了一下。
配置
AndroidAnnotation的官网为http://androidannotations.org/。但是事实上,在这个网站上只有一个例子代码展示它能够如何如何简化代码,实际的内容主要还是在github上。
通过连接打开git,进入它的git wiki。在这里可以下载jar包,并由一些如何使用的guide。包括如何在Eclipse和IntelliJ上配置。不过在这里它讲的比较简单,如果按照它的方法配置,可能会遇到很多问题。另外bz用的时AndroidStudio,虽然是基于IntelliJ,但是不知为何在细节上还是有些差距的,比如AndroidStudio上就没有Annotation Processing这个设置项。所以我们需要找寻一种适用于AndroidStudio且更为简洁的方式。
得益于Gradle,我们可以直接使用build文件引入AndroidAnnotation。这里需要五步。
1.引入对android-apt的依赖。在app module的build文件中添加以下代码。
buildscript { repositories { mavenCentral() } dependencies { classpath 'com.neenbedankt.gradle.plugins:android-apt:1.2+' }}apply plugin: 'android-apt' //添加android-apt插件
2.设置android-apt参数 。注意把包名换成你的应用的。另外outputs[0]是在新的android-studio的版本中才需要加的。
apt { arguments { androidManifestFile variant.outputs[0].processResources.manifestFile resourcePackageName "你的包名" }}
3.使用apt引入对androidannotation的依赖。
dependencies { apt "org.androidannotations:androidannotations:3.0+" compile "org.androidannotations:androidannotations-api:3.0+" compile fileTree(dir: 'libs', include: ['*.jar']) compile 'com.android.support:appcompat-v7:21.0.3'}
5.最后的build文件应该是这样的。
buildscript { repositories { mavenCentral() } dependencies { classpath 'com.neenbedankt.gradle.plugins:android-apt:1.2+' }}apply plugin: 'com.android.application'apply plugin: 'android-apt'android { compileSdkVersion 21 buildToolsVersion "21.1.2" defaultConfig { applicationId "com.tanglikang.annotationtest" minSdkVersion 9 targetSdkVersion 21 versionCode 1 versionName "1.0" } buildTypes { release { minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' } }}apt { arguments { androidManifestFile variant.outputs[0].processResources.manifestFile resourcePackageName "com.tanglikang.annotationtest" }}dependencies { apt "org.androidannotations:androidannotations:3.0+" // add these compile "org.androidannotations:androidannotations-api:3.0+" // two lines compile fileTree(dir: 'libs', include: ['*.jar']) compile 'com.android.support:appcompat-v7:21.0.3'}
6.重新build工程,系统会自动下载依赖的第三方库。然后就可以使用AndroidAnnotation了。
使用
AndroidAnnotation的使用其实很简单,主要需要注意两个点。
一是标签的使用。AA中有很多有用的标签,具体可以参考以下网页:https://github.com/excilys/androidannotations/wiki/AvailableAnnotations。
二是在编译工程的时候,AA会将原来的Activity换成Activity_的形式,所以需要在AndroidMainfest中也做相应地修改。
这里尝试一个最简单地例子。这里使用了三个标签@EActivity设置布局文件。@ViewById引入一个控件。@Click设置空间点击事件。
@EActivity(R.layout.activity_main)public class MainActivity extends ActionBarActivity { @ViewById(R.id.tv_1) TextView hello; @Click(R.id.tv_1) public void showToast(){ Toast.makeText(MainActivity.this, "ok", Toast.LENGTH_LONG).show(); } @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); }}
<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.tanglikang.annotationtest" > <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name=".MainActivity_" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application></manifest>
总结
经过实践,AndroidAnnotation确实能够大幅度增加android编程效率,值得学习和使用。
参考文献:
http://www.jayway.com/2014/02/21/androidannotations-setup-in-android-studio/
- AndroidAnnotation配置与使用
- android studio 配置 AndroidAnnotation
- AndroidStudio配置Androidannotation
- AndroidAnnotation初使用
- AndroidAnnotation注解使用详解
- Androidannotation的使用
- Android Studio下配置AndroidAnnotation
- Android Studio下配置AndroidAnnotation
- AndroidAnnotation 在AS中的配置
- 01.AndroidAnnotation的使用步骤
- Androidannotation使用之@Rest与服务器交互的JSON数据转换(二)
- Android studio 下 配置AndroidAnnotation 环境
- Android studio正确配置AndroidAnnotation注解
- Android Studio2.2.3配置androidannotation框架
- AndroidStudio中使用AndroidAnnotation的简单介绍
- androidAnnotation 在androidstudio 中配置的正确配置,绝对可用
- 【Android进阶】(1)使用开源框架AndroidAnnotation
- Android注解AndroidAnnotation的使用及实现流程分析
- iOS 一些常用的宏定义
- log笔记
- 博客开通声明
- shell 参数列表的获取&shell使用的一些总结
- VirtualBox两个错误的解决办法
- AndroidAnnotation配置与使用
- 浅拷贝和深拷贝,以及push_back()的奥秘【转】
- TCP长连接与短连接的区别
- Unity编译到Xcode自动添加文件及代码修改
- android ViewPager中获取Fragment
- WindowsApi+Easyx图形库的透明时钟
- Flash AS3学习之路—鼠标事件
- 对python中的字典进行排序
- html