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.

Dev8D :: Thursday 12th February

dev8D

dev8D

Today I went to the dev8D event sponsored by JISC at Birkbeck University. Though the event is being held over the duration of this week I was only able to attend today. Today the main agenda was community activities with discussions into real life systems and how they can continue to grow.

In the morning there were two sessions running;

  1. Virtual Learning Environments (VLEs) including Blackboard, Moodle and Sakai.
  2. Online Public Access Catalogs (OPACs) including library catalogs.

I chose to attend the session on VLEs. The talk/discussion facilitator was Sarah Sherman, where we were told about the various systems used including a talk about a single blackboard instance that is ran and shared with 6 brands and how this is managed. A talk was then given about Imperial College’s web ct system, though was quite surprised to see that they used it for short question assessments, though the fact that no wiki was being used didnt surprise me.

A close look was then done for LSE’s system which runs on Moodle. I was quite surprised to see that well since college the layout of Moodle had been kept the same (the college i studied at implemented a VLE running Moodle). The moodle system still is a flat structured system will alot of the functionality being on the homepage. The system is used for content delivery, discussions(class and group) and presentation tools. Wikis are not used because of the lack of power and turnitin is not currently used.

The VLE used by Cambridge was then domonstrated, which was Camtools for their current system and their currently in development Sakai system.  The interface of the Sakai system was similar to the Blackboard V9 with the draggable widgets, though it was said that those system provides the user with the option of changing the layout to their preference, only approx 4% of the userbase had actually done this.

Blackboard V9 was then presented. This version I felt had a slightly improved interface to V8 that i have been using at University currently. Some of the new features included allowing the user to move the screen widgets to different parts of the page which is also implemented on the Cambridge systems. There was further work to make the system easier to manage and help make tasks easy to be carried out by academics. There were also features that had been improved on since  V9 including the wiki system.

Some interesting discussion was brought including talking about using a single blackboard instance for more than 1 brand. Though it was mentioned that you can not have more than one customisation of the system for one instance. Perhaps this maybe sorted in the future?

Lunch was then served with an array of different sandwidchs, that definately put a smile on my face! Just sat around chatting to a nice bloke from Southampton University and talked briefly about Remora which I am involved in.

For the afternoon sessions I chose to attend the talk/discussion on Collaberation Tools including Instant Messaging, VoIP and various online cloud-based tools including Google Mail and Google Docs.

This talk was very interesting with talks from Westminster University and about how they moved their University wide email system to Google (Though perhaps quite a funny turn with the 2 1/2 hours downtime that happened yesterday). This talk did show that money can be saved by using services over hosting your own systems but then there comes a problem which some including myself is an extremely important matter, trust.

Some people could already express concern at the amount of reliance on Google for their email and documents that what happens if Google decide to either charge you or perhaps use your sensitive data?

The talk then basically turned in a grilling by a security consultant from darq# which was very interesting to be inbetween. Some talk was given into the advantages of using Instant Messaging and VoIP tools such as Skype, and disadvantages including the problems with bandwidth if your an unlucky soul to become a “super node”.

Overall I enjoyed myself alot and got to speak with some very interesting individuals. Perhaps Happiness did come of this event?