android下JNI中C调用第三方SO问题
需求描述:
现有一个第三方so文件(比如:libXXX.so),有详细的接口文档,可以在android下直接调用,但现需要用C再封装一层,自己写C调用第三方的libXXX.so,再编译成自已的so文件,android中再调用自己的so文件。
问题:
1.C下如何调用libXXX.so?
2.Android.mk文件如何编写?
请贴详细代码,多谢!
有详细代码吗?最好 Android.mk跟C调用的都有。
用C调用第三方so
void *handle;
int (*call_method)(void);
handle = dlopen(so_path, RTLD_LAZY);
//函数指针
call_method = dlsym(handle, "method_name(调用方法的函数名)");
相对应的c代码:
#include "string.h"
#include "jni.h"
#include "dlfcn.h"
#include <fcntl.h>
void * filehandle = NULL;
jint (*getResult)(JNIEnv *, jobject, jint, jint) =NULL;
jint JNICALL Java_com_example_hellojni_HelloAct_getInt(JNIEnv * env,
jobject thiz) {
jint result = 0;
filehandle = dlopen("libhello-jni.so", RTLD_LAZY);
if (filehandle) {
getResult = (jint (*)(JNIEnv *, jobject, jint, jint)) dlsym(filehandle,
"Java_com_example_hellojni_HelloJni_add");
if (getResult)
result = getResult(env, thiz, 3, 10);
dlclose(filehandle);
filehandle = NULL;
}
return result + 1;
}