往Android上移植lua

來源:互聯網
上載者:User
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

描述:檔案全在這裡
附件:
luatest.rar (198 K)  原文發在這裡 http://www.bangchui.org/read.php?tid=21852 
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.