[转]Problems of Android NDK(新手容易犯的错误)
For the detail of JNI functions:
http://www.public.iastate.edu/~java/docs/guide/nativemethod/functions.doc.html#17314
Note:
1.The Definition about JNIEnv is different in C and C++.
#if defined(__cplusplus)
typedef _JNIEnv JNIEnv;
typedef _JavaVM JavaVM;
#else
typedef const struct JNINativeInterface* JNIEnv;
typedef const struct JNIInvokeInterface* JavaVM;
#endif
jsize len = (*env)->GetArrayLength(env,array);
In C++,JNIEvn has the inner member function which can deal with the pointer.
jsize len =env->GetArrayLength(array);
So you may get the following error if you apply the C's usage to C++:
$ make APP=hello-jni-C++ Android NDK: Building for application 'hello-jni-C++' Compile++ thumb: hello-jni-C++ <= sources/samples/hello-jni-C++/hello-jni.cpp sources/samples/hello-jni-C++/hello-jni.cpp: In function '_jstring* Java_com_example_hellojni_HelloJni_stringFromJNI(JNIEnv*, _jobject*)': sources/samples/hello-jni-C++/hello-jni.cpp:29: error: base operand of '->' has non-pointer type '_JNIEnv' make: *** [out/apps/hello-jni-C++/android-1.5-arm/objs/hello-jni-C++/hello-jni.o] Error 1 |
JNI can only use C complier,so you have to add extern "C" when you use C++. or you'll get java.lang.UnsatisfiedLinkError
JNIEnv * env--> JNI interface pointer
JObject thiz-->this pointer
Problems:
1.Fatal signal 7 (SIGBUS) at 0x00000000 (code=128)
origin:
*(pRect+i) = *((RECT*)(inbuf+n));
solution:
RECT temp = *((RECT*)(inbuf+n));
*(pRect+i) = temp;
analyse:NDK's compiler may be different from linux or the cygwin is not perfect.
2.Fatal signal 11 (SIGBUS)
Someone says "This is segmentation fault, means that the program accessed a memory location that was not assigned. ". Maybe.
3.java.lang.UnsatisfiedLinkError
Make sure that:
a. you have added extern "C" to the C++ part
b. you jni method name is right(package name_class name_ method name)
原文:http://www.cnblogs.com/qiengo/archive/2012/07/27/2573577.html