Reputation: 45
I got this "java.lang.UnsatisfiedLinkError: Native method not found:" when I was trying to access jni natvie methods
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)
Then I found out that if I remove the .h file and only keep the .cpp native file. The error will not occur.
This is my header file
/* 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
And this is my cpp file
#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
My Android.mk file is as follow:
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)
I found out that the error will not occur if I do remove the header file and only keeps the cpp file. But I have no idea the reason for it.
Upvotes: 0
Views: 2531
Reputation: 19282
I suggest you remove the
#ifndef _Included_com_example_nativeegl_MyRenderer
#define _Included_com_example_nativeegl_MyRenderer
and
#endif
from the cpp file.
They get defined when you include the header, and therefore the definition of the actual function doesn't happen, so it won't link.
When you remove the header, they don't get defined, so the function is defined.
The include guards are for headers: be careful what you copy and paste!
Upvotes: 2