Call Lua Script From an Android Application

Let me first clarify a few things:

I am not trying to run Lua script from the command line.
I am not trying to call any Android functions from Lua

So, from this perspective, this is what I am trying to do.

From an Android action, directly or indirectly call the (JNI / SL4A) Lua script and return the results to the action.

Now, looking at the documentation for SL4A, I see several drawbacks:

1) I can’t find the documentation that says it allows you to programmatically call Lua. 2) It seems that SL4A may need to be installed as a separate application (not too apparent).

The only other option I see is to cross-compile the NDKs of all Lua and then try to call it in C code somehow.

+6
source share
1 answer

You can see my sample AndroLua project. It contains a Lua interpreter built directly into an Android application using the Android NDK. Very small changes were needed to implement it in the Android application.

To actually use Lua from your application, LuaJava is also included in the kit, allowing you to use Lua with Java and vice versa round.

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

Update: loading modules

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

Imagine that in the application data directory there is a module called hello.lua :

 $ 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!') 
+7
source

Source: https://habr.com/ru/post/904059/


All Articles