不满意的链接错误:Android JNI

时间:2014-10-06 10:42:05

标签: java android c++ java-native-interface unsatisfiedlinkerror

我正在开发一个使用JNI的Android项目。经过2天的调试,我仍然收到一个错误:一个不满意的链接错误:

Java.lang.UnsatisfiedLinkError:找不到本机方法:org.opencv.samples.facedetect.Hello.sayHello :()V

以前有很多人遇到过这个问题,所以我已经阅读并尝试了许多可能的解决方案,但我仍然无法让它工作:(

我的代码:

Hello.java =

public class Hello { 

    public static native void sayHello(); 

    static {
        Log.i("JNI", "Loading hello");
        System.loadLibrary("hello");
    }
} 

Hello_jni.cpp =

#include <jni.h>
#include <Hello_jni.h>
#include <stdio.h>
#include <android/log.h>

#define LOG_TAG "FaceDetection/DetectionBasedTracker"
#define LOGD(...) ((void)__android_log_print(ANDROID_LOG_DEBUG, LOG_TAG, __VA_ARGS__))

extern "C"  JNIEXPORT void JNICALL Java_org_opencv_samples_facedetect_Hello_sayHello
 (JNIEnv *env, jclass obj)
{
 printf("Hello world!\n");
 LOGD("werkt");
 return;
}

Hello_jni.h =

/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* Header for class org_opencv_samples_facedetect_Hello */

#ifndef _Included_org_opencv_samples_facedetect_Hello
#define _Included_org_opencv_samples_facedetect_Hello
#ifdef __cplusplus
extern "C" {
#endif
/*
 * Class:     org_opencv_samples_facedetect_Hello
 * Method:    sayHello
 * Signature: ()V
 */
JNIEXPORT void JNICALL Java_org_opencv_samples_facedetect_Hello_sayHello
  (JNIEnv *, jclass);

#ifdef __cplusplus
}
#endif
#endif

Android.mk =(第一部分是来自openCV的其他模块。我使用openCV中的facedetection示例作为我的应用程序的模板)

LOCAL_PATH := $(call my-dir)

include $(CLEAR_VARS)

#OPENCV_CAMERA_MODULES:=off
#OPENCV_INSTALL_MODULES:=off
#OPENCV_LIB_TYPE:=SHARED
include ../../sdk/native/jni/OpenCV.mk

LOCAL_SRC_FILES  := DetectionBasedTracker_jni.cpp
LOCAL_C_INCLUDES += $(LOCAL_PATH)
LOCAL_LDLIBS     += -llog -ldl

LOCAL_MODULE     := detection_based_tracker
include $(BUILD_SHARED_LIBRARY)

LOCAL_PATH := $(call my-dir)

include $(CLEAR_VARS)

LOCAL_MODULE := hello
LOCAL_SRC_FILE := org_opencv_samples_facedetect_Hello_jni.cpp
LOCAL_LDLIBS     += -llog 

include $(BUILD_SHARED_LIBRARY)

从我的主要活动中,我调用Hello.sayHello()(静态)。这会触发错误。 尝试: - 在cpp文件中删除/添加'extern“C”'到sayHello()。 - 将功能从原生更改为非原生

库似乎加载正常,这是整个错误:

I/JNI(21440): Hello.sayHello() called
I/JNI(21440): Loading hello
D/dalvikvm(21440): Trying to load lib /data/data/org.opencv.samples.facedetect/lib/libhello.so 0x4248c960
D/dalvikvm(21440): Added shared lib /data/data/org.opencv.samples.facedetect/lib/libhello.so 0x4248c960
D/dalvikvm(21440): No JNI_OnLoad found in /data/data/org.opencv.samples.facedetect/lib/libhello.so 0x4248c960, skipping init
W/dalvikvm(21440): No implementation found for native Lorg/opencv/samples/facedetect/Hello;.sayHello:()V
D/AndroidRuntime(21440): Shutting down VM
W/dalvikvm(21440): threadid=1: thread exiting with uncaught exception (group=0x41a4e2a0)
E/AndroidRuntime(21440): FATAL EXCEPTION: main
E/AndroidRuntime(21440): java.lang.UnsatisfiedLinkError: Native method not found: org.opencv.samples.facedetect.Hello.sayHello:()V
E/AndroidRuntime(21440):    at     org.opencv.samples.facedetect.Hello.sayHello(Native Method)

如果有人可以帮助我,你就会成为英雄!我只是无法让它工作......

提前致谢:)

Jelmer

2 个答案:

答案 0 :(得分:1)

你的dalvik运行时会告诉你究竟出了什么问题:

W/dalvikvm(21440): No implementation found for native Lorg/opencv/samples/facedetect/Hello;.sayHello:()V D/AndroidRuntime(21440): Shutting down VM

这意味着您没有正确实现本机方法,并且Java运行时无法找到它。这样的错误只发生在运行时,lib编译好了,这给了我一个预感,即方法名称(在.h和.cpp文件中)是错误的并且不对应于jni命名格式,或者有错误在.cpp文件中。

答案 1 :(得分:1)

它现在正在运行,问题是Android.mk文件。感谢Vorren提供解决方案;) 工作make文件是:

LOCAL_PATH := $(call my-dir)

include $(CLEAR_VARS)

#OPENCV_CAMERA_MODULES:=off
#OPENCV_INSTALL_MODULES:=off
#OPENCV_LIB_TYPE:=SHARED
include ../../sdk/native/jni/OpenCV.mk

LOCAL_SRC_FILES  := DetectionBasedTracker_jni.cpp
LOCAL_C_INCLUDES += $(LOCAL_PATH)
LOCAL_LDLIBS     += -llog -ldl

LOCAL_MODULE     := detection_based_tracker

include $(BUILD_SHARED_LIBRARY)

include $(CLEAR_VARS)

LOCAL_SRC_FILES := Hello_jni.cpp
LOCAL_C_INCLUDES := $(LOCAL_PATH)
LOCAL_LDLIBS     += -llog 

LOCAL_MODULE := hello

include $(BUILD_SHARED_LIBRARY)

有两个错误;语法错误LOCAL_SRC_FILE(缺少S),我打电话给了我的dir&#39;在make文件中有2次被禁止(在此处找到:https://groups.google.com/forum/#!topic/android-ndk/Qmr_WQH-uKk