Reputation: 21335
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.
Upvotes: 6
Views: 8151
Reputation: 16753
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!')
Upvotes: 7