jni数据类型的定义和jni常用函数的定义可以参考:
android-ndk-r8b/platforms/android-14/arch-arm/usr/include/jni.h,当然,mips和x86下面也是有的。
1、基本数据类型
#ifdef HAVE_INTTYPES_H
# include <inttypes.h> /* C99 */
typedef uint8_t jboolean; /* unsigned 8 bits */
typedef int8_t jbyte; /* signed 8 bits */
typedef uint16_t jchar; /* unsigned 16 bits */
typedef int16_t jshort; /* signed 16 bits */
typedef int32_t jint; /* signed 32 bits */
typedef int64_t jlong; /* signed 64 bits */
typedef float jfloat; /* 32-bit IEEE 754 */
typedef double jdouble; /* 64-bit IEEE 754 */
#else
typedef unsigned char jboolean; /* unsigned 8 bits */
typedef signed char jbyte; /* signed 8 bits */
typedef unsigned short jchar; /* unsigned 16 bits */
typedef short jshort; /* signed 16 bits */
typedef int jint; /* signed 32 bits */
typedef long long jlong; /* signed 64 bits */
typedef float jfloat; /* 32-bit IEEE 754 */
typedef double jdouble; /* 64-bit IEEE 754 */
#endif
/* "cardinal indices and sizes" */
typedef jint jsize;
2、数组对象数据类型
从定义上看不出什么
#ifdef __cplusplus
/*
* Reference types, in C++
*/
class _jobject {};
class _jclass : public _jobject {};
class _jstring : public _jobject {};
class _jarray : public _jobject {};
class _jobjectArray : public _jarray {};
class _jbooleanArray : public _jarray {};
class _jbyteArray : public _jarray {};
class _jcharArray : public _jarray {};
class _jshortArray : public _jarray {};
class _jintArray : public _jarray {};
class _jlongArray : public _jarray {};
class _jfloatArray : public _jarray {};
class _jdoubleArray : public _jarray {};
class _jthrowable : public _jobject {};
typedef _jobject* jobject;
typedef _jclass* jclass;
typedef _jstring* jstring;
typedef _jarray* jarray;
typedef _jobjectArray* jobjectArray;
typedef _jbooleanArray* jbooleanArray;
typedef _jbyteArray* jbyteArray;
typedef _jcharArray* jcharArray;
typedef _jshortArray* jshortArray;
typedef _jintArray* jintArray;
typedef _jlongArray* jlongArray;
typedef _jfloatArray* jfloatArray;
typedef _jdoubleArray* jdoubleArray;
typedef _jthrowable* jthrowable;
typedef _jobject* jweak;
#else /* not __cplusplus */
/*
* Reference types, in C.
*/
typedef void* jobject;
typedef jobject jclass;
typedef jobject jstring;
typedef jobject jarray;
typedef jarray jobjectArray;
typedef jarray jbooleanArray;
typedef jarray jbyteArray;
typedef jarray jcharArray;
typedef jarray jshortArray;
typedef jarray jintArray;
typedef jarray jlongArray;
typedef jarray jfloatArray;
typedef jarray jdoubleArray;
typedef jobject jthrowable;
typedef jobject jweak;
#endif /* not __cplusplus */
3、常用函数
3.1、数组操作函数
NewXXXArray系列函数,jbyteArray NewByteArray(jsize val0);
创建Array对象。val0是Array对象大小。
GetXXXArrayElements系列函数,jbyte *GetByteArrayElements(jbyteArray val0, jboolean *val1);
从Array对象获得数组。val0是Array对象,val1是否是拷贝(直接NULL)。
ReleaseXXXArrayElements系列函数,void ReleaseByteArrayElements(jbyteArray val0, jbyte *val1, jint val2);
说是释放,不知道释放什么了。val0是Array对象,val1是数据,val2如何释放(不知道)。
GetXXXArrayRegion系列函数,void GetByteArrayRegion(jbyteArray val0, jsize val1, jsize val2, jbyte *val3);
SetXXXArrayRegion系列函数,void SetByteArrayRegion(jbyteArray val0, jsize val1, jsize val2, jbyte *val3);
是读取和设置部分Array对象值。
Java_comxxx_xxx(jbyteArray arr){
jbyte *pj, *p;
boolean isCopy = JNI_TRUE;
pj = (*env)->GetByteArrayElements(env, arr, NULL);
LOGI("val0 = %c and val1 = %c and val2 = %c", pj[0], pj[1], pj[2]);
pj[0]='+'; pj[1]='-'; pj[2]='*';
p = (*env)->GetByteArrayElements(env, arr, NULL);
LOGI("val0 = %c and val1 = %c and val2 = %c", p[0], p[1], p[2]);
(*env)->ReleaseByteArrayElements(env, arr, pj, JNI_ABORT);
pj[0]='1'; pj[1]='2'; pj[2]='3';
p = (*env)->GetByteArrayElements(env, arr, NULL);
LOGI("val0 = %c and val1 = %c and val2 = %c", p[0], p[1], p[2]);
}
打印结果如下:
val0 = A and val1 = B and val2 = C
val0 = + and val1 = - and val2 = *
val0 = 1 and val1 = 2 and val2 = 3
可以看得出GetByteArrayElements后,pj的改变就会影响arr的值,本以为ReleaseByteArrayElements后就不会影响了,结果还是影响,不知道是不是我使用方法不对。
3.2 String操作
char *GetStringUTFChars(jstring string, jboolean *isCopy);