Click here to Skip to main content
15,867,686 members
Articles / Mobile Apps / Android

Calling Lua from Java Android using CLE

Rate me:
Please Sign up or sign in to vote.
5.00/5 (1 vote)
1 Mar 2012CPOL1 min read 21.7K   3  
This article presents a method which uses CLE middleware to call Lua from a Java application on Android

Java is major language to develop applications on Android platform. But, in some cases, programmers want to call lua code from Java to perform some functions. There are many articles discussing this topic. Here, we present another method, which uses CLE middleware to call lua from Java application on Android.

CLE(Common Language Extension) is developed by srplab which presents functions to aid multi-language calls between scripts, including Java, Python, Lua, etc. A lua engine has been compiled into cle core library. Using CLE, Java calls lua code becomes very easy. Architecture of CLE is as follows:

Java Calls Lua Functions

Lua Function

Obj=Service:_New("TestClass");
function Obj:LuaAdd(x,y)
return x+y;
end

Java Code

Java
StarObjectClass a = Service._GetObject("TestClass")._New();
a._Call("LuaAdd",12,34));

Callback of Lua to Java

Java Callback Function

Java
StarObjectClass a = Service._GetObject("TestClass")._New()._Assign(new StarObjectClass(){
public int JavaAdd(StarObjectClass self,int x,int y){
return x+y;
}
});

Lua Code

Obj:JavaAdd(x,y)

Java Gets Object’s Attributes Defined in Lua

Lua: Obj.LuaValue = 200;
Java: a._Get("LuaValue")

Lua Gets Object’s Attributes Defined in Java

Java : a._Set("JavaValue",100);
Lua : self.JavaValue

Method 1 Source Code (Integrate CLE With Your Project)

  1. Download devfiles from http://code.google.com/p/cle-for-android and then Open Eclipse.
  2. Create project for Android.
  3. Add CLE libraries to project as follows:

  4. Lua codes:
    SrvGroup = libstarcore._GetSrvGroup()
    Service = SrvGroup:_GetService("","")
     
    --Create objects
    Obj=Service:_New("TestClass");
     
    --Define functions
    function Obj:LuaAdd(x,y)
    print("Call lua function...");
    return x+y;
    end 
     
    --Call java functions
    function Obj:LuaPrint(x,y)
    print( "Value defined in java is ",self.JavaValue );
    print( "Function result from java ",self:JavaAdd(x,y) );
    end
     
    --define Attributes
    Obj.LuaValue = 200;<br />
  5. Java codes:
    package com.cle.luafromjava;
     
    import android.app.Activity;
    import android.os.Bundle;
    import android.content.res.AssetManager;
     
    import java.io.IOException;
    import java.io.InputStream;
     
    import com.srplab.www.starcore.*;
     
    public class LuafromjavaActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
     
    //--init CLE
    StarCoreFactoryPath.InitDefault(Runtime.getRuntime(),"/data/data/com.cle.luafromjava");
     
    StarCoreFactory starcore= StarCoreFactory.GetFactory();
    StarServiceClass Service=starcore._InitSimple("test","123",0,0);
    Service._CheckPassword(false);
     
    AssetManager assetManager = getAssets(); 
     
    try{
    String luabuf;
     
    InputStream dataSource = assetManager.open("code.lua");
    int size=dataSource.available();
    byte[] buffer=new byte[size]; 
    dataSource.read(buffer); 
    dataSource.close(); 
    luabuf=new String(buffer);
     
    Service._RunScript("lua",luabuf,"cmd","");
    }
    catch(IOException e ){
    } 
     
    StarObjectClass a = Service._GetObject("TestClass")._New()._Assign(new StarObjectClass(){
    public int JavaAdd(StarObjectClass self,int x,int y){
    System.out.println("Call java function...");
    return x+y;
    }
    });
    a._Set("JavaValue",100);
    System.out.println(a._Get("LuaValue")); 
    System.out.println(a._Call("LuaAdd",12,34)); 
    a._Call("LuaPrint",56,78); 
    }
    }

Method 2 Source Code

(Install cle from http://code.google.com/p/cle-for-android independently.)

  1. Install CLE from http://code.google.com/p/cle-for-android/downloads/list
  2. Download devfiles from http://code.google.com/p/cle-for-android, and then Open Eclipse
  3. Create project for Android.
  4. Add starcore_android_r3.jar to project
  5. Lua codes
    Same as method 1
  6. Java codes

    Delete the following code line. Others are the same as method 1.

    StarCoreFactoryPath.InitDefault(Runtime.getRuntime(),"/data/data/com.cle.luafromjava");

Examples can be downloaded from http://www.srplab.com/android/calling_lua_from_java.rar.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
China China
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
-- There are no messages in this forum --