【发布时间】:2022-10-12 23:51:40
【问题描述】:
我正在尝试为this 问题建立一个解决方法。方法是将所有关于Python的代码移动到一个单独的so文件中,并制作一个loader来加载和运行它。在我看来,dlopen 应该将所有符号加载到默认命名空间中,然后我可以绕过命名空间问题。
这是 JNI 中的加载器:
extern "C" JNIEXPORT void JNICALL
Java_com_example_app_NativeLoader_load(
JNIEnv* env,
jobject /* this */,
jstring jLibraryPath) {
const char *libraryPath = env->GetStringUTFChars(jLibraryPath, NULL);
void *handle = dlopen(libraryPath, RTLD_NOW|RTLD_GLOBAL);
if (handle == nullptr) {
LOGD("load %s failed: %s", libraryPath, dlerror());
goto exit;
}
// The lib is loaded successfully
// Then I should load the entry function, and run it.
// But here is another test:
void *sym = dlsym(handle, "PyExc_SystemError"); ///< this works
void *handle_2 = dlopen("/some/path/to/_csv.cpython-310.so", RTLD_NOW);
if (handle_2 == nullptr) {
// !!! It goes here, but it shouldn't.
LOGD("load _csv.cpython-310.so failed: %s", dlerror());
}
exit:
env->ReleaseStringUTFChars(jLibraryPath, libraryPath);
}
它确认PyExc_SystemError 在主库中,但是当我dlopen _csv.cpython-310.so 时,它说:
03:57:17.744 3102 3102 D example: load /data/app/com.example.app-v4JTCukKIPJdXmBSnDMO6A==/base.apk!/lib/x86_64/_csv.cpython-310.so failed: dlopen failed: cannot locate symbol "PyExc_SystemError" referenced by "/data/app/com.example.app-v4JTCukKIPJdXmBSnDMO6A==/base.apk!/lib/x86_64/_csv.cpython-310.so"
运行环境为Android 9.0/10.0/11.0,NDK r25。为什么符号不可用?
【问题讨论】:
-
哪个
LOGD产生了引用的错误消息?我认为这是第一个,因为第二个有不同的字符串文字。 -
不,是第二个。第一次
dlopen加载成功。
标签: android android-ndk dlopen