Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calling Lua Script From an Android Application

Tags:

android

lua

Let me first of all clarify a few things:

I am not trying to run a a Lua script from a command line.
I am not trying to invoke any android functions from Lua

So with that out of the way, here is what I am trying to do.

From an Android Activity invoke directly OR indirectly (JNI/SL4A) a Lua script and get back the results in the activity.

Now looking at documentation for SL4A I see a few drawbacks:

1) I cannot find the documentation saying that it lets one programmatically call Lua. 2) It looks like SL4A might need to install as a separate application (not too seemless).

The only other option I see is to NDK cross compile all of Lua and then try to invoke it in C code in some manner.

like image 534
Androider Avatar asked Dec 19 '11 07:12

Androider


People also ask

Does Lua work on Android?

Yes, you can use only lua to write apps for android using LuaJava from the kepler project (though I don't believe its being maintained anymore). You can create and manipulate Java objects seemlessly, and interact with the Android APIs through lua.

Can you code Lua on mobile?

Fortunately, we've got the Corona SDK and the Lua programming language, which enables cross platform mobile development.

How do I run a Lua script?

To run a Lua scriptOpen the Lua Script Library through Prepare > Run Lua Script. Use the appearing dialog to load, save, and execute Lua scripts as well as to create new ones. Select the script to be run. Click Execute Script.


1 Answers

You may want to look at my sample project AndroLua. It contains a Lua interpreter embedded directly into an Android application using the Android NDK. Only very small changes were necessary to successfully embed it into the Android application.

In order to actually use Lua from your application, LuaJava is also bundled to allow you to use Lua from Java and the other way round.

Look at the application to see an example how I override the print function to allow an output to a TextView instead of a console.

Update: loading modules

I assume the module you want to load is implemented in Lua. The standard Lua techniques for module loading work as usual - you just have to modify the package.path to your application data directory (or wherever you want to store your scripts/modules).

Imagine that you have a module called hello.lua in the application data directory:

$ adb shell
# cd /data/data/sk.kottman.androlua
# cat hello.lua 
module(..., package.seeall)
function greet(name)
  print('Hello ' .. name)
end
#

Then try running this code in the interpreter:

-- add the data directory to the module search path
package.path = '/data/data/sk.kottman.androlua/?.lua;'..package.path
-- load the module
require 'hello'
-- run a function, should show "Hello Lua!"
hello.greet('Lua!')
like image 127
Michal Kottman Avatar answered Oct 19 '22 02:10

Michal Kottman