Luajavabridge-lua simple solution for interoperability with Java

Source: Internet
Author: User
Tags lua

Introduction: Interaction between Android platform code and LUA code interacts with C + + and Java, and LUA interacts with C + + (Lua <==> C + + <==> Java)

I first met this Lua. Calling Java is the first way to write an interface function that calls Java with the help of A/C + + with JNI, and then export these functions to LUA using tolua++. The biggest problem with this approach is that it is too cumbersome, and a little bit of modification is necessary to recompile, seriously reducing the development efficiency.

Later learned that luaj this thing, found before the practice is simply too stupid.

Luaj Main Features:

1. Java Class Static Method can be called from Lua

2. Support for Int/float/boolean/string/lua function five parameter types when calling Java method

3. You can pass LUA function as a parameter to Java and have Java save a reference to Lua function

4. You can call LUA's global functions from Java, or invoke the Lua fuction that references point to

GL thread and UI thread concepts:

GL Thread: Responsible for image rendering (environment switch Java->lua)

UI thread: The user interface responsible for the Android system (environment switch Lua->java)

1. After the cocos2d-x is started, the LUA code will be called by the GL thread, so the Java method called from Lua, if it involves the display of the system user interface, the update operation, then must let this part of the code switch to the UI thread to run.

2, and vice versa, when calling Lua code from Java, this call needs to be executed on the GL thread, otherwise the LUA code will not be able to update the COCOS2D-X internal state, although it executes.

Use Luaj.callstaticmethod in Lua to invoke Java static methods:

//The func here is a callback function
functionJniutil:calljavacallbacklua (func)LocalLuaj =require "Cocos.cocos2d.luaj" LocalClassName ="org/cocos2dx/lua/appactivity" Localargs = {"Callbacklua", Func}//ParameterLocalSIGs ="(ljava/lang/string;i) V"//Parameter type LocalOK = Luaj.callstaticmethod (ClassName,"Callbacklua", Args,sigs)if notOk Then Print("============= Call callback error") Else Print("-------------Call Callback Success") EndEnd

In appactivity, add a static method:

 static  int  luafunccallback = 0; public  static  void  Callbacklua (final  String tipinfo,< Span style= "color: #0000ff;" >final  int   Luafunc) { //  luafunccallback = Luafunc;  //  Tipinfo "Callbacklua" from Lua is useless here. } 

J Ava calls the Lua method:

 Public Static native intCallluafunctionwithstring (intLuafunctionid, String value);//Call local lua-function Public Static native intCallluaglobalfunctionwithstring (String luafunctionname, String value);//Calling Global Lua-function Public Static native intRetainluafunction (intLuafunctionid);//retain one time lua-function Public Static native intReleaseluafunction (intLuafunctionid);//release off Lua-function

Usage scenarios: For example, we purchased the diamond callback into the game in the Payment SDK

Java calls Lua Method 1:

Sinstance.runonglthread (new  Runnable () {  @Override  publicvoid  Run () {    "success");    Cocos2dxluajavabridge.releaseluafunction (Luafunccallback);  }});

Java calls Lua Method 2:

Sinstance.runonglthread (new  Runnable () {    @Override    publicvoid  Run () {            //G_callbackfromjava as a global method    in Lua Cocos2dxluajavabridge.callluaglobalfunctionwithstring ("G_callbackfromjava", "success");    });

Luajavabridge-lua simple solution for interoperability with Java

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.