如果有一个应用程序有一个 .so 库。比如说“example.so”,它有一个类似 Java_com_domain_demo_exampleFuntion
的函数,你能从你的应用程序中调用它吗?它有不同的应用程序 ID?
如果新的应用程序 ID 是 com.otherdomain.demo2
那么就会出现类似这样的错误
No implementation found for void Java_com_otherdomain_demo2_exampleFuntion()
最佳答案
如果您不能重命名您的应用程序,还有另一种实现此目的的方法:编写一个小的 .so
文件,该文件链接到 example.so
并调用 JNI 的 在其
:JNI_OnLoad
函数中注册 Natives
以下示例改编自 Android JNI documentation :
JNIEXPORT jint JNI_OnLoad(JavaVM* vm, void* reserved) {
JNIEnv* env;
if (vm->GetEnv(reinterpret_cast<void**>(&env), JNI_VERSION_1_6) != JNI_OK) {
return JNI_ERR;
}
// Find your class. JNI_OnLoad is called from the correct class loader context for this to work.
jclass c = env->FindClass("com/markov/App/MyClass");
if (c == nullptr) return JNI_ERR;
// Register your class' native methods.
static const JNINativeMethod methods[] = {
{"myExampleFunction", "()V", reinterpret_cast(Java_com_domain_demo_exampleFuntion)},
};
int rc = env->RegisterNatives(c, methods, sizeof(methods)/sizeof(JNINativeMethod));
if (rc != JNI_OK) return rc;
return JNI_VERSION_1_6;
}
此示例将 Java_com_domain_demo_exampleFunction
绑定(bind)到 com.markov.App.MyClass#myExampleFunction
。
当然,您可以向 methods
数组添加更多函数。
请注意,您复制的函数的行为可能取决于您绑定(bind)它的类的某些字段。如果这些字段不在您的 com.markov.App.MyClass
类中,则 JNI 调用将失败或崩溃。例如,许多 Java 包装器使用 long
字段来存储指向 C 内存的指针。
关于android - 您可以在其他应用程序中使用一个应用程序中的 .so 库吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58894268/