Requiring other lua scripts from within a Lua script using LuaJ on Android

1k Views Asked by At

I'm having trouble calling Lua scripts from Java via LuaJ on Android that require other Lua scripts. I think it's something to do with my current working directory.

What I'm trying in Java:

InputStream input = EvilApp.getContext().getAssets().open("lua/pathTest.lua");
Prototype p = LuaC.instance.compile(input, "pathTest.lua");
LuaValue g = JsePlatform.standardGlobals();
LuaClosure c = new LuaClosure(p, g);
c.call();

pathTest.lua:

require "Foo"
local str = Foo.getString()
print(str)

For this specific test, both lua files are in the same directory for simplicity, but I will need relative paths to other lua files.

I've tried playing with package.path, but nothing I've tried has worked. When running in Android, package.path == "?.lua" by default.

I've run this test with relative paths via commandline, and inside Eclipse using Koneki, and they work fine. It's specifically the Android environment that's failing.

Also, I'm able to get Lua scripts with no requires to work fine in Android.

1

There are 1 best solutions below

0
On BEST ANSWER

Figured out one solution. Adding the absolute path to the package.path worked. For example, my Lua scripts are contained in my assets folder, so adding:

package.path = package.path .. ";/assets/lua/?.lua;"

before a require for a file contained in /assets/lua/ works.

Similarly, removing the leading backslash:

package.path = package.path .. ";assets/lua/?.lua;"

also works, so it seems that LuaJ by default starts the package.path on the Android project's root directory. However, I was unable to get other relative paths to work, such as:

package.path = package.path .. ";assets/../assets/lua/?.lua;"