【问题标题】:Use .so library for android in python在 python 中为 android 使用 .so 库
【发布时间】:2023-01-24 23:58:23
【问题描述】:

我需要使用为 android (jni) 开发的 .so 库,但我需要在 python 中使用它

安卓代码:

package x2;

import android.os.Build;

import java.util.Base64;

public class X {
        static {
            System.loadLibrary("adlemx");
        }

        public static String m0do(String str) {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                return Base64.getEncoder().encodeToString(x01(str).getBytes());
            }
            return "";
        }

        public static native String x01(String str);
    }

我试过使用 ctypes

from ctypes import cdll

print(cdll.LoadLibrary("./adlemx.so"))

但我得到错误:

Traceback (most recent call last):
  File "D:\_Projects\DiaryBackend\main.py", line 24, in <module>
    print(cdll.LoadLibrary("./adlemx.so"))
  File "C:\Program Files\Python39\lib\ctypes\__init__.py", line 452, in LoadLibrary
    return self._dlltype(name)
  File "C:\Program Files\Python39\lib\ctypes\__init__.py", line 374, in __init__
    self._handle = _dlopen(self._name, mode)
OSError: [WinError 193] %1 is not an application Win32

【问题讨论】:

    标签: python android-ndk ctypes .so


    【解决方案1】:

    尝试创建名为“adlemx.py”的文件 并粘贴此代码

    def __bootstrap__():
       global __bootstrap__, __loader__, __file__
       import sys, pkg_resources, imp
       __file__ = pkg_resources.resource_filename(__name__,'adlemx.so')
       __loader__ = None; del __bootstrap__, __loader__
       imp.load_dynamic(__name__,__file__)
    __bootstrap__()
    

    然后导入这个文件

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-09-04
      • 2010-09-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多