• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

haodynasty/AndroidLuaExample: Lua 5.3.3 and LuaJava ported to Android example

原作者: [db:作者] 来自: 网络 收藏 邀请

开源软件名称(OpenSource Name):

haodynasty/AndroidLuaExample

开源软件地址(OpenSource Url):

https://github.com/haodynasty/AndroidLuaExample

开源编程语言(OpenSource Language):

C 82.1%

开源软件介绍(OpenSource Introduction):

AndroidLuaExample

update to Lua 5.3.3 and LuaJava ported to Android example

License Download

Import

add to build.gradle,${latest.version} is Download

dependencies {
    compile 'com.blakequ.luajava:luajava:${latest.version}'
}

maven

<dependency>
  <groupId>com.blakequ.luajava</groupId>
  <artifactId>luajava</artifactId>
  <version>${latest.version}</version>
  <type>pom</type>
</dependency>

How to use

you can download example and study how to use. add proguard rules

# luajava
-keep class org.keplerproject.luajava.**{*;}
# For native methods
-keepclasseswithmembernames class * {
    native <methods>;
}

1. init lua

init lua file only once after start app

private void initLua(){
        mLuaState = LuaStateFactory.newLuaState();
        mLuaState.openLibs();
        //push Log object to lua, in lua using like: Log:i(TAG, "this log can show in AS logcat window")
        try {
            mLuaState.pushObjectValue(Log.class);
            mLuaState.setGlobal("Log");
        } catch (LuaException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
    }

2. add lua file to raw

app/src/main/res/raw/luafile.lua

function GetVersion(info, intvalue)
    Log:i("LuaLog", info..intvalue)
    print('this log')
    return 1
end

3. invoke lua function

private void executeLuaFile()
    {
            mLuaState.getGlobal("GetVersion");
            mLuaState.pushString("reload lua test");// input params
            mLuaState.pushNumber(10);
            mLuaState.call(2, 1);//2 input, 1 output
            String result = mLuaState.toString(-1);
            if (result == null){
                System.out.println("GetVersion return empty value");
            }else {
                System.out.println("GetVersion return value"+result);
            }
    }

4. invoke lua and error handle

private void executeLuaFile2()
    {
            //in lua file not exist this method:GetNotMethod
            mLuaState.getGlobal("GetNotMethod");
            mLuaState.pushString("reload lua test");// input params
            mLuaState.pushNumber(10);//not use pushInteger
            //if using call will throw exception(can not catch), so you must use pcall
            //success to invoke method if return 0, otherwise is error.
            int retCode = mLuaState.pcall(2, 1, -1);//2 input, 1 output
            String result = mLuaState.toString(-1);
            if (retCode != 0){
                LogUtils.e(TAG, "Fail to invoke GetNotMethod by lua errorCode:"+retCode+" errorMsg:"+result);
            }else {
                if (result == null){
                    System.out.println("GetVersion return empty value");
                }else {
                    System.out.println("GetVersion return value"+result);
                }
            }
    }

Link




鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap