Android, Hecl and scripting

Aside from my hard drive crash, I’ve been working to figure out the best way to integrate Android and Hecl. Since Hecl has been a small, simple, humble language suitable for J2ME (or JavaME or whatever), that has meant in the past that it hasn’t really been able to utilize more complex or advanced Java features like reflection, because they aren’t present in the J2ME API. Android’s more complete implementation of “Java” (yes, I know about the VM, but the programming language I’m writing in is still Java) opens up a number of possibilities.

First on my list of things to do was aim for a generic access layer for Java objects using reflection, so that you can do things like this:

$button settext "blah blah"

Now that we have that working, I also wanted to be able to create new Android widgets from Hecl, which is where things get tricky. The “problem” is that I want something that feels like a scripting language, not simply a reflection of the underlying Java. For instance, the XML description of a button looks like this:

<Button id="@+id/execute"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Execute"
/>

Which is actually not so bad in terms of verbosity. The Java code is uglier, in my opinion:

Button button = new Button(activity);
button.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
button.setText("Execute");

Especially the LayoutParams business, that is nowhere near as clear as the XML, where it’s very obvious what values you’re giving to what parameters. You wouldn’t know that from the Java code without looking up the constructor for the LinearLayout.LayoutParams class.

Now, with Hecl I could of course write the code by hand to make things ‘friendlier’, but I’m worried that if I have to do that too much, it will take a long time to cover the rather extensive API, and it will also bloat the code. The ideal solution would be subvert the XML stuff for my own purposes, however, that’s proving tricky. My first idea was to utilize the AttributeSet parameter that you pass to many View derived objects’ constructors, in order to transform code like this:

button -text hello -layout_width 100 -layout_height 34

Into something that Android can digest. Unfortunately, that’s proving difficult, as the internals of Android barf on the class I’m passing them, even though it, to my knowledge, implements the AttributeSet interface without any problems.

This really made me wish that they would hurry up and release the source code, because I would have a far better understanding of the relationship between the XML attributes and the Java methods if I had access to what’s going on under the hood. Harumph.

My next attempt was a hack that takes things like -text and transforms them into calls to setText, but that isn’t as good, because you need a little bit more custom code for things like the layout parameters, which require special handling. However, lacking the source code, or some help from the Google folks with access to it (which isn’t likely over the next few days as they’re doubtless off consuming turkeys), it looks like that’s what I’ll have to do.

Leave a comment