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
JavaC/C++byte
booleanjboolean1
bytejbyte1
charjchar2
shortjshort2
intjint4
longjlong8
floatjfloat4
doublejdouble8

 

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

转载于:https://www.cnblogs.com/qiengo/archive/2012/07/28/2612878.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值