NDK Note

 For the detail of JNI functions:

http://www.public.iastate.edu/~java/docs/guide/nativemethod/functions.doc.html#17314

 


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);

jintArray array = (*env)-> NewIntArray(env, 10);  
for(; i<= 10; i++){  
    (*env)->SetIntArrayRegion(env, array, i-1, 1, &i);   
} 

  In C++,JNIEvn has the inner member function which can deal with the pointer.

    jsize len =env->GetArrayLength(array);

jintArray array= env-> NewIntArray(10);  
for(; i<= 10; i++){  
    env->SetIntArrayRegion(array, i-1, 1, &i);   
}

  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

2.Data Type
Java C/C++ byte
boolean jboolean 1
byte jbyte 1
char jchar 2
short jshort 2
int jint 4
long jlong 8
float jfloat 4
double jdouble 8

 

Java C/C++
boolean[ ] JbooleanArray
byte[ ] JbyteArray
char[ ] JcharArray
short[ ] JshortArray
int[ ] JintArray
long[ ] JlongArray
float[ ] JfloatArray
double[ ] JdoubleArray

  You can use xxx * GetXxxArrayElements(xxxArray array, jboolean *isCopy)  to get a C pointer which point to a java array.And transfer the pointer to ReleaseXxxArrayElemes when needn’t it.

3.Operations About Java

jclass FindClass(JNIEnv *env, const char *name); 

  Pay attention to the second param’s type const char *,you may need to convert to const char* of utf8 with the help of GetStringUTFChars if you transfer the java’s unicode jstring to this function.Remember to use ReleaseStringUTFChars after get the class successfully.

  Java's character encoding is unicode,but we need utf8 in jni.so you should always use GetStringUTFChars when you get a jstring from java.Then ReleaseStringUTFChars.

jclass GetSuperclass(JNIEnv *env, jclass clazz); 

  get parent or super class.

jboolean IsAssignableFrom(JNIEnv *env, jclass clazz1,jclass clazz2); 

  Judge whether the class1’s object can be converted to class2’s object successfully.

jclass GetObjectClass(JNIEnv *env, jobject obj);

  get the class by object.

jboolean IsInstanceOf(JNIEnv *env, jobject obj,jclass clazz);

jmethodID GetMethodID(JNIEnv *env, jclass clazz,const char *name, const char *sig);

  get the ID of a java method.

NativeType CallXXXMethod (JNIEnv *env, jobject obj,jmethodID methodID, va_list args);

  CallObjectMethod,CallBooleanMethod,CallByteMethod,CallCharMethod,CallShortMethod

  CallIntMethod,CallLongMethod,CallFloatMethod,CallDoubleMethod,CallVoidMethod.......
  Here the java class is not static.

  va_list args:argument list

Access the Field of Java

    1. jfieldID GetFieldID(JNIEnv *env, jclass clazz,const char *name, const char *sig)

    2. NativeType GetXXXField(JNIEnv *env, jobject obj,jfieldID fieldID);

  GetObjectField,GetBooleanField,GetByteField,GetCharField,GetShortField,GetIntField,GetLongField,GetFloatField,GetDoubleField。
    3. void SetXXXField(JNIEnv *env, jobject obj, jfieldID fieldID,NativeType value); 
  SetObjectField,SetBooleanField,SetByteField,SetCharField,SetShortField,SetIntField,SetLongField,SetFloatField,SetDoubleField

    4.jfieldID GetStaticFieldID(JNIEnv *env, jclass clazz,const char *name, const char *sig);

    5. NativeType GetStaticXXXField(JNIEnv *env, jclass clazz,jfieldID fieldID);
    6. void SetStaticXXXField(JNIEnv *env, jclass clazz,jfieldID fieldID, NativeType value);

posted @ 2012-07-28 10:35  Qiengo  阅读(432)  评论(0编辑  收藏  举报