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!)
- 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}
- Create an empty folder somehwere and add a subfolder named “jni”
- place all the .h and .c files from ${LUA}/src in the jni folder you just created.
- 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…)
- Remove line 179 (‘struct lconv *cv = localeconv();)
- Create a new file called Android.mk and paste the following statements to it:
LOCAL_PATH := $(call my-dir) include $(CLEAR_VARS) LOCAL_ARM_MODE := arm LOCAL_MODULE := lua LOCAL_SRC_FILES := lapi.c \ lauxlib.c \ lbaselib.c \ lcode.c \ ldblib.c \ ldebug.c \ ldo.c \ ldump.c \ lfunc.c \ lgc.c \ linit.c \ liolib.c \ llex.c \ lmathlib.c \ lmem.c \ loadlib.c \ lobject.c \ lopcodes.c \ loslib.c \ lparser.c \ lstate.c \ lstring.c \ lstrlib.c \ ltable.c \ ltablib.c \ ltm.c \ lundump.c \ lvm.c \ lzio.c \ print.c include $(BUILD_SHARED_LIBRARY)
- Create a file called AndroidManifest.xml in the parent folder of the “jni” folder and add the line ‘‘ to it
- 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
1563

被折叠的 条评论
为什么被折叠?



