泰晓科技 -- 聚焦 Linux - 追本溯源,见微知著!
网站地址:https://tinylab.org

儿童Linux系统,可打字编程学数理化
请稍侯

Lua Programing on Android

Wu Zhangjin 创作于 2013/11/24

by falcon wuzhangjin@gmail.com of TinyLab.org 2013/11/24

Introduction

The official programming languagesof Android areJava, C and C++, but up to now, lots of other programming languages have been supported on Android, such languages includePython, Perl, JRuby, Lua, BeanShell, JavaScript, Tcl, and shell, C#.

To do Java, C and C++ programming on Android, we can simply install Android SDK and NDKand follow the documents from Android Developer Center.

To programming with the otherlanguages listed above, we can try the Scripting Layer for Android(SL4A) and Common Language Extension for Android(CLE), to get a quick start, please read thewiki pageof SL4A and an example about CLE.

Here, will not use the powerful SL4A and CLE (will talk about them in other articles), but instead, will expore some other ways to let a language work on Android, Let’suse the small Lua as our demo language.

“Lua is a powerful, fast, lightweight, embeddable scripting language.”

In this article, we will using LuaJIT, AndroLua on Android to do Lua programming.

  • LuaJIT is a Just-In-Time Compiler (JIT) for the Lua programming language.
  • AndroLua is the Lua interpreter ported to the Android platform. Others have ported Lua to Android, but this project is special:

    it includes LuaJava, so you can access (almost) everything the Android API provides because writing code on the soft keyboard can be hard, you can connect to it using TCP an upload code from your computer

LuaJIT

Build LuaJIT withAndroid NDK

To share the common libraries provided by Android system, we can build LuaJIT with the ARM Cross Compiler from Android NDK :

$ wget -c http://luajit.org/download/LuaJIT-2.0.1.tar.gz
$ tar zxf LuaJIT-2.0.1.tar.gz && cd LuaJIT-2.0.1
$ apt-get install gcc-multilib
$ make HOST_CC="gcc -m32" CROSS=arm-linux-androideabi- TARGET_SYS=Linux
$ adb push src/luajit /data/
3177 KB/s (378280 bytes in 0.116s)
$ adb shell
root@android:/ # chmod 777 /data/luajit
root@android:/ # /data/luajit
LuaJIT 2.0.1 -- Copyright (C) 2005-2013 Mike Pall. http://luajit.org/
JIT: ON ARMv7 fold cse dce fwd dse narrow loop abc sink fuse
> for i = 1,4 do print(i) end
1
2
3
4
>

The above LuaJIT is dynamically linked and its dynamic linker is: /system/bin/linker:

$ file src/luajit
src/luajit: ELF 32-bit LSB executable, ARM, version 1 (SYSV), dynamically linked (uses shared libs), stripped
$ arm-linux-androideabi-readelf -l src/luajit | grep interpreter
      [Requesting program interpreter: /system/bin/linker]

Build LuaJIT with Linaro ARM Cross Compiler

If no Andriod NDK installed, the Linaro ARM Cross Compiler can be used, for example, in Ubuntu system, it can be simply installed with:

$ sudo apt-get install gcc-arm-linux-gnueabi

Since this compiler use different dynamic linker and different libraries, to compile LuaJIT for Android, static linking may be better for it avoid the installation of the dynamic linker and the shared libraries:

The using is the same, let’s use the math operation as an example:

$ adb push src/luajit /data/
$ adb shell
root@android:/ # /data/luajit
LuaJIT 2.0.1 -- Copyright (C) 2005-2013 Mike Pall. http://luajit.org/
JIT: ON ARMv7 fold cse dce fwd dse narrow loop abc sink fuse
> print(math.sin(2.3))
0.74570521217672
>

Write a Lua script

$ cat > /tmp/test.lua
#!/data/luajit

print(math.sin(2.3))
$ adb push /tmp/test.lua /data/
$ adb shell chmod 777 /data/test.lua
$ adb shell /data/test.lua
0.74570521217672

AndroLua

The above method allows us to build theLua programming environment easily but it lacks of some features provided by AndroLua:

  • It integrates the official Lua 5.1, allows to execute generic Lua scripts, have no compatiblity issues
  • It integrates the LuaJava,allows scripts written in Lua to manipulate components developed in Java
  • It allows to write Lua programs on Android, excute it and check the status (Not mature enough)
  • Because writing code on the soft keyboard can be hard, you can connect to it using TCP an upload code from your computer

Now, Let’s refer to the README, download, build, install and useAndrodLuaon Android.

Download AndroLua

AndrodLua is maintained under a git repository, just clone it:

$ git clone git://github.com/mkottman/AndroLua.git && cd AndroLua

Build AndroLua

To build AndroLua, we need to update the project with new Android version with the ‘android update’ command and then build a debug package with the ‘ant’ command. Here, we build it for Android 4.2.

$ android list
Available Android targets:
----------
id: 1 or "android-16"
     Name: Android 4.1.2
     Type: Platform
     API level: 16
     Revision: 4
     Skins: HVGA, WVGA800 (default), WXGA800, WVGA854, WQVGA400, WXGA800-7in, WQVGA432, QVGA, WSVGA, WXGA720
     ABIs : no ABIs.
----------
id: 2 or "android-17"
     Name: Android 4.2
     Type: Platform
     API level: 17
     Revision: 1
     Skins: HVGA, WVGA800 (default), WXGA800, WVGA854, WQVGA400, WXGA800-7in, WQVGA432, QVGA, WSVGA, WXGA720
     ABIs : armeabi-v7a
Available Android Virtual Devices:
$ android update project -p ./ -t 2

Then, install ant, and build the AndroLua package:

$ sudo apt-get install ant
$ ant debug

As a result, the package is compiled: bin/Main-debug.apk.

Install AndroidLua

Now, let’s install it with ‘adb’:

$ adb install bin/Main-debug.apk

Using AndroLua

After installation, the AndrodLua icon will be listed in the desktop of your Android device, start it and it will looks like:

image

Write your Lua programs and execute them there.

Remote Programming

As we can see, the AndroLua UI interface is very simple andwriting code in Android with the soft keyboard is hard, so, we can try the remote programming feature of AndrodLua, to use it, Let’s forward its remote service to local:

$ adb forward tcp:3333 tcp:3333

And then, start the local Lua intepreter with:

$ lua ./interp.lua
loading init.lua

> require 'import'
> print(Math:sin(2.3))
>
>
0.74570521217672

This is also very simple.

Summary

In this article, we have shown two methods to build the Lua programming environment for Android, accordingly, simple Lua scripts are written and executed on Android system with these environments.

As we can see, both of them are simple and only for newcomers:

  • To build a full Lua programming environment, the SL4A and CLE, or the other commercial IDEs are required, we will discuss them in the other articles.
  • But, both of themshow us the hidden details behind SL4A and CLE, based on these practical steps and the open source codes, we may be able tobuild the other native programming environments and develop similar IDEs ourselves.

For example, the book: Optimizing Embedded Systems using Busybox shows how to build native Bash and C programming environments for Android system.



Read Related:

Read Latest: