Friday, June 18, 2010

Kahlua on Android

Yes, that's right, Kahlua supports Android (or Android supports Kahlua, depending on how you see it). And it worked surprisingly easy. All in all, I spent two hours installing the Android SDK, reading the Android documentation and creating an example program that used Kahlua. I even managed to create a very simple interpreter. And it didn't just load the Kahlua core, it also supports the J2SE extensions. It was almost out of the box, and I'll expand on that later.

This was a great success, and I attribute it to the following reasons:
  • The Android SDK is really polished. It was easy to install, easy to get started, and the documentation was pretty good!
  • Kahlua (core and J2SE) is compatible with the same subset of Java 5 that Android supports.
As far as I can tell, the classes in Android are fully compatible with Suns Java classes; at least I haven't found any bugs in Kahlua on Android yet.

The only problem I had was that I got a NoSuchMethodError when invoking Arrays.copyOfRange. It turns out that this method was introduced in Java 6, which explains why Android didn't support it. I simply converted the code into using the old and trusted System.arrayCopy and the code worked!

The Android interpreter even supported the reflection- and annotation-based exposing of Java methods, which I demonstrated by exposing a method that called Thread.sleep. (This was also useful for testing that the UI components in Android worked correctly while executing a script.)

The Android Kahlua interpreter is checked in to the git repository for Kahlua2, if anyone is interested in seeing it. It is basically a single Java file in 148 lines of code.

21 comments:

Pieter Greyling said...

Hi Kristofer,

This looks interesting. I think I will have a go at getting your Android Kahlua interpreter to run on my device.

Thanks,
Pieter

Kristofer said...

Cool, let me know how it works out!

Pieter Greyling said...

It worked like a charm! I have the Android KahluaInterpreter application running on my Nexus One.

I first built the libs, kahlua-core.jar and kahlua-j2se.jar from source (renamed from kahlua-5.1_2.0.0-core.jar and kahlua-5.1_2.0.0-j2se.jar). Then I made an Eclipse Helios/ADT project for the interpreter to which I, obviously, added the above jars as dependencies. This then built, debugged (in the emulator) and deployed (to my device) fine.

I have been playing around with it on my phone for a while now without any hitches.

In fact, I might add a few features of my own if you don't mind. So perhaps I will send you a message via GitHub to make contact if OK with you.

Great work! Thanks for this.

Kristofer said...

Glad to hear it worked. Looking forward to seeing some new features as well!

Pieter Greyling said...

Hi Kristofer,

I have featured Kahlua2 in Chapter 4 of the following book I co-authored. It is due for publication in about 3 weeks:

Practical Android Projects (Apress),
by Lucas L Jordan, Pieter Greyling
http://apress.com/book/view/1430232439
Table of Contents
01. Android Fundamentals
02. Development Tools in Practice
03. Roll Your Own Android Scripting Environment
04. Embedding Lua in Android Applications
05. Introducing SL4A: The Scripting Layer for Android
06. Creating a GUI with HTML/JavaScript and AIR
07. Using REST with Facebook and Twitter
08. Using the Google App Engine with Android
09. Game Development: Graphics
10. Game Development: Animation
11. App Inventor

Kristofer said...

Very cool! I'll have to buy the book and check it out.

Kristofer said...

Cool, my local online book shop has it listed:
http://www.adlibris.com/se/product.aspx?isbn=1430232439

Pieter Greyling said...

Great! Just for the record, the author list is incorrect on their site though. There are two of us (not three) so it should be just: Lucas Jordan and Pieter Greyling.

The Kahlua2 chapter looks like this:
Chapter 4: Embedding Lua in Android Applications
-- Introducing Lua and Kahlua2
-- Lua Resources
-- Kahlua2 Resources
-- Using Kahlua2 in Your Android Java Applications
-- Development Environment Configuration
-- The Kahlua2 Project
-- Setting up the Kahlua2 Runtime Files Project
-- Building from the Console
-- Building from an IDE
-- The Kahlua2 Runtime Libraries
-- The Kahlua2 Android Interpreter Project
-- Setting up the Project
-- Building from the Console
-- Building from an IDE
-- Running the Kahlua2 Android Interpreter
-- Understanding the Basics of Embedding Kahlua2
-- The KahluaDroid Project
-- Running the KahluaDroid Application
-- Running Lua Code On or Off the Main GUI Thread
-- Exposing Android Application Methods to Kahlua2
-- Calling Application Methods as Lua Functions
-- Implementing an Application Startup Script
-- Summary

Uejji said...

