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.

An introduction to Aspect Oriented Programming – AspectJ

As I have been finding in my research, software modularity is crucial to maintainable, separable code. Sadly in modern times, Object Oriented Programming though allowing for common class related code to be modularised, it lacks the modularity needed to avoid code scattering (you know when you have some type of functionality found in parts all over a program) and code tangling (when you have different aspects of your program all mixed up in a single method). Anyhow, this is where the use of Aspect Oriented Programming can come in handy. Aspect-Oriented Programming is based around the idea of modularising “aspects” of your program into their own modules, called Aspects (you was expecting that wasn’t you?).

Common examples to how this can be useful is normally in the situations of say security: On particular class methods you need to do some sort of security checks.  Now in OOP, you would probably have this tangled and scattered across all the classes which require this checking. Though at first may feel normal, what happens when you want to alter the security checking? You need to go through all the classes/methods affected, wasting time (which otherwise can be wasted on facebook).

In this post I will be going through a simple example of AspectJ, an AOP extension to Java.

Firstly, to use aspectj you need to download the tools, these can be for a number of IDEs but I recommend the eclipse plugin (http://eclipse.org/ajdt//). Once the plugin is installed and enabled we are all set and ready to begin.

Starting a new AspectJ Project

Once you have the development tools installed and enabled, we will start by starting a new AspectJ project. For this you may find it isn’t in the menu when clicking on “new”, so click on other. We setup normal Java project settings and make a very basic HelloWorld program.

 

Now, for this example I will work with an aspect called “Logger”. The beauty of AOP is being able to crosscut concerns (code tangling and scattering). For instance, if you updating security aspects of your program that were involved all over your program, you would have these parts in lots of different methods, making it difficult to track. With AOP, you can put all these concerns within a single aspect, needing to edit/update that single aspect file…simple!

Within the aspect, we now first specify the pointcut (where do you want to apply advice) in line 8. Then you want to then want to program what advice you want it to execute and when (before, around, or after the pointcut). As you see in the second screenshot, the string ” Aspect” is appended to the string “Hello World” already outputted.

I strongly suggest after trying this simple example you experiment with other more complex examples. In the next post I will tell you guys how this method can be integrated and used within Android programming as an alternative programming paradigm to OOP.

Until next time!

Summer in Slovakia so far

Hey guys, just thought I would talk about what I have been upto while in Slovakia, as I’m about halfway through my trip.
Well, firstly happy to report I’m having a great time here. First week/10 days was spent at Katkas parents house which was nice, we went mushroom picking and a little sight seeing in the town. Other activites include going to a rodeo (like in America) and taking her dogs for walks in the local fields. Also saw Anna for abit which was nice.
In other news, a paper I wrote for the doctoral symposium at SLE2010 got accepted which I’m happy about. Just have to now revise the piece ready for final submission September 17th. The symposium is being held in Eindhoven, Netherlands and will give me a chance to talk to some very experienced researchers to help get some direction and tips which is great and something to look forward to!
Other than that currently I have been doing a few different bits and pieces which includes some android development (a little app I hope to openssource to add to a openssource project called phpsysinfo) and playing with aspectJ. For those that don’t know what aspectJ is, well it is a java implementation allowing the programmer to use aspect oriented programming, a technique that allows you to crosscut program aspects and concerns which can help increase the modularity of a program. When I have something worth putting up I will.
Anyhow, until next time!