Compiling Lua for Android
[1]
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 = ‘.’;’ 【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…) [注释:原因是lua解读小数点时,按区域设置来,Android统一设成点就行了]。
5.Remove line 179 (‘struct lconv *cv = localeconv();)
6.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)
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
注释:编译时,把这个包含jni的目录(如luatest)拷贝到
ndk目录的apps下。并且加入一个Application.mk,内容为,
APP_PROJECT_PATH := $(call my-dir)
APP_MODULES := lua
再在ndk根目录下执行make APP=luatest
即可得到一个liblua.so在android-ndk-r5\apps\luatest\libs\armeabi目录下。
这里有一遍介绍lua本身的文章,很好http://sunxiunan.com/?p=1515
[1] 参见
http://www.badlogicgames.com/wordpress/?p=943
|
|