|
一.环境准备
在官网下载最新的CLE for Android 开发包,其中包含示例工程和API文档。解压开发包,拷贝如下文件
重点:这种方式如果是第三方或者是android 环境运行不了的插件就不适用了,只适用一些简单的插件
http://www.srplab.com/cn/files/products.html

你可能会遇到的问题
1:注意so版本;版本都要一致,从开发包里复制粘贴; 2:注意so引入路径;as和ec是有区别的;
拷贝 starcore_android_rX.X.jar的arm64-v8a目录内容到工程的libs目录 拷贝starcore_android_rX.X.jar的到starcore_android_r3.7.dex、starcore_android_r3.7.jar到libs目录,
asset中的so在android.python.3.4.5.zip包和android.python.3.7.0.zip包解压出来的, 如下图:
libs的和assets里面的so;

二、编写Python代码
test.py文件内容如下:
def add(x,y) : return x+y calljava.py内容如下:
import imp #test load path def log(content): JavaClass.d("formPython",content) log("Hello Android,form python") py_code.py内容如下:
import time def get_time(): return time.time() 将py_code.py压缩为py_code.zip文件。将编写的Python源码放入Android 工程的assets目录,其中还要包含一些Python需要的环境及标准库(android.python.3.7.0里面arm64-v8a\lib-dynload中找到对应的so库),见下图
三、编写Android相关代码,初始化CLE并调用Python
package com.juchiwang.pythonText;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.widget.TextView;
import com.srplab.www.starcore.StarCoreFactory;
import com.srplab.www.starcore.StarCoreFactoryPath;
import com.srplab.www.starcore.StarMsgCallBackInterface;
import com.srplab.www.starcore.StarObjectClass;
import com.srplab.www.starcore.StarServiceClass;
import com.srplab.www.starcore.StarSrvGroupClass;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivityPython extends AppCompatActivity {
public StarSrvGroupClass SrvGroup;
private StarServiceClass Service;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView textView = findViewById(R.id.text);
textView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
AsyncTask.execute(new Runnable() {
@Override
public void run() {
loadPy();
}
});
}
});
}
void loadPy() throws IOException {
//拷贝Python相关环境
File destDir = new File("/data/data/" + getPackageName() + "/files");
if (!destDir.exists())
destDir.mkdirs();
java.io.File python_libFile = new java.io.File("/data/data/" + getPackageName() + "/files/python3.7.zip");
if (!python_libFile.exists()) {
try {
copyFile(this, "python3.7.zip", null);
} catch (Exception e) {
System.out.println("cccccccc/// " + e);
}
}
// 拷贝Python 代码
try {
copyFile(this, "dianzan.py", "");
} catch (Exception e) {
System.out.println("aaaaaaaaa/// " + e);
}
copyFile(this, "python3.7.zip",null);
copyFile(this, "_struct.cpython-37m.so",null);
copyFile(this, "binascii.cpython-37m.so",null);
copyFile(this, "time.cpython-34m.so",null);
copyFile(this, "zlib.cpython-37m.so",null);
try {
// 加载Python解释器/data/app/com.ss.android.ugc.awemeText-RkDXzKEW3yrWkTsfikKBLw==/lib/arm64-v8a/
System.load(this.getApplicationInfo().nativeLibraryDir + "/libpython3.7m.so");
// 除了将代码直接拷贝,还支持将代码压缩为zip包,通过Install方法解压到指定路径
// InputStream dataSource = getAssets().open("dianzan.zip");
// StarCoreFactoryPath.Install(dataSource, this.getApplicationInfo().nativeLibraryDir,true );
} catch (Exception e) {
e.printStackTrace();
}
/*----init starcore----*/
StarCoreFactoryPath.StarCoreCoreLibraryPath = this.getApplicationInfo().nativeLibraryDir;
StarCoreFactoryPath.StarCoreShareLibraryPath = this.getApplicationInfo().nativeLibraryDir;
StarCoreFactoryPath.StarCoreOperationPath = "/data/data/" + getPackageName() + "/files";
final StarCoreFactory starcore = StarCoreFactory.GetFactory();
starcore._SRPLock();
SrvGroup = starcore._GetSrvGroup(0);
Service = SrvGroup._GetService("test","123");
if( Service == null ){
Service = starcore._InitSimple("test", "123", 0, 0);
}else{
Service._CheckPassword(false);
}
Service._CheckPassword(false);
// StarServiceClass Service = starcore._InitSimple("test", "123", 0, 0);
// SrvGroup = (StarSrvGroupClass) Service._Get("_ServiceGroup");
// Service._CheckPassword(false);
/*----run python code----*/
SrvGroup._InitRaw("python37", Service);
StarObjectClass python = Service._ImportRawContext("python", "", false, "");
// 设置Python模块加载路径
python._Call("import", "sys");
StarObjectClass pythonSys = python._GetObject("sys");
StarObjectClass pythonPath = (StarObjectClass) pythonSys._Get("path");
pythonPath._Call("insert", 0, "/data/data/" + getPackageName() + "/files/python3.7.zip");
pythonPath._Call("insert", 0, this.getApplicationInfo().nativeLibraryDir);
pythonPath._Call("insert", 0, "/data/data/" + getPackageName() + "/files");
//
starcore._SRPUnLock();
//调用Python代码
python._Set("dianzan.py", Log.class);
Service._DoFile("python", "/data/data/" + getPackageName() + "/files/dianzan.py", "");
Object add_num1 = python._Call("getItchat");
Object add_num2 = python._Call("NickName");
// Object add_num1 = python._Call("get_time");
// int a = (int) add_num1;
Log.e("aaa", add_num1 + "---"+ add_num2);
// Intent lan = getPackageManager().getLaunchIntentForPackage("com.juchiwang.pythonText");
// Intent intent = new Intent(Intent.ACTION_MAIN);
// intent.addCategory(Intent.CATEGORY_LAUNCHER);
// intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
// intent.setComponent(lan.getComponent());
// startActivity(intent);
}
private void copyFile(Activity c, String Name, String desPath) throws IOException {
File outfile = null;
if (desPath != null)
outfile = new File("/data/data/" + getPackageName() + "/files/" + desPath + Name);
else
outfile = new File("/data/data/" + getPackageName() + "/files/" + Name);
//if (!outfile.exists()) {
outfile.createNewFile();
FileOutputStream out = new FileOutputStream(outfile);
byte[] buffer = new byte[1024];
InputStream in;
int readLen = 0;
if (desPath != null)
in = c.getAssets().open(desPath + Name);
else
in = c.getAssets().open(Name);
while ((readLen = in.read(buffer)) != -1) {
out.write(buffer, 0, readLen);
}
out.flush();
in.close();
out.close();
}
}
|