android工程使用luaj,GitHub - kigkrazy/luaj-android: luaj procject for android.

luaj安卓框架

本项目由luaj的jse版本为基础修改而来。语法上与luaj并无不一样,主要修改了几处安卓平台上的引用错误。

文档

简单使用

引入库

compile 'com.reizx:luaj-android:3.0.4'

JAVA调用LUA例子

以下例子都会输出以luaj-tag为tag的日志。

执行文件(demo)

调用方法:

public void invokeFile() {

String path = "/sdcard/SimpleExample.lua";

// copy the script to path

ResourceUtils.copyFileFromAssets("SimpleExample.lua", path);

// init global before

// create an environment to run in

// Globals globals = JsePlatform.standardGlobals();

// Use the convenience function on Globals to load a chunk.

LuaValue chunk = globals.loadfile(path);

// Use any of the "call()" or "invoke()" functions directly on the chunk.

chunk.invoke();

}

SimpleExample.lua

---

--- 例子执行请执行rlua模块中AndroidTest的SimpleExample测试用例

--- Generated by EmmyLua(https://github.com/EmmyLua)

--- Created by kig.

--- DateTime: 2018/3/6 17:34

---

-- 引入类

LuajLog = luajava.bindClass("com.reizx.luaj.component.LuajLog") --Log打印类

-- 调用类的静态接口

LuajLog:d("hello luaj...1")

LuajLog:d("hello luaj...2")

LuajLog:d("hello luaj...3")

执行脚本流

public void invokeStream() {

String path = "/sdcard/SimpleExample.lua";//请查看"执行文件"中的SimpleExample.lua

// get the script InputStream

InputStream in = new ByteArrayInputStream(ResourceUtils.readAssets2String("SimpleExample.lua").getBytes());

// init global before

// create an environment to run in

// Globals globals = JsePlatform.standardGlobals();

// Use the convenience function on Globals to load a chunk.

/** Load the content form an input stream as a binary chunk or text file.

* @param is InputStream containing a lua script or compiled lua"

* @param chunkname Name that will be used within the chunk as the source.

* @param mode String containing 'b' or 't' or both to control loading as binary or text or either.

* @param environment LuaTable to be used as the environment for the loaded function.

* */

LuaValue chunk = globals.load(in, "@"+"Simple", "bt", globals);

// Use any of the "call()" or "invoke()" functions directly on the chunk.

chunk.invoke();

}

自定义接口

public void invokeCustom(){

Globals globals = customEvn();

// get the script InputStream

InputStream in = new ByteArrayInputStream(ResourceUtils.readAssets2String("hyperbolicapp.lua").getBytes());

LuaValue chunk = globals.load(in, "@"+"hyperbolicapp", "bt", globals);

// Use any of the "call()" or "invoke()" functions directly on the chunk.

chunk.invoke();

}

/**

* 自定义lua环境,可以自定义API

* @return

*/

public Globals customEvn(){

Globals globals = new Globals();

globals.load(new JseBaseLib());

globals.load(new PackageLib());

globals.load(new Bit32Lib());

globals.load(new TableLib());

globals.load(new StringLib());

globals.load(new CoroutineLib());

globals.load(new JseMathLib());

globals.load(new JseIoLib());

globals.load(new JseOsLib());

globals.load(new LuajavaLib());

//todo register library

globals.load(new hyperbolic());

LoadState.install(globals);

LuaC.install(globals);

return globals;

}

hyperbolicapp.lua

-- Sample luaj code to try loading and executing the 'hyperbolic' sample library

--

-- The sample library source is in examples/jse/hyperbolic.java.

-- For this sample to work, that source must be compiled, and the class must

-- be on the class path.

--

-- First load the library via require(). This will call the public constructor

-- for the class named 'hyperbolic' if it exists, and then initialize the

-- library by invoking LuaValue.call('hyperbolic') on that instance

require 'hyperbolic'

LuajLog = luajava.bindClass("com.reizx.luaj.component.LuajLog") --Log打印类

-- Test that the table is in the globals, and the functions exist.

-- LuajLog:d('hyperbolic : '..hyperbolic)

-- LuajLog:d('hyperbolic.sinh : '..hyperbolic.sinh)

-- LuajLog:d('hyperbolic.cosh : '..hyperbolic.cosh)

-- Try exercising the functions.

LuajLog:d('sinh(0.5) : '..hyperbolic.sinh(0.5))

LuajLog:d('cosh(0.5) : '..hyperbolic.cosh(0.5))

修改日志

修复classloader引用错误。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值