1. 程式人生 > >No implementation found for native 以及 java.lang.UnsatisfiedLinkError問題解決一例

No implementation found for native 以及 java.lang.UnsatisfiedLinkError問題解決一例

JNI 問題記錄如下:

05-09 01:36:35.029: I/dalvikvm-heap(766): Grow heap (frag case) to 8.971MB for 1228816-byte allocation
05-09 01:36:35.368: D/dalvikvm(766): GC_CONCURRENT freed 1K, 4% free 9116K/9479K, paused 6ms+5ms
05-09 01:36:37.149: W/dalvikvm(766): No implementation found for native Lorg/opencv/samples/tutorial2/JNInterface;.matchFingers (Ljava/lang/String;)Ljava/lang/String;
05-09 01:36:37.149: D/AndroidRuntime(766): Shutting down VM 05-09 01:36:37.149: W/dalvikvm(766): threadid=1: thread exiting with uncaught exception (group=0x409c01f8) 05-09 01:36:37.178: E/AndroidRuntime(766): FATAL EXCEPTION: main 05-09 01:36:37.178: E/AndroidRuntime(766): java.lang.UnsatisfiedLinkError: matchFingers
05-09 01:36:37.178: E/AndroidRuntime(766): at org.opencv.samples.tutorial2.JNInterface.matchFingers(Native Method) 05-09 01:36:37.178: E/AndroidRuntime(766): at org.opencv.samples.tutorial2.ItemDetailFragment3$buttonMatchFingerslistener.onClick(ItemDetailFragment3.java:207) 05-09 01:36:37.178: E/AndroidRuntime(766): at android.view.View.performClick(View.java:3511) 05-09 01:36:37.178: E/AndroidRuntime(766): at android.view.View$PerformClick.run(View.java:14105) 05-09 01:36:37.178: E/AndroidRuntime(766): at android.os.Handler.handleCallback(Handler.java:605) 05-09 01:36:37.178: E/AndroidRuntime(766): at android.os.Handler.dispatchMessage(Handler.java:92) 05-09 01:36:37.178: E/AndroidRuntime(766): at android.os.Looper.loop(Looper.java:137) 05-09 01:36:37.178: E/AndroidRuntime(766): at android.app.ActivityThread.main(ActivityThread.java:4424) 05-09 01:36:37.178: E/AndroidRuntime(766): at java.lang.reflect.Method.invokeNative(Native Method) 05-09 01:36:37.178: E/AndroidRuntime(766): at java.lang.reflect.Method.invoke(Method.java:511) 05-09 01:36:37.178: E/AndroidRuntime(766): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784) 05-09 01:36:37.178: E/AndroidRuntime(766): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551) 05-09 01:36:37.178: E/AndroidRuntime(766): at dalvik.system.NativeStart.main(Native Method) 05-09 01:36:37.759: I/dalvikvm(766): threadid=3: reacting to signal 3 05-09 01:36:37.819: I/dalvikvm(766): Wrote stack traces to '/data/anr/traces.txt'

問題顯然是沒有找到相應的本地函式。解決辦法也很簡單,就是Java程式碼,.h檔案和.cpp檔案中的宣告,肯定有一個不對應的。

我的這個問題是JNI回撥函式的標頭檔案由Javah生成,第二個引數是Jobject,而我在cpp中一般使用Jclass,在標頭檔案中改過來即可。

//Java_org_opencv_samples_tutorial2_JNInterface.h

//JNIEXPORT jstring JNICALL Java_org_opencv_samples_tutorial2_JNInterface_matchFingers
 // (JNIEnv *, jobject, jstring);
//改為
JNIEXPORT jstring JNICALL Java_org_opencv_samples_tutorial2_JNInterface_matchFingers
  (JNIEnv *, jclass, jstring);
//總之要跟
Java_org_opencv_samples_tutorial2_JNInterface.cpp中的實現完全一致。