2020年android studio ------jni常用函数总结

jni常用方法

c函数

获得java类

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

获得java变量

 jfieldID    (*GetFieldID)(JNIEnv*, jclass, const char*, const char*);

获得java变量值

 jobject     (*GetObjectField)(JNIEnv*, jobject, jfieldID);

获得java静态变量

jfieldID    (*GetStaticFieldID)(JNIEnv*, jclass, const char*,
                        const char*);

获得java静态变量的值

jobject     (*GetStaticObjectField)(JNIEnv*, jclass, jfieldID);

java普通函数调用(无返回值函数)

    void        (*CallVoidMethod)(JNIEnv*, jobject, jmethodID, ...);

java静态函数调用 (无返回值函数)

 void        (*CallStaticVoidMethod)(JNIEnv*, jclass, jmethodID, ...);

char*转jstring类型(utf编码)

jstring     (*NewStringUTF)(JNIEnv*, const char*);

char*转jstring(gbk编码)

jstring     (*NewString)(JNIEnv*, const jchar*, jsize);

获得gbk编码的jstring长度

jsize       (*GetStringLength)(JNIEnv*, jstring);

string转char*(utf编码)

jstring     (*NewStringUTF)(JNIEnv*, const char*);

string转char*(gbk编码)

const jchar* (*GetStringChars)(JNIEnv*, jstring, jboolean*);

获得jstring长度

jsize       (*GetStringUTFLength)(JNIEnv*, jstring);

获得数组的长度

jsize       (*GetArrayLength)(JNIEnv*, jarray);

c++函数

获得java类

jclass FindClass(const char* name)
{ return functions->FindClass(this, name); }

获得java变量

jfieldID GetFieldID(jclass clazz, const char* name, const char* sig)
{ return functions->GetFieldID(this, clazz, name, sig); }

获得java变量值

jobject GetObjectField(jobject obj, jfieldID fieldID)
{ return functions->GetObjectField(this, obj, fieldID); }

获得java静态变量

jfieldID GetStaticFieldID(jclass clazz, const char* name, const char* sig)
{ return functions->GetStaticFieldID(this, clazz, name, sig); }

获得java静态变量的值

jobject GetStaticObjectField(jclass clazz, jfieldID fieldID)
{ return functions->GetStaticObjectField(this, clazz, fieldID); }

java普通函数调用(无返回值函数)

void CallVoidMethod(jobject obj, jmethodID methodID, ...)

java静态函数调用 (无返回值函数)

void CallStaticVoidMethod(jclass clazz, jmethodID methodID, ...)

char*转jstring类型(utf编码)

jstring NewStringUTF(const char* bytes)
{ return functions->NewStringUTF(this, bytes); }

char*转jstring(gbk编码)

jstring NewString(const jchar* unicodeChars, jsize len)
{ return functions->NewString(this, unicodeChars, len); }

获得gbk编码的jstring长度

jsize GetStringLength(jstring string)
    { return functions->GetStringLength(this, string); }

string转char*(utf编码)

const char* GetStringUTFChars(jstring string, jboolean* isCopy)
{ return functions->GetStringUTFChars(this, string, isCopy); }

string转char*(gbk编码)

const jchar* GetStringChars(jstring string, jboolean* isCopy)
{ return functions->GetStringChars(this, string, isCopy); }

获得jstring长度

jsize GetStringUTFLength(jstring string)
{ return functions->GetStringUTFLength(this, string); }

获得数组的长度

jsize GetArrayLength(jarray array)
{ return functions->GetArrayLength(this, array); }

jni中基本类型的定义

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 */

/* "cardinal indices and sizes" */
typedef jint     jsize;

jni中的枚举

typedef union jvalue {
    jboolean    z;
    jbyte       b;
    jchar       c;
    jshort      s;
    jint        i;
    jlong       j;
    jfloat      f;
    jdouble     d;
    jobject     l;
} jvalue;
jstring Ljava/lang/String;

c++中的宏定义

#define JNI_FALSE   0
#define JNI_TRUE    1

#define JNI_VERSION_1_1 0x00010001
#define JNI_VERSION_1_2 0x00010002
#define JNI_VERSION_1_4 0x00010004
#define JNI_VERSION_1_6 0x00010006

#define JNI_OK          (0)         /* no error */
#define JNI_ERR         (-1)        /* generic error */
#define JNI_EDETACHED   (-2)        /* thread detached from the VM */
#define JNI_EVERSION    (-3)        /* JNI version error */
#define JNI_ENOMEM      (-4)        /* Out of memory */
#define JNI_EEXIST      (-5)        /* VM already created */
#define JNI_EINVAL      (-6)        /* Invalid argument */

#define JNI_COMMIT      1           /* copy content, do not free buffer */
#define JNI_ABORT       2           /* free buffer w/o copying back */

你可能感兴趣的:(c\c++)