srplab's blog

Discuss about multi language programming of c/c ,java,c#,python,etc. Distribute object middleware

Post sort order : index

14/28 POSTS
preview
13-15/28 POSTS

close

Calling lua from java android using CLE

Feb 28, 2012 10:51 PMPublicPageviews 302 0

        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
Method1 source code ( intergrate 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;
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);        
    }
}
Method2 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 method1
StarCoreFactoryPath.InitDefault(Runtime.getRuntime(),"/data/data/com.cle.luafromjava");

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

Report abuse for this article

Copyright © 2013 Yahoo!, Inc. All rights reserved