Aspect Oriented Android Development – Tool Integration

In the last post, I demonstrated a really simple example of how to use AspectJ in a Java program. In this post I will be taking it further and showing you how you can easily integrate the AspectJ and Android Development tools, and quickly be able to run Aspects within your Android app.

First thing though, a little theory about why integrating these tools will make your life of AOP much easier. Though programming applications for Android is mostly in Java (you could use C/C++ with the NDK), its doesn’t run Java bytecode. Android uses the Dalvik VM, as apposed to the Java VM. Now, you may ask why do I care? Well it means that you need to use the Android Toolchain. The first image shows the steps it takes to compile an Android application.

Android Build Processes

Android Build Processes

Now you (hopefully) understand the build process of android applications, lets talk about how aspects are compiled and weaved to an application. When compiling an AspectJ project, it firstly compiles Classes and Aspects to Java bytecode. Then an AspectJ Weaver weaves the Aspect bytecode into the Class bytecode. Now this presents a small issue. Within the build process you see that the class files are changed to Dalvik bytecode using the Dex tool. This means that we need to add a process after the javac and before the dx tool, but the use of the AspectJ builder, which compiles and weaves. This does mean that weaving can only be done compile-time and not run-time, because dynamic weaving would only create Java bytecode, not Dalvik.

I’ve seen over the internet examples of this being done using Ant and other scripts. But surely the better approach would be to integrate the AspectJ tools with the Android tools? In this article I will explain how you can do this. bringing AOP to Android development, while allowing you to still use the tools created for AspectJ and Android development.

