Click here to Skip to main content
13,042,630 members (80,958 online)
Click here to Skip to main content
Add your own
alternative version

Stats

15.7K views
3 bookmarked
Posted 1 Mar 2012

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

  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)

Share

About the Author

li9705
China China
No Biography provided

You may also be interested in...

Pro

Comments and Discussions

 
-- There are no messages in this forum --
Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170713.1 | Last Updated 1 Mar 2012
Article Copyright 2012 by li9705
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid