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

坚强是说给别人听的谎言 提交于 2019-12-07 18:48:35

问题


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:


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;"


来源:https://stackoverflow.com/questions/27588723/requiring-other-lua-scripts-from-within-a-lua-script-using-luaj-on-android

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!