Java 调用env->;JNI中的ThrowNew()
我已经调试这个错误3天了。在我的Android项目中,我试图从本机代码抛出一个异常。我创建了自己的异常类,它扩展了标准Java异常类。下面是我从本机代码引发异常的方法Java 调用env->;JNI中的ThrowNew(),java,android,android-ndk,java-native-interface,android-gradle-plugin,Java,Android,Android Ndk,Java Native Interface,Android Gradle Plugin,我已经调试这个错误3天了。在我的Android项目中,我试图从本机代码抛出一个异常。我创建了自己的异常类,它扩展了标准Java异常类。下面是我从本机代码引发异常的方法 extern "C" JNIEXPORT void JNICALL Java_com_company_MyClass_nativeCall(JNIEnv *env, jclass type) { // Other code stuffs jclass clazz = env->FindClass("com/c
extern "C" JNIEXPORT void JNICALL Java_com_company_MyClass_nativeCall(JNIEnv *env, jclass type)
{
// Other code stuffs
jclass clazz = env->FindClass("com/company/MyExceptionClass");
env->ThrowNew(clazz, "Error");
}
但是,当ThrowNew()被执行时,它会崩溃
F/libc (19699): Fatal signal 4 (SIGILL) at 0x7191ed06 (code=1), thread 19699 (mythread.myclass)
I/DEBUG ( 6189): signal 4 (SIGILL), code 1 (ILL_ILLOPC), fault addr 7191ed06
I/DEBUG ( 6189): r0 00000000 r1 41641d00 r2 4017b394 r3 61ff270f
I/DEBUG ( 6189): r4 61ff270f r5 41579870 r6 00000003 r7 bec33238
I/DEBUG ( 6189): r8 bec33360 r9 6d4fdbf0 sl 41579880 fp bec33374
I/DEBUG ( 6189): ip 00000000 sp bec331f0 lr 415c02e3 pc 7191ed06 cpsr 600e0030
I/DEBUG ( 6189): d0 0000000000000000 d1 0000000000000000
I/DEBUG ( 6189): d2 0000000000000000 d3 0000000000000000
I/DEBUG ( 6189): d4 6d582ab06d582a78 d5 6d582b206d582ae8
I/DEBUG ( 6189): d6 6d582b906d582b58 d7 3f8000003f800000
I/DEBUG ( 6189): d8 0000000000000000 d9 0000000000000000
I/DEBUG ( 6189): d10 0000000000000000 d11 0000000000000000
I/DEBUG ( 6189): d12 0000000000000000 d13 0000000000000000
I/DEBUG ( 6189): d14 0000000000000000 d15 0000000000000000
I/DEBUG ( 6189): d16 6d5396b8416499f8 d17 6d5396b84164a1e8
I/DEBUG ( 6189): d18 6d5041986d539560 d19 6d5396086d5041d0
I/DEBUG ( 6189): d20 6d5042786d504240 d21 6d5394486d5042b0
I/DEBUG ( 6189): d22 6d5395286d539480 d23 6d5395d06d539598
I/DEBUG ( 6189): d24 0000000000000000 d25 0000000000000000
I/DEBUG ( 6189): d26 0000000000000000 d27 0000000000000000
I/DEBUG ( 6189): d28 0000000000000000 d29 0000000000000000
I/DEBUG ( 6189): d30 0000000000000000 d31 0000000000000000
I/DEBUG ( 6189): scr 60000010
我还尝试了使用env->Throw()和throwable对象。但行为是一样的。我认为这可能是平板电脑设备(Sony Z2)的动态libc支持问题,所以我使用静态libc++编译以下gradle文件。但没有任何效果。我还是会撞车
apply plugin: 'com.android.library'
android {
compileSdkVersion 23
buildToolsVersion "23.0.1"
defaultConfig {
minSdkVersion 19
targetSdkVersion 23
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
externalNativeBuild {
cmake {
arguments "-DANDROID_PLATFORM=android-23", "-DANDROID_NATIVE_API_LEVEL=android-23", "-DANDROID_PIE=ON", "-DANDROID_STL=c++_static"
cppFlags "-DANDROID_NATIVE_API_LEVEL=android-23", "-DANDROID_PLATFORM=android-23", "-DANDROID_STL=c++_static"
cFlags "-DANDROID_NATIVE_API_LEVEL=android-23", "-DANDROID_PLATFORM=android-23"
}
}
ndk {
abiFilters 'armeabi-v7a', 'arm64-v8a'
}
}
publishNonDefault true
productFlavors {
internal {
versionName "0.0.1-internal"
}
consumer {
versionName "0.0.1-public"
}
}
buildTypes {
release {
minifyEnabled true
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
debug {
debuggable true
jniDebuggable true
}
}
externalNativeBuild {
cmake {
path "CMakeLists.txt"
}
}
}
dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
exclude group: 'com.android.support', module: 'support-annotations'
})
compile 'com.android.support:appcompat-v7:23.0.1'
testCompile 'junit:junit:4.12'
}
我将非常感谢您的帮助。您能用一个只有一个JNI函数(调用
env->ThrowNew()
)的小测试应用程序重现它吗?如果没有-很可能是应用程序中的其他内容导致了此崩溃。也许你会在某个地方或其他什么地方出现内存损坏。谢谢。。我会试试的。我发现了问题。那是因为非常愚蠢的原因。首先,我在类上设置了作用域ref,所以不知何故它释放了引用和jclass内存。不知道为什么ThrowNew没有保留自己的裁判。另一个原因是我没有在投球后立即返回。