我想使用NDK从Android到JNI运行一些Python代码,如下所示:
#include <jni.h>
#include <string>
#include <stdio.h>
#include <Python.h>
extern "C" JNIEXPORT jstring JNICALL
Java_com_example_mypythonapplication_MainActivity_stringFromJNI(
JNIEnv* env,
jobject /* this */)
{
std::string command = "print('Hello World from Python!!!')";
PyObject* pInt;
Py_Initialize();
PyRun_SimpleString(command.c_str());
Py_Finalize();
std::string message = "Command ";
message.append(command);
message.append(" was executed successfully!")
return env->NewStringUTF(message.c_str());
}
Android是否有预构建的libpython
可用,或者是否有任何方法交叉编译它以实现这一点
Python仅可在主机下使用:
您可以使用Chaquopy。要查看在android上运行python的各种工具this,本文将很有帮助
Android
编译Python
或者,更简单一点,using docker:
将
/path/to/android-ndk
替换为Android NDK的路径将生成的文件夹部署到
Android
系统上的可读文件夹中,然后调用提供该路径的Py_SetPythonHome
请注意,可能需要运行:
在Java线程以外的线程上
用
21.1.6352462
、Python 3.8.6
和Python 3.9.0
测试相关问题 更多 >
编程相关推荐