Calling python from java for android using CLE and SL4A
Java is major language to develop applications on android platform. But, in some case, programmers want to call python code from java to perform some functions. By now, there is no direct method to write program with python.
SL4A is an open project, which supports script languages including python, lua, etc. But calling python from java has not been supported directly. Programmers have to use JNI method, and native code to write interface for python. This is more difficult, you have to manage relationship between java objects and python objects, kinds of references, interface parameters conversion, and callback functions from python to java.
CLE(Common Language Extension) is a middleware which presents functions to aid multi-language calls between scripts, including java and python. Using CLE, the above problems can be solved easily. Architecture of CLE is as follow: 

Java calls python functions:
python function:
Obj=Service._New("TestClass");
def Obj_PythonAdd(self,x,y) :
return x+y;
Obj.PythonAdd = Obj_PythonAdd;
java code:
StarObjectClass a = Service._GetObject("TestClass")._New();
a._Call("PythonAdd",12,34));
Callback of python 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;
}
});
python code:
Obj.JavaAdd(x,y)
Java gets object’s attributes defined in python
Python: Obj.PythonValue = 200;
Java: a._Get("PythonValue")
Python gets object’s attributes defined in java
Java : a._Set("JavaValue",100);
Python: self.JavaValue
Method1 source code ( intergrate CLE with your project):
1. Install SL4A from http://code.google.com/p/android-scripting/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 CLE libraries to project as follows:



5. Python codes
SrvGroup = libstarpy._GetSrvGroup()
Service = SrvGroup._GetService("","")
#Create objects
Obj=Service._New("TestClass");
#Define functions
def Obj_PythonAdd(self,x,y) :
print("Call python function...");
return x+y;
Obj.PythonAdd = Obj_PythonAdd;
#Call java functions
def Obj_PythonPrint(self,x,y) :
print( "Value defined in java is ",self.JavaValue );
print( "Function result from java ",self.JavaAdd(x,y) );
Obj.PythonPrint = Obj_PythonPrint;
#define Attributes
Obj.PythonValue = 200;
6. Java Codes:
package com.cle.pythonfromjava;
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 PytonfromjavaActivity 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.pythonfromjava");
StarCoreFactory starcore= StarCoreFactory.GetFactory();
StarServiceClass Service=starcore._InitSimple("test","123",0,0);
Service._CheckPassword(false);
AssetManager assetManager = getAssets();
try{
String pythonbuf;
InputStream dataSource = assetManager.open("code.py");
int size=dataSource.available();
byte[] buffer=new byte[size];
dataSource.read(buffer);
dataSource.close();
pythonbuf=new String(buffer);
Service._RunScript("python",pythonbuf,"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("PythonValue"));
System.out.println(a._Call("PythonAdd",12,34));
a._Call("PythonPrint",56,78);
}
}
Method2 source code ( install cle from http://code.google.com/p/cle-for-android independently):
1. Install SL4A from http://code.google.com/p/android-scripting/downloads/list
2. Install CLE from http://code.google.com/p/cle-for-android/downloads/list
3. Download devfiles from http://code.google.com/p/cle-for-android, and then Open Eclipse
4. Create project for android.
5. Python 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.pythonfromjava");
Examples may be downloaded from http://www.srplab.com/android/calling_python_from_java.rar
Examples may be downloaded from http://www.srplab.com/android/calling_python_from_java.rar
