gpt4 book ai didi

java - "java.lang.UnsatisfiedLinkError: Native method not found:"去掉头文件可以修复错误吗?

转载 作者:行者123 更新时间:2023-11-28 03:04:14 24 4
gpt4 key购买 nike

当我尝试访问 jni natvie 方法时,出现“java.lang.UnsatisfiedLinkError: Native method not found:”

11-20 10:52:29.246 E/AndroidRuntime( 2742):     at com.example.nativeegl.MyRenderer.nativeGetHelloString(Native Method)
11-20 10:52:29.246 E/AndroidRuntime( 2742): at com.example.nativeegl.MyRenderer.onDrawFrame(MyRenderer.java:36)
11-20 10:52:29.246 E/AndroidRuntime( 2742): at android.opengl.GLSurfaceView$GLThread.guardedRun(GLSurfaceView.java:1516)
11-20 10:52:29.246 E/AndroidRuntime( 2742): at android.opengl.GLSurfaceView$GLThread.run(GLSurfaceView.java:1240)

然后我发现,如果我删除 .h 文件而只保留 .cpp native 文件。不会发生错误。

这是我的头文件

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

#ifndef _Included_com_example_nativeegl_MyRenderer
#define _Included_com_example_nativeegl_MyRenderer
extern "C" {

/*
* Class: com_example_nativeegl_MyRenderer
* Method: nativeGetHelloString
* Signature: ()Ljava/lang/String;
*/
JNIEXPORT jstring JNICALL Java_com_example_nativeegl_MyRenderer_nativeGetHelloString
(JNIEnv *, jobject);
}
#endif

这是我的cpp文件

#include "com_example_nativeegl_MyRenderer.h"
#ifndef _Included_com_example_nativeegl_MyRenderer
#define _Included_com_example_nativeegl_MyRenderer
extern "C" {
/*
* Class: com_example_nativeegl_MyRenderer
* Method: nativeGetHelloString
* Signature: ()Ljava/lang/String;
*/
JNIEXPORT jstring JNICALL Java_com_example_nativeegl_MyRenderer_nativeGetHelloString
(JNIEnv *env, jobject obj) {
return env->NewStringUTF((char*)" This is calling from JNI suckers!");
}
}
#endif

我的Android.mk文件如下:

LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_CFLAGS := -Wall
LOCAL_MODULE := myegl_jni
LOCAL_C_INCLUDES := $(LOCAL_PATH)/include
LOCAL_CPP_EXTENSION := .cpp
LOCAL_SRC_FILES := com_example_nativeegl_MyRenderer.cpp

include $(BUILD_SHARED_LIBRARY)

我发现如果我删除头文件并只保留cpp文件,就不会发生错误。但我不知道它的原因。

最佳答案

我建议你删除

#ifndef _Included_com_example_nativeegl_MyRenderer
#define _Included_com_example_nativeegl_MyRenderer

#endif

来自 cpp 文件。当您包含 header 时,它们会被定义,因此不会发生实际函数的定义,因此它不会链接。当您删除 header 时,它们不会被定义,因此函数已被定义。
include guards 用于标题:小心你复制和粘贴的内容!

关于java - "java.lang.UnsatisfiedLinkError: Native method not found:"去掉头文件可以修复错误吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20094392/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com