Hello. I thought you might find it fascinating that I've written a pi calculation benchmark implemented in Lua, using your Kahlua2 VM. It's called "LuaPi" and you can find it on Android Market or on the Market website at https://market.android.com/details?id=net.humanfish.gpl.android.luapicalc

Android app development said...

This is one of the valuable post.Nicely you describe about Android.This is one of the good collection.I like this post.Android app developers

MaMe82 said...

I'm not able to get it to work on Android.
I've cloned via git and after compilation with ant I copied the kahlua-5.1_2.1.0-core.jar and kahlua-5.1_2.1.0-j2se.jar files to an Android Project.

I've tested with the delivered android KahluaInterpreter.java

running print("hello") results in the following output: length=31; index=7963

It results from an exception of the catch-block of the AsyncTasks doInBackground method.

The VariableElement class imported by se.krka.kahlua.integration.processor.DescriptorUtil.getDescriptor isn't supplied by the Android SDK (tried Api-Level 1.6 till 4.0.3). So how do you get thís thing to run on android ?

If there is a chance to run it, does Kahlua2 provide a way to compile to Dalvik bytecode ?

MaMe82 said...

Okay I tired to get closer to the issue, the relevant part of the stack trace is:

06-25 17:18:12.096: W/System.err(6404): java.lang.ArrayIndexOutOfBoundsException: length=31; index=7963
06-25 17:18:12.106: W/System.err(6404): at org.luaj.kahluafork.compiler.LexState.token2str(LexState.java:246)
06-25 17:18:12.106: W/System.err(6404): at org.luaj.kahluafork.compiler.LexState.txtToken(LexState.java:261)

So the token2str method goes a bit to far.

MaMe82 said...

The problem seems to exist only on my Samsung Galaxy S 2 (Android 4.0.3) I'm not able to reproduce it with a Android 4.0.3 Emulator. Any Hints ?

MaMe82 said...

I've identified the problem. The SGS 2 comes with a soft-keyboard (the one with Swype).

The " converts to unicode 8220 instaed of 34 if this keyboard is used. So it isn't an error in Kahlua, but developers should be taking care of issues like that on android. The interpreter is useless if one doesn't disable the Swype Keyboard for Galaxy S 2 device.

Nice work with Kahlua. I'm really excited if there is a performance boost against luajava on android, which I used up till now.

MaMe82 said...

Impressiv!!

I've testet Kahlua with a simple Lua function incrementing a value everytime it is called.

20000 function calls via java need the following time on a Samsung Galaxy S 2 (Android 4.0.3):

pure java - 2 ms
luajava - 14836 ms (including much memory/gc overhead, heap grows to 8MB)
kahlua - 326 ms (nearly no heap allocation)

It should even be possible to make the update function of my particle system scriptable, if dalvik bytecode compilation was possible. Something like this is badly needed. I can't see
another way to do runtime code compilation on Android.

Kristofer said...

Thanks for your comments and tests. That exception _is_ a bug though. Invalid source code should give a more reasonable error message than that. "Illegal character found at stdin:123: `"`" or something like that.

As for bytecode compilation, it might be possible to use some instrumentation library on android, I haven't really tried it. I know LuaJ has some bytecode compilation code, but I am not sure how mature it is or how well it works on android.

EG said...
This comment has been removed by the author.
EG said...

--This is somewhat off topic, Kristofer:
--I've installed Ant and built Kahlua (despite --an emma_ant.properties file missing), but I --can't locate test_results.html in --/testsuite/. Is this due to the lack of Emma --Ant, or perhaps IDEA?

Edit: I was mistaken in my interpretation of ant's output. The test is run: " [java] Test result: 3219 ok. 0 failed." There just is not n a testresults.html.

azathgthgr said...

I'm also getting a 'VariableElement' not found error, but it seems to be ignored. Everything works as expected.
Is that normal?

Tery Emilson said...

The following line in KahluaUtil.java is failing because apparently it's looking for a file named stdlib.lbc in the root.

InputStream stream = environment.getClass().getResourceAsStream(name + ".lbc");


I cannot find this file 'anywhere' in the source. Am I missing something simple? I did find a copy of stdlib.lbc at this URL:

http://code.google.com/p/openwig/source/browse/trunk/src/openwig/?name=version-0.2.99&r=7deb8fa50e13155fa80c37d43ddbc429243f144d

However when I try using it I get the following error:

Could not load bytecode:Signature 1

Thanks in advance for any help you can provide.

for IT the said...

Great Article
Java Online Training | Java EE course

Java Training in Chennai | J2EE Training in Chennai | java j2ee training institutes in chennai ~ Java Course in Chennai | Java Training Institutes in Chennai

Java 360 | IT Technical Articles |Java Training Institutes

Post a Comment