Click here to Skip to main content
Click here to Skip to main content

Calling Lua from Java Android using CLE

, 1 Mar 2012
Rate this:
Please Sign up or sign in to vote.
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 case, 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 follow:

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 call back 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)

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 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)

About the Author

li9705

China China
No Biography provided

Comments and Discussions

 
-- There are no messages in this forum --
| Advertise | Privacy | Mobile
Web04 | 2.8.140709.1 | Last Updated 1 Mar 2012
Article Copyright 2012 by li9705
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid