作为一门开发语言,反射类机制是必不可少的!android也有类似的开源IOC框架,通过依赖注入(IOC)的实现思想,可以把约定好的一大串代码通过注解@的方式,以IOC技术来生成中间件,AndroidAnnotations在这方面出色,不仅体积小,而且功能强大,极大的减少了代码量,下面将带来一系列的教程。
希望此篇能帮到大家,本系列教程目录:转载请链接并注明:转自 小树技术博客 .
AndroidAnnotations注解框架之介绍+配置(一):飞机直达
AndroidAnnotations注解框架之@Pref(二): 飞机直达
AndroidAnnotations注解框架之@Activity(三):飞机直达
AndroidAnnotations注解框架之@Application(四): 飞机直达
一 Android Annotations 的介绍
AndroidAnnotations官网:AndroidAnnotations首页
开源于github的项目地址:AndroidAnnotations Github
AndroidAnnotations是一个能够让你快速进行Android开发的开源框架,它能让你专注于真正重要的地方。使代码更加精简,使项目更加容易维护,它的目标就是“Fast Android Development.Easy maintainance”.通过一段时间的使用发现,相比原生的Android开发,确实能够让你少些很多代码,它的首页也给出了一个简单的例子,通过例子也可以看到代码比之前几乎少写了一半。由于是开源,所以大家都可以直接拿来使用。
至于使用这个开源框架有什么好处(只有不到50k大小),我这里就不详细翻译github上的介绍了,就简单说一下特性:
1、使用依赖注入(Dependency Injection)#本博接来下几篇的文章将要介绍的开源组件都使用DI, 不熟悉的可以了解一下Inversion of Control(IoC)
2、简化的线程模型(Simplified threading model)
3、事件绑定(Event binding)
4、REST Client
5、No Magic [不知道为什么这样称呼,直译过来就是:无魔法,它的意思是:AndroidAnnotations在编译的时候会产生一个子类(接下来你会明白),你查看这个子类,可以看到它是如何工作的]
更多的介绍可以前往官网或者Github上的项目去查看,它带有超级详细和全面的wiki,CookBook和API文档,十分爽心悦目。
二 Android Annotations 的部署
(一) 环境:
开发工具:Eclipse & android stdio & other (本篇以eclipse为例子,其他IDE请参考官网)
JDK版本:jdk1.6 以上
构建工具:Ant(Eclipse默认的build tool,因此无需装额外的插件)
androidannotations:3.1 (请前往 发行地址 下载)
(二) 步骤:
1,准备好需要的环境,见上文
2,配置好项目文件
3,配置Ant
4,配置Eclipse
1. 新建项目配置JAR包
首先需要有一个普通的android项目,下载并解压Anno 3.1的JAR包,解压后有两个jar包,androidannotations-api-2.7.1.jar放置于项目libs文件夹,androidannotations-2.7.1.jar放置于项目compile-libs文件夹下
(compile-libs需要自己创建,创建在项目的根目录下)
2. 创建配置文件
(在项目根目录下,创建 build.xml和custom_rules.xml)
创建build.xml,内容如下:(注意修改项目名,其他不变,C+V即可)
<?xml version="1.0" encoding="UTF-8"?> <project name="这里是项目名" default="help"> <property file="local.properties" /> <property file="ant.properties" /> <property environment="env" /> <condition property="sdk.dir" value="${env.ANDROID_HOME}"> <isset property="env.ANDROID_HOME" /> </condition> <loadproperties srcFile="project.properties" /> <fail message="sdk.dir is missing. Make sure to generate local.properties using 'android update project' or to inject it through the ANDROID_HOME environment variable." unless="sdk.dir" /> <import file="custom_rules.xml" optional="true" /> <import file="${sdk.dir}/tools/ant/build.xml" /> </project>
创建 custom_rules.xml,内容如下:C+V即可
<?xml version="1.0" encoding="UTF-8"?> <property name="generated.dir" value=".apt_generated" /> <property name="generated.absolute.dir" location="${generated.dir}" /> <property name="java.compilerargs" value="-s '${generated.absolute.dir}'" /> <target name="-pre-compile" > <mkdir dir="${generated.absolute.dir}" /> </target><target name="-compile" depends="-pre-build, -build-setup, -code-gen, -pre-compile" > <do-only-if-manifest-hasCode elseText="hasCode = false. Skipping..." > <!-- merge the project's own classpath and the tested project's classpath --> <path id="project.javac.classpath" > <path refid="project.all.jars.path" /> <path refid="tested.project.classpath" /> <path path="${java.compiler.classpath}" /> <fileset dir="compile-libs" includes="*.jar" /> </path> <javac bootclasspathref="project.target.class.path" classpathref="project.javac.classpath" debug="true" destdir="${out.classes.absolute.dir}" encoding="${java.encoding}" extdirs="" fork="${need.javac.fork}" includeantruntime="false" source="${java.source}" target="${java.target}" verbose="${verbose}" > <src path="${source.absolute.dir}" /> <src path="${gen.absolute.dir}" /> <compilerarg line="${java.compilerargs}" /> </javac> <!-- if the project is instrumented, intrument the classes --> <if condition="${build.is.instrumented}" > <then> <echo level="info" > Instrumenting classes from ${out.absolute.dir}/classes... </echo> <!-- build the filter to remove R, Manifest, BuildConfig --> <getemmafilter appPackage="${project.app.package}" filterOut="emma.default.filter" libraryPackagesRefId="project.library.packages" /> <!-- define where the .em file is going. This may have been setup already if this is a library --> <property name="emma.coverage.absolute.file" location="${out.absolute.dir}/coverage.em" /> <!-- It only instruments class files, not any external libs --> <emma enabled="true" > <instr instrpath="${out.absolute.dir}/classes" metadatafile="${emma.coverage.absolute.file}" mode="overwrite" outdir="${out.absolute.dir}/classes" verbosity="${verbosity}" > <filter excludes="${emma.default.filter}" /> <filter value="${emma.filter}" /> </instr> </emma> </then> </if> <!-- if the project is a library then we generate a jar file --> <if condition="${project.is.library}" > <then> <echo level="info" > Creating library output jar file... </echo> <property name="out.library.jar.file" location="${out.absolute.dir}/classes.jar" /> <if> <condition> <length length="0" string="${android.package.excludes}" trim="true" when="greater" /> </condition> <then> <echo level="info" > Custom jar packaging exclusion: ${android.package.excludes} </echo> </then> </if> <propertybyreplace name="project.app.package.path" input="${project.app.package}" replace="." with="/" /> <jar destfile="${out.library.jar.file}" > <fileset dir="${out.classes.absolute.dir}" excludes="${project.app.package.path}/R.class ${project.app.package.path}/R$*.class ${project.app.package.path}/BuildConfig.class" includes="**/*.class" /> <fileset dir="${source.absolute.dir}" excludes="**/*.java ${android.package.excludes}" /> </jar> </then> </if> </do-only-if-manifest-hasCode> </target>
3. 配置eclipse
在eclipse中,右键项目,选择属性Properties
Properties>> Java Compiler ,勾上Enable,确保编译器版本为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
4,重新编译项目
选择eclipse菜单,Project>>clean>>选上该项目>>确定clean
5,至此,AndroidAnnotations配置完成
这里附上一个简单的Demo,可以导入后,继续Ant和eclipse的配置即可正常使用,如有问题,请留言。
Comments 4 条评论