I searched for a solution of how to use AspectJ with Android a few days ago. There is not much about it on the web for now but I've found an interesting google code project described here Pune GTUG: Adding Aspect to Android. The authors proposed to use Ant Java build tool to add aspects in the Android application.
I gave it a try and after some difficulties (outdated libraries and missing environment variables) and with help of Nehal Menta's post I've made it work. However this solution had one big issue for me, because the Eclipse environment didn't provide Aspect UI guidelines and suggestions.
This is why I was looking for another solution and I've finally found it - it is to use the AJDT (AspectJ Development Tools) plugin for Eclipse and modify the .project file. Let's see how to do it.
- Install ADT (AspectJ Development Tools) plugin for Eclipse http://www.eclipse.org/ajdt/
- Help -> Install New Software… -> Add
- Go here: http://www.eclipse.org/ajdt/downloads/
- Choose your Eclipse version, copy the update site url and paste it in the add repository dialog location field – click Ok
- You will see a list of available downloads.
- Select AspectJ Development Tools (Required) and click Finish
- Restart Eclipse after plugin installation
- Create a new Android Hello World project.
- Add some code and verify if it works.
- Add AspectJ jar library.
- Project -> Properties -> Java Build Path –> Libraries -> Add External JARs… You should find it in your Eclipse folder /plugins/org.aspectj.runtime.*/aspectjrt.jar
- Go in your project folder and open .project file in a text editor.
- Add new <buildCommand> at the first place in the <buildSpec> element
- Remove javabuilder <buildCommand> element.
- Add new <nature> element after AndroidNature in <natures> element.
- Save the file. (I include the modified .project file)
- In Eclipse, go in Project Manger and press F5 key (refresh).
- Now everything is ready for AspectJ programming with autobuild and UI guidelines.
- Add new Aspect file – File - > New -> Other -> Aspect.
- Add some code in the aspect file.
- Run your application as Android Application.
And that's all :) Now the application compiles automatically and you'll have also the Aspect UI guidelines and suggestions!
For demonstration, I've used the example code from Pune GTUG: Adding Aspect to Android blog post with a showToast method added. Check out some screen-shots bellow.
Please feel free to leave a comment in the guestbook.
permalink 24. Června 2011 - [19:41:10]
Zpět na: celý archiv