如何使用android apectj

原文网址:

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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值