joaoventura / pybridge

Reuse Python code in native Android applications
215 stars 56 forks source link

How to include python .so modules (e.g. select.so) for all architectures? #22

Closed kabukky closed 5 years ago

kabukky commented 5 years ago

Hi, is there a way to include the $(CRYSTAX_PATH)/sources/python/3.5/libs/$(TARGET_ARCH_ABI)/modules/select.so file for all architectures?

I know I can copy the select.so to the assets/python folder - that works. But that will only include it for one architecture.

I tried it like this, in the Android.mk, but the python interpreter can't find it:

# Include select.so

include $(CLEAR_VARS)
LOCAL_MODULE    := select
LOCAL_SRC_FILES := $(CRYSTAX_PATH)/sources/python/3.5/libs/$(TARGET_ARCH_ABI)/modules/select.so
include $(PREBUILT_SHARED_LIBRARY)

Error:

com.jventura.pyapp I/pybridge: ImportError: No module named 'select'
kabukky commented 5 years ago

Here's the workaround I'm using now:

In PyBridge.java:

    public static JSONObject call(JSONObject payload) {
        try {
            String[] abis = Build.SUPPORTED_ABIS;
            payload.put("architecture", abis[0]);
            String result = call(payload.toString());
            return new JSONObject(result);
        } catch (JSONException e) {
            e.printStackTrace();
            return null;
        }
    }

In boostrap.py:

 import os
 sys.path.insert(0, os.path.dirname(os.path.realpath(__file__)) + '/libs/' + args['architecture'])

Then I put the .so files in the corresponding assets/python/libs/<arch> folders.

That works fine. Is there a better way?

ZhangKuixun commented 5 years ago

I have the same problem. How did you solve it?

kabukky commented 5 years ago

@ZhangKuixun see my workaround above.

You should put

 import os
 sys.path.insert(0, os.path.dirname(os.path.realpath(__file__)) + '/libs/' + args['architecture'])

at the top of the python function you are calling since that is where you will have access to args['architecture'].

ZhangKuixun commented 5 years ago

Failure, there are still the same mistakes @kabukky

kabukky commented 5 years ago

@ZhangKuixun this issue is about including .so files for ALL architectures. However, I've commented in your issue about the missing _ctypes.so, which is is separate issue.

ZhangKuixun commented 5 years ago

File "/data/user/0/com.jventura.pyapp/assets/python/stdlib.zip/platform.py", line 115, in File "/data/user/0/com.jventura.pyapp/assets/python/stdlib.zip/subprocess.py", line 438, in ImportError: No module named 'select'

sys.path.insert(0, os.path.dirname(os.path.realpath(__file__)) + '/libs/arm64-v8a') in bootstrap.py

there are still the same mistakes @kabukky

ZhangKuixun commented 5 years ago

_ctypes.so has been solved, I annotate # import ctypes.

ZhangKuixun commented 5 years ago

this is my demo, https://github.com/ZhangKuixun/pybridge Can you take a look at it for me? @kabukky

joaoventura commented 5 years ago

@ZhangKuixun, please don't open issues begging for user support like you have been doing..