Click here to Skip to main content
13,005,608 members (62,897 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as


2 bookmarked
Posted 28 Feb 2012

Calling Python from Java for Android using CLE and SL4A

, 28 Feb 2012
Rate this:
Please Sign up or sign in to vote.
Calling Python from Java for Android using CLE and SL4A

Java is major language to develop applications on Android platform. But, in some cases, programmers want to call Python code from Java to perform some functions.

By now, there is no direct method to write programs 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 follows:

Java calls Python functions:

Python Function

def Obj_PythonAdd(self,x,y) :
    return x+y;
Obj.PythonAdd = Obj_PythonAdd;

Java Code

StarObjectClass a = Service._GetObject("TestClass")._New();
Callback of python 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;

Python Code


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 (Integrate CLE With Your Project)

  1. Install SL4A from
  2. Download devfiles from, and then Open Eclipse
  3. Create project for Android.
  4. Add CLE libraries to project as follows:

  5. Python code:
    SrvGroup = libstarpy._GetSrvGroup()
    Service = SrvGroup._GetService("","")
    #Create objects
    #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 code:
    package com.cle.pythonfromjava;
    import android.os.Bundle;
    import android.content.res.AssetManager;
    import com.srplab.www.starcore.*;
    public class PytonfromjavaActivity extends Activity {
        /** Called when the activity is first created. */
        public void onCreate(Bundle savedInstanceState) {
            //--init CLE
    StarCoreFactory starcore= StarCoreFactory.GetFactory();
    StarServiceClass Service=starcore._InitSimple("test","123",0,0);
    AssetManager assetManager = getAssets();     
                String pythonbuf;
                   InputStream dataSource ="");
                   int size=dataSource.available();
                   byte[] buffer=new byte[size]; 
                   pythonbuf=new String(buffer);
            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;

Method2 Source Code (Install cle from independently):

  1. Install SL4A from
  2. Install CLE from
  3. Download devfiles from, and then Open Eclipse
  4. Create project for Android
  5. Python code:
    Same as method 1
  6. Java code:
    Delete the following code line. Others are the same as method1.

Examples may be downloaded from


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

China China
No Biography provided

You may also be interested in...


Comments and Discussions

QuestionApp Crashes Pin
Member 1247786821-Apr-16 23:55
memberMember 1247786821-Apr-16 23:55 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.170627.1 | Last Updated 28 Feb 2012
Article Copyright 2012 by li9705
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid