android开发调⽤python脚本
⼀.环境准备
在官⽹下载最新的CLE for Android 开发包,其中包含⽰例⼯程和API⽂档。解压开发包,拷贝如下⽂件
重点:这种⽅式如果是第三⽅或者是android 环境运⾏不了的插件就不适⽤了,只适⽤⼀些简单的插件
你可能会遇到的问题
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
1package com.juchiwang.pythonText;
2
3import android.app.Activity;
t.Context;
t.Intent;
6import android.os.AsyncTask;
7import android.os.Bundle;
8import android.os.Handler;
9import android.util.Log;
10import android.view.View;
11import android.widget.TextView;
12
13import com.srplab.www.starcore.StarCoreFactory;
14import com.srplab.www.starcore.StarCoreFactoryPath;
15import com.srplab.www.starcore.StarMsgCallBackInterface;
16import com.srplab.www.starcore.StarObjectClass;
17import com.srplab.www.starcore.StarServiceClass;
18import com.srplab.www.starcore.StarSrvGroupClass;
19
20import java.io.BufferedInputStream;
21import java.io.BufferedOutputStream;
22import java.io.File;
23import java.io.FileNotFoundException;
24import java.io.FileOutputStream;
25import java.io.IOException;
26import java.io.InputStream;
27
28import androidx.annotation.Nullable;
29import androidx.appcompat.app.AppCompatActivity;
30
31public class MainActivityPython extends AppCompatActivity {
32 public StarSrvGroupClass SrvGroup;
33 private StarServiceClass Service;
34 @Override
35 protected void onCreate(@Nullable Bundle savedInstanceState) {
36 Create(savedInstanceState);
37 setContentView(R.layout.activity_main);
38 TextView textView = findViewById();
39 textView.setOnClickListener(new View.OnClickListener() {
40 @Override
41 public void onClick(View v) {
42 ute(new Runnable() {
43 @Override
44 public void run() {
45 loadPy();
46 }
47 });
48 }
49 });
50 }
51
52 void loadPy() throws IOException {
53 //拷贝Python相关环境
54 File destDir = new File("/data/data/" + getPackageName() + "/files");
55 if (!ists())
55 if (!ists())
56 destDir.mkdirs();
57 java.io.File python_libFile = new java.io.File("/data/data/" + getPackageName() + "/files/python3.7.zip");
58 if (!ists()) {
59 try {
60 copyFile(this, "python3.7.zip", null);
61 } catch (Exception e) {
62 System.out.println("cccccccc/// " + e);
63 }
64 }
65
66 // 拷贝Python 代码
67 try {
68 copyFile(this, "dianzan.py", "");
69 } catch (Exception e) {
70 System.out.println("aaaaaaaaa/// " + e);
71 }
72
73 copyFile(this, "python3.7.zip",null);
74 copyFile(this, "_struct.cpython-37m.so",null);
75 copyFile(this, "binascii.cpython-37m.so",null);
76 copyFile(this, "time.cpython-34m.so",null);
77 copyFile(this, "zlib.cpython-37m.so",null);
78
79 try {
80 // 加载Python解释器/data/app/com.ss.android.ugc.awemeText-RkDXzKEW3yrWkTsfikKBLw==/lib/arm64-v8a/
81 System.ApplicationInfo().nativeLibraryDir + "/libpython3.7m.so");
82 // 除了将代码直接拷贝,还⽀持将代码压缩为zip包,通过Install⽅法解压到指定路径
83// InputStream dataSource = getAssets().open("dianzan.zip");
84// StarCoreFactoryPath.Install(dataSource, ApplicationInfo().nativeLibraryDir,true );
85 } catch (Exception e) {
86 e.printStackTrace();
87 }
88
89 /*----init starcore----*/
90 StarCoreFactoryPath.StarCoreCoreLibraryPath = ApplicationInfo().nativeLibraryDir;
91 StarCoreFactoryPath.StarCoreShareLibraryPath = ApplicationInfo().nativeLibraryDir;
92 StarCoreFactoryPath.StarCoreOperationPath = "/data/data/" + getPackageName() + "/files";
93
94 final StarCoreFactory starcore = StarCoreFactory.GetFactory();
95 starcore._SRPLock();
96 SrvGroup = starcore._GetSrvGroup(0);
97 Service = SrvGroup._GetService("test","123");
98 if( Service == null ){
99 Service = starcore._InitSimple("test", "123", 0, 0);
100 }else{
101 Service._CheckPassword(false);
102 }
103 Service._CheckPassword(false);
104
105
106// StarServiceClass Service = starcore._InitSimple("test", "123", 0, 0);
107// SrvGroup = (StarSrvGroupClass) Service._Get("_ServiceGroup");
108// Service._CheckPassword(false);
109
110 /*----run python code----*/
111 SrvGroup._InitRaw("python37", Service);
112 StarObjectClass python = Service._ImportRawContext("python", "", false, "");
113 // 设置Python模块加载路径
114 python._Call("import", "sys");
115 StarObjectClass pythonSys = python._GetObject("sys");
116 StarObjectClass pythonPath = (StarObjectClass) pythonSys._Get("path");
117 pythonPath._Call("insert", 0, "/data/data/" + getPackageName() + "/files/python3.7.zip");
118 pythonPath._Call("insert", 0, ApplicationInfo().nativeLibraryDir);
119 pythonPath._Call("insert", 0, "/data/data/" + getPackageName() + "/files");
120 //
120 //
121 starcore._SRPUnLock();
122
123 //调⽤Python代码
124 python._Set("dianzan.py", Log.class);
125 Service._DoFile("python", "/data/data/" + getPackageName() + "/files/dianzan.py", "");
126 Object add_num1 = python._Call("getItchat");
127 Object add_num2 = python._Call("NickName");
128// Object add_num1 = python._Call("get_time");
129// int a = (int) add_num1;
130 Log.e("aaa", add_num1 + "---"+ add_num2);
131
132// Intent lan = getPackageManager().getLaunchIntentForPackage("com.juchiwang.pythonText"); 133// Intent intent = new Intent(Intent.ACTION_MAIN);
134// intent.addCategory(Intent.CATEGORY_LAUNCHER);
135// intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
136// intent.Component());
137// startActivity(intent);
138 }
139
140 private void copyFile(Activity c, String Name, String desPath) throws IOException {
141 File outfile = null;
142 if (desPath != null)
143 outfile = new File("/data/data/" + getPackageName() + "/files/" + desPath + Name);
144 else
145 outfile = new File("/data/data/" + getPackageName() + "/files/" + Name);
146 //if (!ists()) {
147 ateNewFile();
148 FileOutputStream out = new FileOutputStream(outfile);
149 byte[] buffer = new byte[1024];
150 InputStream in;
151 int readLen = 0;
python解释器下载
152 if (desPath != null)
153 in = c.getAssets().open(desPath + Name);
154 else
155 in = c.getAssets().open(Name);
156 while ((readLen = in.read(buffer)) != -1) {
157 out.write(buffer, 0, readLen);
158 }
159 out.flush();
160 in.close();
161 out.close();
162 }
163}
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论