android 调用 python

发布时间:2019-07-29 10:28:41编辑:auto阅读(1798)

    文章可以帮你解决android下调用python方法获取返回值,往python函数中传参等。。。

    我这里使用AS,如果使用ec开发的直接看 http://www.srplab.com/cn/index.html 官方下载的开发包 里面有demo,我下载了可以跑通;

    不管是不是AS和ec,开始还是去看下CLE官网的开发包吧,下载下来看看,ec的有例子,可以编译过;
    http://www.srplab.com/cn/index.html

    开发包下载: http://www.srplab.com/cn/files/products.html

    你可能会遇到的问题

    1:注意so版本;版本都要一致,从开发包里复制粘贴;
    2:注意so引入路径;as和ec是有区别的;


    有什么问题大家可以一起讨论,之前找了很多资料,只有这个最后成功了,但是开始也有很多问题,还有通过JNI的 , 但是引入python.h 和python libs有问题VC上可以,在AS这个开发工具上怎么搞不知道,,现在这种移植也还行,简单,比较方便;之前还看了SL4A那个 也太扯淡了 ; jython在jvm环境下编译没毛病,一个jar搞定,但是如果在android下编译,那就不能用了,dvm和jvm确实不一样的;;;还是第一次直观的表现出来;还有一些其它的,太难懂,就不一一说了;


    如下图:

    libs的和jniLibs里面的so;
    这里写图片描述

    assets中放.py的文件和python的zip包;

    我的add.py文件的代码

    import time
    
    def get_time():
        return time.time();
    
    print("sin sin sin sin sin")
    
    def add_num(a,b):  // 相加
        return a+b
    
    def get_array(a): // 外边传入一个数组
        return a[0]  
    
    def get_Str():  // 返回字符串
        return "sin niu bi"

    里面import time 这里会爆红,需要把 time.cpython-34m.so放到assets下面可以执行get_time()这个函数;

    下面是代码;

    public class MainActivity extends Activity {
    
        static {
            System.loadLibrary("native-lib");
        }
    
        public native String stringFromJNI();
    
    
        public StarSrvGroupClass SrvGroup;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            TextView tv1 = (TextView) findViewById(R.id.sample_text1);
            TextView tv2 = (TextView) findViewById(R.id.sample_text2);
            TextView tv3 = (TextView) findViewById(R.id.sample_text3);
            TextView tv4 = (TextView) findViewById(R.id.sample_text4);
            TextView tv5 = (TextView) findViewById(R.id.sample_text5);
    //        tv.setText(stringFromJNI());
    
            // // TODO: 2018/5/4
    
            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.4.zip");
            if (!python_libFile.exists()) {
                try {
                    copyFile(this, "python3.4.zip", null);
                } catch (Exception e) {
                    System.out.println("cccccccc///  " + e);
                }
            }
    
            try {
                copyFile(this, "add.py", "");
            } catch (Exception e) {
                System.out.println("aaaaaaaaa///  " + e);
            }
    
    
            try {
    //            copyFile(this, "_struct.cpython-34m.so", null);
    //            copyFile(this, "binascii.cpython-34m.so", null);
                copyFile(this, "time.cpython-34m.so", null);
                copyFile(this, "zlib.cpython-34m.so", null);  //     这里zlib需要加上,不知道具体用法,不加会报zlib引入错误;估计是so里面有使用;
            } catch (Exception e) {
                System.out.println("DDDDDD/// " + e);
            }
    
            try {
                //--load python34 core library first;
    
                System.load(this.getApplicationInfo().nativeLibraryDir + "/libpython3.4m.so");
                Log.e("aaaaa", "aaaaaaaaaaaaa-->" + this.getApplicationInfo().nativeLibraryDir + "/libpython3.4m.so");
            } catch (UnsatisfiedLinkError ex) {
                Log.e("aaaaa", "bbbbbbbbbbbbbbb");
                System.out.println("bbbbbbbbbb///   " + ex.toString());
                Log.e("ssssss", "qweqweqwe-->" + this.getApplicationInfo().nativeLibraryDir + "/libpython3.4m.so");
            }
    
            /*----init starcore----*/
            StarCoreFactoryPath.StarCoreCoreLibraryPath = this.getApplicationInfo().nativeLibraryDir;
            StarCoreFactoryPath.StarCoreShareLibraryPath = this.getApplicationInfo().nativeLibraryDir;
            StarCoreFactoryPath.StarCoreOperationPath = "/data/data/" + getPackageName() + "/files";
    
            StarCoreFactory starcore = StarCoreFactory.GetFactory();
            StarServiceClass Service = starcore._InitSimple("test", "123", 0, 0);
            SrvGroup = (StarSrvGroupClass) Service._Get("_ServiceGroup");
            Service._CheckPassword(false);
    
            /*----run python code----*/
            SrvGroup._InitRaw("python34", Service);
            StarObjectClass python = Service._ImportRawContext("python", "", false, "");
            python._Call("import", "sys");
    
            StarObjectClass pythonSys = python._GetObject("sys");
            StarObjectClass pythonPath = (StarObjectClass) pythonSys._Get("path");
            pythonPath._Call("insert", 0, "/data/data/" + getPackageName() + "/files/python3.4.zip");
            pythonPath._Call("insert", 0, this.getApplicationInfo().nativeLibraryDir);
            pythonPath._Call("insert", 0, "/data/data/" + getPackageName() + "/files");
    
    
            //
            String CorePath = "/data/data/" + getPackageName() + "/files";
            Service._DoFile("python", CorePath + "/add.py", "");
            int _Callint = python._Callint("add_num", 5, 8);
            Log.e("ssssss", "_Callint: " + _Callint);
    
    
            Object add_num1 = python._Call("add_num", 1, 6);
            int a = (int) add_num1;
            Log.e("aaa", a + "");
    
            tv1.setText("python中计算5+8:   " + _Callint + "");
            tv2.setText("python中计算1+6:   " + a + "");
    
    
            short[] arrS = {456, 254, 693};  //get_array
            Object get_array = python._Call("get_array", arrS);
            String s = get_array.toString();
    //        short[] aaa = (short[]) get_array;
            Log.e("ccc", "aaa.len: " + s);
    
            Object get_str = python._Call("get_Str");
            Log.e("dddd", "get_str: " + (String) get_str);
    
            tv3.setText("往python中传递一个数组 {456, 254, 693},python中获取第一个值并返回:   " + s + "");
            tv4.setText("python返回一个字符串:   " + (String) get_str + "");
    
    
            Object get_time = python._Call("get_time");
            Log.e("tttt","get_time: "+get_time);
    
        }
    
    
        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();
            //}
        }
    
    
    }

    好了,大功告成,总之现在是可以调用了,还不知道在复杂的python代码下能不能经得起考验 ,反正目前这样看着是没有问题的;有问题及时讨论。。。
    另外,刚开始网上找解决办法看的这篇文章: https://blog.csdn.net/yingshukun/article/details/78571992
    这里面也有其他方式调用python,有兴趣也可以看看;
    此处谢谢大佬分享。


关键字