【发布时间】:2014-12-01 06:44:00
【问题描述】:
我正在制作一个库应用程序,它使用 google breakpad 检测 android 中的本机崩溃。每当我的主应用程序发生本机崩溃时,breakpad 都会调用以下回调。 从这个回调中,我需要使用 JNI 调用 java 类中的静态 void 方法。
bool breakpad_callback(const google_breakpad::MinidumpDescriptor& descriptor, void* context, bool succeeded) {
JNIEnv* env = NULL;
if (vm->GetEnv(reinterpret_cast<void**>(&env), JNI_VERSION_1_6) != JNI_OK)
{
if(isDebug)
LOGI("Failed to get the environment");
return false;
}
if(isDebug)
LOGI("attaching thread ...");
vm->AttachCurrentThread(&env, NULL);
if(isDebug)
LOGI("handle exception");
ExceptionHandlerClass = (jclass)env->NewGlobalRef(env->FindClass("com/abc/Myclass"));
if (ExceptionHandlerClass == NULL)
LOGE("Could not find java class");
ExceptionHandlerMethod = env->GetStaticMethodID(ExceptionHandlerClass, "handleException", "()V");
if (ExceptionHandlerMethod == NULL)
LOGE("Could not bind exception handler method");
// handle exception
env->CallStaticVoidMethod(ExceptionHandlerClass, ExceptionHandlerMethod);
if(env->ExceptionCheck()) {
LOGI("got exception");
env->ExceptionDescribe();
}
if(isDebug)
LOGI("exception handled");
}
这是我的java方法:
package com.abc;
public class Myclass {
public static void handleException() {
System.out.println("inside handle exception");
}
}
这在 Android 5.0 之前可以正常工作。但是在 Lollipop 中,我无法调用我的 java 方法,因为我无法在 Logcat 控制台上看到“内部句柄异常”日志。
这是我在 logcat 上看到的日志消息:
12-01 13:57:46.617: I/AACRNative(1617): attaching thread ...
12-01 13:57:46.617: I/AACRNative(1617): handle exception
12-01 13:57:46.619: I/AACRNative(1617): got exception
12-01 13:57:46.620: W/art(1617): JNI WARNING: java.lang.StackOverflowError thrown while calling printStackTrace
12-01 13:57:46.620: I/AACRNative(1617): exception handled
有人可以帮我解决这个问题吗?
【问题讨论】:
-
你看到任何输出了吗? logcat 中是否显示任何 LOGE 行?此外,如果抛出任何 java 异常,您还应该检查您的代码。我的猜测是
ClassNotFoundException。在棒棒糖中,java VM 从dalvik更改为ART。ART在类名方面更严格。确保你的正确。 -
您好,我在代码中添加了一些日志语句。你是对的,因为我在调用 java 方法后遇到异常,但我无法使用 ExceptionDescribe(); 打印异常;还验证了我的班级名称。这似乎是正确的。
-
本机崩溃处理代码不一定会找到处于可调用状态的虚拟机。
标签: android c++ java-native-interface android-5.0-lollipop