CLE的使用笔记
CLE官网http://www.srplab.com/cn/index.html
下载CLE for Android,解压文件,拷贝 starcore_android_rX.X.jar到工程的libs目录,拷贝 libstar_java.so、libstarcore.so、libpython3.6m.so和libstar_python36.so到libs/x86目录,拷贝编写的Python源码和python需要的环境及标准库到src/main/assets目录.需要的python的.so文件可下载
android.python.x.x.x,解压获得。在build.gradle中android {}中添加
sourceSets.main.jniLibs.srcDirs = ['libs']
使用JAVA初始化CLE http://www.srplab.com/cn/files/cle_initialization.html
1 import com.srplab.www.starcore.*; 2 public class test_server{ 3 4 StarCoreFactoryPath.StarCoreCoreLibraryPath = this.getApplicationInfo().nativeLibraryDir; 5 StarCoreFactoryPath.StarCoreShareLibraryPath = this.getApplicationInfo().nativeLibraryDir; 6 StarCoreFactoryPath.StarCoreOperationPath = "/data/data/"+getPackageName()+"/files"; 7 public static void main(String[] args){ 8 StarCoreFactory starcore=StarCoreFactory.GetFactory(); 9 StarServiceClass Service=starcore._InitSimple("test","123",0,0); 10 StarSrvGroupClass SrvGroup = (StarSrvGroupClass)Service._Get("_ServiceGroup"); 11 12 ...... 13 SrvGroup._ClearService(); 14 starcore._ModuleExit(); 15 } 16 }
java调用 python http://www.srplab.com/cn/files/cle_javacallothers.html
- 调用函数"_InitRaw"初始化python接口
- 调用函数"_LoadRawModule"加载python脚本文件
- 使用函数"_ImportRawContext("python","",false,nil);"获取python全局原生对象,python类
- 使用函数"XX._Call("_StarCall",Args...)"或者"XX._New("","",Args,..)"创建python类的实例
- 使用函数"XX._Call("funcname",...)"或者"XX._Get/_Set("Variable")"调用python函数,设置/获取python变量数值
python调用java http://www.srplab.com/cn/files/cle_pythoncallothers.html
- 调用函数 "_InitRaw" 初始化java接口
- 调用函数 "_LoadRawModule" 加载java脚本文件
- 使用函数 "_ImportRawContext("java","",False,"");" 获取java类
- 使用函数 "XX(Args...)" 创建java类的实例
- 使用函数 "XX.func(...)"或者"XX.Variable"调用java函数,获取/设置java变量数值
1 /*----run python code----*/ 2 SrvGroup._InitRaw("python36", Service); //调用函数_InitRaw初始化python接口 3 StarObjectClass python = Service._ImportRawContext("python", "", false, ""); 4 // 设置Python模块加载路径 5 python._Call("import", "sys"); 6 StarObjectClass pythonSys = python._GetObject("sys"); 7 StarObjectClass pythonPath = (StarObjectClass) pythonSys._Get("path"); 8 pythonPath._Call("insert", 0, appFile.getPath() + File.separator + "python3.6.zip"); 9 pythonPath._Call("insert", 0, appLib); 10 pythonPath._Call("insert", 0, appFile.getPath()); 11 12 //调用Python代码 13 Service._DoFile("python", appFile.getPath() + "/py_code.py", ""); 14 long time = python._Calllong("get_time"); 15 Log.d("", "form python time=" + time); 16 tv1.setText("time:"+String.valueOf(time) + ""); 17 //调用Python代码 18 Service._DoFile("python", appFile.getPath() + "/test.py", ""); 19 int result = python._Callint("add", 5, 2); 20 Log.d("", "result=" + result); 21 tv2.setText("add:"+result + ""); 22 //将一个java类设置给Python,变成Python类 23 python._Set("JavaClass", Log.class); 24 Service._DoFile("python", appFile.getPath() + "/calljava.py", "");
出现问题:ModuleNotFoundError("No module named 'encodings'",)Py_Initialize: unable to load the file system codec
A/libc: Fatal signal 6 (SIGABRT), code -6 (SI_TKILL) in tid 4184
解决方法:似乎是python版本的问题。
可以查看File appFile = new File("/data/data/"+getPackageName()+"/files");中appFile的值,是否包含多个版本的.so文件