James Williams

Groovy on Android-Not Quite There Yet

Edit(3 AUG 09): Since this post was written there has been some success with getting some low level Groovy running on Android. Check out the project here: http://code.google.com/p/discobot/downloads/list

Disclaimer: This is the product of some 5-6 hours or so of messing around.There might be a better way to do things. The main goal of this entry is to get people thinking about it.

First the good news.

The UI for Android doesn't use Hildon or some other abstract ui framework. If you can layout a web page with absolute positioning with ease, you'll have no problems here. Android uses very readable XML files for the layouts.

Now for the bad news: Because it doesn't use a full J2SE JRE/JDK, Groovy doesn't work out of the box. Here's what I've got so far.

  1. After installing the Android SDK, create a new application with the command: .**/activityCreator.py --out myproject your.package.name.ActivityName** or you could use the Eclipse plugin.

  2. Add a Groovy compile task to the generated Ant build file and include the appropriate library jars on its classpath.

  3. You can compile right now with ant and install the application to the emulator(after starting the emulator) with**adb install [in bin directory of your project]**. In a separate window you can see the output of the running emulator with by starting a debug shell, **adb shell** and then running logcat. At this point it's complaining about 'groovy/lang/GroovyObject'[ClassNotFoundException].

  4. All classes need to be converted to Davlik's dex format. The dx command does this. I tried to convert the groovy-all jar and the contents of the groovy/lib directory in separate attempts but I was getting heap overflow errors. To remedy this, I expanded the jar into bin/classes and removed the swing specific bits. It's not required to remove them but it lessens the likelyhood of a heap over flow. Now rerun the ant task to include them in the application package.

  5. We get complaints about java.beans.Inspector which is located in rt.jar(in one of the jre directories). I extracted all the files in the java.beans directory into bin/classes. The next missing dependency is one of the classes in sun.reflect.misc. I extracted all the files from that directory into bin/classes as well.

Stack trace redacted due to age of post.

Not quite there yet but it's close...failing just before the ApplicationLoader. I wish I could root around more in the android jar. So that's it kids, I hope you don't hold it against me if you thought I had a solution. There is a decent penalty for all the extra classes. The final versions spins for about 3 secs before failing.