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:
    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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值