这是我的程序:
extern "C" {
JNIEXPORT jint Java_android_app_integrity_VerifyIntegrity_checkCrc(JNIEnv *jniEnv,jobject thiz,jstring crcStr) {
jclass clsZipFile = jniEnv->FindClass("java/util/zip/ZipFile");
jmethodID mtdConstruct = jniEnv->GetMethodID(clsZipFile, "<init>", "(Ljava/lang/String;)V");
jmethodID mtdGetEntry = jniEnv->GetMethodID(clsZipFile,"getEntry","(Ljava/lang/String;)Ljava/util/zip/ZipEntry;");
jclass clsZipEntry = jniEnv->FindClass("java/util/zip/ZipEntry");
jmethodID mtdGetCrc = jniEnv->GetMethodID(clsZipEntry,"getCrc","()L");
LOGD("pos2");
jobject objZipFile = jniEnv->NewObject(clsZipFile,mtdConstruct,crcStr);
if (NULL == objZipFile){
LOGD("NULL == objZipFile");
}
LOGD("pos3");
jobject objZipEntry = jniEnv->CallObjectMethod(objZipFile, mtdGetEntry,"classes.dex");
LOGD("pos4");
jlong ret = jniEnv->CallLongMethod(objZipEntry, mtdGetCrc);
LOGD("%ld",(long int)ret);
return 0;
}
};
它仅打印“pos2”。 “LOGD(” pos2“);”下面的行会导致死机!
我找不到原因。谁能帮我?谢谢!
最佳答案
尝试修复以下行。它具有无效的签名,将导致对MethodNotFound异常引发隐式异常,并且很可能是罪魁祸首。
jmethodID mtdGetCrc = jniEnv->GetMethodID(clsZipEntry,"getCrc","()L");
应该:
jmethodID mtdGetCrc = jniEnv->GetMethodID(clsZipEntry,"getCrc","()J");
但是,我还有其他建议来检查所有FindClass和FindMethod调用的返回值,因为它们不仅返回NULL,而且在失败时也会抛出异常。当JNI无法分配本地引用对象返回以进行jclass查找时,也会引发OutOfMemoryException。
关于android - 如何修复env-> NewObject()上的JNI崩溃?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27595746/