Giter Site home page Giter Site logo

lerist / androidluaexample Goto Github PK

View Code? Open in Web Editor NEW

This project forked from haodynasty/androidluaexample

0.0 3.0 0.0 1.19 MB

Lua 5.3.3 and LuaJava ported to Android example

License: MIT License

Java 9.58% Lua 1.82% Makefile 0.95% C 82.11% C++ 5.55%

androidluaexample's 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

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

androidluaexample's People

Contributors

haodynasty avatar

Watchers

James Cloos avatar Lerist avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.