我正在尝试在Android中使用JNI,但仍然会收到'UnsatisfiedLinkError - 找不到本机方法'。我花了几个小时寻找解决方案,但仍然遇到麻烦。谢谢你的帮助。
我的代码如下:
Java类:
package com.example.icam;
public class Native {
static{
System.loadLibrary("nativeLib");
}
public static native int nativeFunction();
}
头文件(使用javah生成):
/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* Header for class com_example_icam_Native */
#ifndef _Included_com_example_icam_Native
#define _Included_com_example_icam_Native
#ifdef __cplusplus
extern "C" {
#endif
/*
* Class: com_example_icam_Native
* Method: nativeFunction
* Signature: ()I
*/
JNIEXPORT jint JNICALL Java_com_example_icam_Native_nativeFunction
(JNIEnv *, jclass);
#ifdef __cplusplus
}
#endif
#endif
C ++源文件:
#include "com_example_icam_Native.h"
JNIEXPORT jint JNICALL Java_com_example_icam_Native_nativeFunction(JNIEnv * env, jclass clazz){
return (jint)1;
}
我的Android.mk
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
OPENCV_CAMERA_MODULES:=on
OPENCV_INSTALL_MODULES:=on
#OPENCV_LIB_TYPE:=SHARED
APP_ABI := armeabi-v7a
LOCAL_SRC_FILES := native.cpp
include C:\OpenCV-2.4.8-android-sdk\sdk\native\jni\OpenCV.mk
LOCAL_LDLIBS += -llog -ldl
LOCAL_MODULE := nativeLib
include $(BUILD_SHARED_LIBRARY)
我对JNI很新,也许我错过了什么?
错误讯息:
02-14 03:44:37.501: E/AndroidRuntime(23484): java.lang.UnsatisfiedLinkError: Native method not found: com.example.icam.Native.nativeFunction:()I
02-14 03:44:37.501: E/AndroidRuntime(23484): at com.example.icam.Native.nativeFunction(Native Method)
02-14 03:44:37.501: E/AndroidRuntime(23484): at com.example.icam.MainActivity.onCreate(MainActivity.java:81)
02-14 03:44:37.501: E/AndroidRuntime(23484): at android.app.Activity.performCreate(Activity.java:5231)
02-14 03:44:37.501: E/AndroidRuntime(23484): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
02-14 03:44:37.501: E/AndroidRuntime(23484): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
02-14 03:44:37.501: E/AndroidRuntime(23484): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
02-14 03:44:37.501: E/AndroidRuntime(23484): at android.app.ActivityThread.access$800(ActivityThread.java:135)
02-14 03:44:37.501: E/AndroidRuntime(23484): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
02-14 03:44:37.501: E/AndroidRuntime(23484): at android.os.Handler.dispatchMessage(Handler.java:102)
02-14 03:44:37.501: E/AndroidRuntime(23484): at android.os.Looper.loop(Looper.java:136)
02-14 03:44:37.501: E/AndroidRuntime(23484): at android.app.ActivityThread.main(ActivityThread.java:5017)
02-14 03:44:37.501: E/AndroidRuntime(23484): at java.lang.reflect.Method.invokeNative(Native Method)
02-14 03:44:37.501: E/AndroidRuntime(23484): at java.lang.reflect.Method.invoke(Method.java:515)
02-14 03:44:37.501: E/AndroidRuntime(23484): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
02-14 03:44:37.501: E/AndroidRuntime(23484): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
02-14 03:44:37.501: E/AndroidRuntime(23484): at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:3)
.c文件中声明的函数不是.h文件中声明的函数,也不是Java代码中的本机方法。你已经在行的某个地方更改了名称而没有保持所有内容同步。
答案 1 :(得分:0)
我有同样的问题,我通过更改
修复了它JNIEXPORT jint JNICALL Java_com_example_icam_Native_nativeFunction (JNIEnv *, jclass );
到
JNIEXPORT jint JNICALL Java_com_example_icam_Native_nativeFunction (JNIEnv *, jobject );
答案 2 :(得分:-1)
您必须再次 ndk_build
构建 .so 文件