Compiling Lua for Android

Today i was checking out Lua. I first got in contact with Lua in 2008 when Jattenale started using it for his MMO Gods & Idols. Blizzard also famously uses it for UI scripting in World of Warcraft and a bunch of other big names use it in their products as well.

Lua is a very simple yet powerful embeddable scripting language written in pure Ansi-C (with a few slight exceptions :p) and is portable to a lot of platforms without much effort. Today i tried to compile it with the Android NDK toolchain and was suprised how easy it was to get out a nice shared lib which i can use in my Android projects (with some JNI wrapper code). Here are 8 easy steps to compile Lua as a shared library for Android with the latest Android NDK (for glory and profit!)

  1. Download the latest Lua tar ball from http://www.lua.org/ftp/lua-5.1.4.tar.gz and extract it’s contents. I assume the root folder of Lua to be at ${LUA}
  2. Create an empty folder somehwere and add a subfolder named “jni”
  3. place all the .h and .c files from ${LUA}/src in the jni folder you just created.
  4. Open the file llex.c and change line 181 to ‘ ls->decpoint = ‘.’;’ (note this is a hack which will influence how decimal points are interpreted when lua code is parsed. We do this cause Android’s native support for locals is non existant…)
  5. Remove line 179 (‘struct lconv *cv = localeconv();)
  6. Create a new file called Android.mk and paste the following statements to it:

  7. Create a file called AndroidManifest.xml in the parent folder of the “jni” folder and add the line ‘‘ to it
  8. Change to the parent folder and type ndk-build and you’ll end up with libs/armeabi/liblua.so

Note: there’s a couple of things that might not work with this basic configuration. Look into luaconfig.h and change stuff accordingly. You’ll probably have problems with loading other shared libs to bind C methods in Lua among other things.

All that’s left is writting a JNI wrapper around the Lua C API which is left as an excercise for you dear reader :p

5 thoughts on “Compiling Lua for Android

  1. Awesome, I’m bored of programming for Android using Java, I think I’ll have to create the JNI wrapper around the Lua C API as you said… 😛

  2. I tried to do the same but I got error telling main function is defined twice in lua.c and luac.c

Leave a Reply

Your email address will not be published.