Anybody knows how can I handle exceptions coming from native method calls?
I'm running the Barcode Reader example from Google Vision API, it works very well reading some 2d - pdf417 codes, but in some cases it crashes with a native exception attempting to use NewStringUTF
like this:
art/runtime/java_vm_ext.cc:410] JNI DETECTED ERROR IN APPLICATION: input is not valid Modified UTF-8: illegal start byte 0x90
art/runtime/java_vm_ext.cc:410] in call to NewStringUTF
art/runtime/java_vm_ext.cc:410] from com.google.android.gms.vision.barcode.internal.NativeBarcode[] com.google.android.gms.vision.barcode.internal.NativeBarcodeDetector.recognizeNative(int, int, byte[], com.google.android.gms.vision.barcode.internal.NativeOptions)
art/runtime/java_vm_ext.cc:410] "Thread-4533" prio=5 tid=19 Runnable
art/runtime/java_vm_ext.cc:410] | group="main" sCount=0 dsCount=0 obj=0x12c8e0a0 self=0x7f849f4200
art/runtime/java_vm_ext.cc:410] | sysTid=9051 nice=0 cgrp=default sched=0/0 handle=0x7f7b7bf440
art/runtime/java_vm_ext.cc:410] | state=R schedstat=( 1663092757 5060164 94 ) utm=165 stm=1 core=2 HZ=100
art/runtime/java_vm_ext.cc:410] | stack=0x7f7b6bd000-0x7f7b6bf000 stackSize=1037KB
art/runtime/java_vm_ext.cc:410] | held mutexes= "mutator lock"(shared held)
art/runtime/java_vm_ext.cc:410] native: #00 pc 00000000004897a8 /system/lib64/libart.so (_ZN3art15DumpNativeStackERNSt3__113basic_ostreamIcNS0_11char_traitsIcEEEEiP12BacktraceMapPKcPNS_9ArtMethodEPv+200)
art/runtime/java_vm_ext.cc:410] native: #01 pc 0000000000458644 /system/lib64/libart.so (_ZNK3art6Thread4DumpERNSt3__113basic_ostreamIcNS1_11char_traitsIcEEEEP12BacktraceMap+224)
art/runtime/java_vm_ext.cc:410] native: #02 pc 000000000030c9e4 /system/lib64/libart.so (_ZN3art9JavaVMExt8JniAbortEPKcS2_+1004)
art/runtime/java_vm_ext.cc:410] native: #03 pc 000000000030d29c /system/lib64/libart.so (_ZN3art9JavaVMExt9JniAbortVEPKcS2_St9__va_list+116)
art/runtime/java_vm_ext.cc:410] native: #04 pc 0000000000141f9c /system/lib64/libart.so (_ZN3art11ScopedCheck6AbortFEPKcz+144)
art/runtime/java_vm_ext.cc:410] native: #05 pc 000000000014a1d0 /system/lib64/libart.so (_ZN3art11ScopedCheck5CheckERNS_18ScopedObjectAccessEbPKcPNS_12JniValueTypeE.constprop.116+11084)
art/runtime/java_vm_ext.cc:410] native: #06 pc 0000000000153418 /system/lib64/libart.so (_ZN3art8CheckJNI12NewStringUTFEP7_JNIEnvPKc+468)
art/runtime/java_vm_ext.cc:410] native: #07 pc 0000000000005774 /data/data/com.google.android.gms/files/com.google.android.gms.vision/barcode/libs/arm64-v8a/libbarhopper.so (_ZN9barhopper9JniObject14SetStringFieldEPKcRKNSt3__112basic_stringIcNS3_11char_traitsIcEENS3_9allocatorIcEEEE+168)
art/runtime/java_vm_ext.cc:410] native: #08 pc 00000000000036f8 /data/data/com.google.android.gms/files/com.google.android.gms.vision/barcode/libs/arm64-v8a/libbarhopper.so (???)
art/runtime/java_vm_ext.cc:410] native: #09 pc 0000000000004c8c /data/data/com.google.android.gms/files/com.google.android.gms.vision/barcode/libs/arm64-v8a/libbarhopper.so (Java_com_google_android_gms_vision_barcode_internal_NativeBarcodeDetector_recognizeNative+84)
I would like to handle the crash in order to catch it and give some feedback to the user.
Unfortunately there is no way to handle exceptions from not your own native code.
I had the similar issue with google-vision. And I reported the issue to google vision github https://github.com/googlesamples/android-vision
Sadly but this library has some bugs. In my case it was the bug in the barcode text parser. It will be cool if you report your problem too.
It may be interesting that there is a way to catch exception from your own native code: Catching exceptions thrown from native code running on Android