现在的位置: 首页 > 综合 > 正文

[Android NDK] Android NDK可能遇到的问题

2013年11月15日 ⁄ 综合 ⁄ 共 1143字 ⁄ 字号 评论关闭

1. java.lang.UnsatisfiedLinkError:
Native method not found的处理

这个异常一般是由于JNI的链接器不能正常识别C++的函数名造成的。处理的方法是用exern "C" {},来包裹需要export的C++的native方法。

如果native的方法比较多,可以在头文件中定义宏:

#ifdef
__cplusplus
extern "C" {
#endif
 
JNIEXPORT
jint JNICALL Java_com_sample_native_method1
  (JNIEnv
*, jobject, jobject);
 
JNIEXPORT
jint JNICALL Java_com_sample_native_method2
  (JNIEnv
*, jobject);
 
...
 
#ifdef
__cplusplus
}
#endif

然后在CPP文件中include头文件。

2. error: base operand of '->' has non-pointer type '_JNIEnv'

在使用JNI的时候,我是用C实现的,但是我实现的方法写成了C++的格式,比如:

JNIEXPORT jstring JNICALL Java_com_example_nativeegl_MyRenderer_nativeGetHelloString
  (JNIEnv *env, jobject obj) {
      return env->NewStringUTF((char*)" This is calling from JNI, suckers!");
  }

在jni.h头文件中,

if defined(__cplusplus)
typedef _JNIEnv JNIEnv; //c++中JNIEnv是结构
typedef _JavaVM JavaVM;
else
typedef const struct JNINativeInterface* JNIEnv; //其他中JNIEnv是指向结构的指针
typedef const struct JNIInvokeInterface* JavaVM; 
endif

所以在使用时会有区别:

JNIEXPORT jstring JNICALL Java_com_example_nativeegl_MyRenderer_nativeGetHelloString
  (JNIEnv *env, jobject obj) {
      return env->NewStringUTF((char*)" This is calling from JNI suckers!"); //env本身是指针,c++要这样写
      reutnr (*env)->NewStringUTF(env, "This is calling from JNI suckers!"); //c中要这样写,因为env是指针的指针。
  }

抱歉!评论已关闭.