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

In C,JNI use(*env)-> to get the value of the method pointer.

   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;

  memcpy((pRect+i), (RECT*)(inbuf+n), sizeof(RECT));

  analyse:NDK's compiler may be different from linux or the cygwin is not perfect.

  ARM cpu访问地址必须4字节对齐。指针从一个大的内存块中截取时必须考虑4字节对齐。

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)

原文地址:https://www.cnblogs.com/qiengo/p/2573577.html