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
StarObjectClass a = Service._GetObject("TestClass")._New();
a._Call("LuaAdd",12,34));
Callback of Lua to Java
Java Callback Function
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)
- Download devfiles from http://code.google.com/p/cle-for-android and then Open Eclipse.
- Create project for Android.
- Add CLE libraries to project as follows:
- 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 />
- 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.)
- Install CLE from http://code.google.com/p/cle-for-android/downloads/list
- Download devfiles from http://code.google.com/p/cle-for-android, and then Open Eclipse
- Create project for Android.
- Add starcore_android_r3.jar to project
- Lua codes
Same as method 1 - 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.