24

一个 jni 相关问题,希望大佬可以解惑

 2 years ago
source link: https://www.v2ex.com/t/787492
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

V2EX  ›  程序员

一个 jni 相关问题,希望大佬可以解惑

  userGyl · 2 小时 27 分钟前 · 135 次点击

流程是在 cpp 中调用 jni.cpp 再调用 java,报错
JNI DETECTED ERROR IN APPLICATION: JNI GetMethodID called with pending exception java.lang.ClassNotFoundException: Didn't find class on path: DexPathList[[directory "."],nativeLibraryDirectories=[/system/lib64, /system_ext/lib64, /system/lib64, /system_ext/lib64]]

下面贴一下调用代码
//通过 jvm 获取 env 的方式
JNIEnv *get_env(int *attach) {
if (javaVM == NULL) return NULL;
*attach = 0;
JNIEnv *jni_env = NULL;
int status = javaVM->GetEnv((void **) &jni_env, JNI_VERSION_1_6);
if (status == JNI_EDETACHED || jni_env == NULL) {
JavaVMAttachArgs vmAttachArgs;
vmAttachArgs.version = JNI_VERSION_1_6;
vmAttachArgs.name = NULL;
vmAttachArgs.group = NULL;

status = javaVM->AttachCurrentThread(&jni_env, &vmAttachArgs);
if (status < 0) {
jni_env = NULL;
} else {
*attach = 1;
}
}
return jni_env;
}
//对应 java 文件声明的 native 函数
extern "C"
JNIEXPORT void JNICALL
.._XJni_Heart(JNIEnv *ev, jobject t) {

int attach = 0;
JNIEnv *env = get_env(&attach);

const char *javaPath="包名 /XJni";
jclass clazz = env->FindClass(javaPath); //debug 显示返回为 null
jmethodID mid = env->GetMethodID(clazz, "CallJavaHeart", "()V");
jobject jobject = env->AllocObject(clazz);
env->CallVoidMethod(jobject, mid);

if (attach == 1) {
del_env();
}


}


网上的一些解决方案试过了,但是无效,希望有大佬可以帮忙看看,非常感激


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK