如何使用android apectj

来源:互联网 发布:淘宝客的pid是什么 编辑:程序博客网 时间:2024/06/10 04:38

原文网址:

http://cund99.blogspot.com/2012/03/marrying-aspectj-to-android-code.html

Android-aspectj is a nice project that proves that AspectJ can be used with Android code, see http://code.google.com/p/android-aspectj. However the available code is a little outdated and the build file is rather complicated.


In this post I will show an improved solution so that we can build an Android App with or without AspectJ stuffs easily:

  • "ant aop debug install", to compile and enable AspectJ
  • "ant debug install", to have a clean compilation and install the app without AspectJ 
Here are the steps you will do:
  1. download and install AspectJ from here: http://www.eclipse.org/aspectj/downloads.php, once downloaded, you should run the jar file to install AspectJ to your machine
  2. Copy the runtime lib: aspectjrt.jar from the AspectJ home to thelibs in your Android Project
  3. Add the following custom Ant code to your build.xml file (in case you dont have the file, just open the project in a Console, run "android update project -p .", the android tool will prepare the file for you)



<taskdef resource="org/aspectj/tools/ant/taskdefs/aspectjTaskdefs.properties">

<classpath>
<pathelement location="${aspectj.home}/lib/aspectjtools.jar" />
</classpath>
</taskdef>

<property name="aop.output" value="bin/aop" />
<target name="-post-compile">
<if condition="${aop.enabled}">
<then>
<echo message="Weaving aspects to .class files before dex converts .class files to .dex file" />
<iajc destDir="${aop.output}" Xlintwarnings="true" showWeaveInfo="true" target="1.5" source="1.5">
<argfiles>
<pathelement location="config.lst" />
</argfiles>
<inpath>
<pathelement location="${out.classes.absolute.dir}" />
</inpath>
<classpath>
<pathelement location="${aspectj.home}/lib/aspectjrt.jar" />
<path refid="android.target.classpath" />
</classpath>
</iajc>
<move file="${out.classes.absolute.dir}" todir="${out.dir}/classes.old" />
<!-- remember to replace your package initial, e.g. com -->
<move file="${aop.output}/com" todir="${out.classes.absolute.dir}" />
<delete dir="${aop.output}" />
</then>
</if>
</target>

<target name="aop">
<property name="aop.enabled" value="true" />
</target>

If you look carefully into the build.xml file, you will see the target "-post-compile" is already there and commented. You should put the above code into that location. Remember to change the version-tag to custom so that your build file will not be overwritten by the android tool if you launch it again.

<!-- version-tag: custom -->

The build.xml file needs one external variable: aspectj.home, and you should put it in the local.properties file, like this:

aspectj.home=/home/programs/aspectj1.6

You can also specify parameters for iajc in the config.lst at the root of your project.

Now you write your aspect code and once you are done, you can build and install your project with or without aspect code by the flag aop:
  • ant aop debug install
  • ant debug install
You can download the example project at, http://code.google.com/p/android-aspectj, delete their build file, create a new build file and update local information with the android tool, and follow the above steps to have a quick try out.

0 0
原创粉丝点击