Java JNI UnsatisfiedLinkError - 未找到本机方法

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21769655/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 10:33:50  来源:igfitidea点击:

JNI UnsatisfiedLinkError - Native method not found

javaandroidandroid-ndkjava-native-interface

提问by user3019612

I'm trying to use JNI in Android but keep getting 'UnsatisfiedLinkError - Native method not found'. I've spent some hours searching for solutions, but still having trouble. Thanks for any help.

我正在尝试在 Android 中使用 JNI,但不断收到“UnsatisfiedLinkError - 未找到本机方法”的消息。我花了几个小时寻找解决方案,但仍然遇到问题。谢谢你的帮助。

My code is as follows:

我的代码如下:

Java class:

Java类:

package com.example.icam;

public class Native {
    static{
        System.loadLibrary("nativeLib");
    }
    public static native int nativeFunction();
}

Header file (generated using javah):

头文件(使用 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++ source file:

C++源文件:

#include "com_example_icam_Native.h"

JNIEXPORT jint JNICALL Java_com_example_icam_Native_nativeFunction(JNIEnv * env, jclass clazz){
    return (jint)1;
}

My Android.mk

我的安卓.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)

I'm very new to JNI, maybe I'm missing something?

我对 JNI 很陌生,也许我错过了什么?

Error message:

错误信息:

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.access0(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)

回答by user207421

The function declared in your .c file isn't the function declared in your .h file, or as the native method in your Java code either. You've changed the name somewhere along the line without keeping everything in sync.

.c 文件中声明的函数不是 .h 文件中声明的函数,也不是 Java 代码中的本机方法。您已经更改了沿线某处的名称,而没有使所有内容保持同步。

回答by zzq

I have the same problem, I fixed it by changing

我有同样的问题,我通过更改解决了它

JNIEXPORT jint JNICALL Java_com_example_icam_Native_nativeFunction (JNIEnv *, jclass);

to

JNIEXPORT jint JNICALL Java_com_example_icam_Native_nativeFunction (JNIEnv *, jobject);

JNIEXPORT jint JNICALL Java_com_example_icam_Native_nativeFunction (JNIEnv *, jclass);

JNIEXPORT jint JNICALL Java_com_example_icam_Native_nativeFunction (JNIEnv *, jobject);

回答by Udit Kumawat

You have to again build your .sofile by ndk_build

您必须再次通过ndk_build构建.so文件