Firstly you want to have the AspectJ Developement Tools plugin to Eclipse installed (http://eclipse.org/ajdt//), and the Android SDK and the Android Developer Tools plugin for Eclipse installed (http://developer.android.com/sdk/index.html). Once these have been installed, we will start a new android project, calling it HelloAndroidAspect, and call the first activity HelloWorldActivity, adding the code found in the following image:

Activity

Activity

Now we need to make alterations to the project so we can use AspectJ. Firstly you need to add the AspectJ library to the project. You need to include the AspectJ library (aspectjrt) into the project, which can be found in your Eclipse plugins folder. The folder you are looking for is named org.aspectj.runtime_1.x.x.x depending on what version you have. The library you need from that folder is called aspectjrt.jar.

Add AspectJ Library

Add AspectJ Library

Now we have done that we need to make alterations to the projects .project file. This file should be altered outside Eclipse. Within this file we need add the weaver, so that it weaves the bytecode after compilation but before dex and packaging. Also we need to add the nature of the project, so that the project can be seen as an Android project, as well as a AspectJ project. You add the weaving section by adding the following in the <buildSpec> section of the file:

<buildCommand>
<name>org.eclipse.ajdt.core.ajbuilder</name>
<arguments>
</arguments>
</buildCommand>

This buildCommand should be the first in the <buildSpec> section. After adding this, add the following to the <natures> section of the file:

<nature>org.eclipse.ajdt.ui.ajnature</nature>

This nature should be in the middle of the two already there. For reference I have included a whole .project file.

Now, the tools have been integrated for your Android AspectJ project. We now want to right click on the project and click on New->Other. Then go to the AspectJ folder and click on Aspect. In the form just call it Test and add the code in the following image:

Aspect

Aspect

Now, if you compile and run the code you should see the following on your android phone/emulator and in the DDMS LogCat:

Android Screenshot

Android Screenshot

Android DDMS

Android DDMS

 

 

 

As you can see the AspectJ is working with your Android Application. In this example I am running just a fairly static method, but in the next post I will show you how you can do UI manipulation in Aspects, that are required to run on Activity instances.

To download all the source files please Click Here. Feel free to comment if you have problems with it.

23 thoughts on “Aspect Oriented Android Development – Tool Integration

  1. Very useful article! Will give answer on how to integrate aspects within android projects. Thanks!

  2. Hm, eclipse doesn’t seem to like it much.
    As soon as I write a pointcut and save a file (thus triggering a build)
    it starts thinking that the file is a Java file instead of AspectJ.

    The lightbulb next to the pointcut asks me if I wan’t to create a class called “pointcut”.

    Why ?

  3. Hi, are you sure you added the buildcommands and natures to the .project file and also add the aspectjrt.jar file to your project? try downloading my source files, and update the location of the aspectjrt.jar file, you may just have mistyped something?
    Hope this helps.

  4. OK, got it now.. but I don’t know what was wrong to begin with.
    Apparently – one can right-click on a project in the Package Explorer >> Configure >> Convert to AspectJ Projects… that and then a little bit of additional fiddling (adding aspectrt.jar explicitly in addition to AspectJ Runtime Libraries) did it for me..
    ..and there’s also a “Remove AspectJ Capability” option under AspectJ Tools, which I ended up using before “” re-adding “” it again.

    One day I might event understand the Eclipse project build process .. :/

    Oh, and thanks a mil for doing the blog article in the first place.

  5. oh right, perhaps it was the .project file? Maybe you tried to put the pointcut inside a class and not a aspect?… Either way I’m glad you got it to work!
    I found the aspects as an easy way of increasing code-reuse, which is always a bonus.
    I’m happy you like the article, knowledge should always be open and shared!
    Have fun experimenting.
    Dean

  6. When I try to add Aspect I get message ” Project HelloAndroidAspect must be an AspectJ project”

  7. Probably because you haven’t added the new aspectj nature to your android project.
    After you add the aspectj library to your project you have to alter the .project file as my post states.

  8. Nice article! I’ve got my android’s project with aspectj running. I also want to add how to include aspects which are outside of the project (within a jar).
    This is pretty easy actually, using AJDT… right click on the project / AspectJ Tool / Configure AspectJ Build Path. Go to Aspect Path tab and include whatever jar you want. Thus the aspectj compiler will use your library to seach aspects before weaving process. You can modified .classpath manually as well (I’m using Maven by the way).

    Hope this help too.

  9. Nice I didn’t try that. I haven’t used maven before. Good to see you got it working. Thanks for the contribution!

  10. I get 04-03 17:17:04.027: E/AndroidRuntime(560): Caused by: java.lang.NoClassDefFoundError: org.aspectj.runtime.reflect.Factory

    To get around it, I unzipped aspectjrt.jar into bin/classes and rebuilt – only then classes.dex could have all the aspectj rt classes.

    What did I miss?

  11. Hi, was that error caused by the project in my zip file or your own project? Without really looking at your project I can really only suggest to check the buildpaths and making sure you have aspectjrt.jar added as a external jar. If you haven’t downloaded my example do so, and see if there is a difference.

    Dean

  12. The build path and all that were fine. I think the problem is with eclipse 3.7 (I am using 3.7.2). I am seeing a similar issue using some other 3rd party lib. To fix the problem, go to the Order and Export tab of Java Build Path and select AspectJ Runtime Library.

  13. To fix the NoClassDefFoundError make sure the AspectJ Runtime Library is checked in Properties/Java Build Path/Order and Export.

  14. I suspect that maybe troublesome. In theory it shouldn’t be too hard, but i am not entirely sure if AspectJ needs to be used on a project in its totality and not just part/library. I do think that maybe it may not entirely be capable in the standard ide tools but more something that needs to be done with an ant script.

    Best bet, is to change the project to a library project (there are android instructions on that, and see how you get on. If it wipes the project changes (in terms of the aspectJ builder being present in the buildchain) try adding it back after changing it to a library project.

    Hope this helps, and please keep us updated as this knowledge and experience I’m sure could be useful to other people!

    Thanks,

    Dean

  15. Be sure to remove the java builder from the .project file as well, it’s not needed if you’re going to use the ajdt builder. If you leave it there, it will keep on complaining that no class pointcut is found etc., which is exactly what somebody above already reported.

  16. Great post, it’s really comfortable foregoing the ant-approach that is posted on many other sites. However, I found that meddling with the .project-file manually isn’t a great idea, I simply right-clicked on the project –> Configure –> convert to aspectj project. That did the trick!

  17. Yeah, I am unsure if at time of writing that option was there. At least from experience, editing .project files have generally been hassle free, but if this is easier for you, that’s great!

  18. Working good thanks a lot 🙂 meanwhile is there a way to monitor the time execution of each method and class using AspectJ?

  19. I am not sure I understand what you mean by “the time execution”? Can you not use breakpoints in your code?

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.