每個native函數(shù)坟岔,都至少有兩個參數(shù)(JNIEnv*,jclass或者jobject)
1)當native方法為靜態(tài)方法時:
jclass 代表native方法所屬類的class對象(JniTest.class)
2)當native方法為非靜態(tài)方法時:
object 代表native方法所屬的對象
1.基本數(shù)據(jù)
Java基本數(shù)據(jù)類型與JNI數(shù)據(jù)類型的映射關系
Java類型->JNI類型->C類型
boolean jboolean
byte jbyte;
char jchar;
short jshort;
int jint;
long jlong;
float jfloat;
double jdouble;
void void
//引用類型(對象)
String jstring
object jobject
//數(shù)組,基本數(shù)據(jù)類型的數(shù)組
byte[] jByteArray
//對象數(shù)組
object[](String[]) jobjectArray
2.常用JNIEnv方法
// 獲取對應的Jclass對象
jclass cls = (*env)->GetObjectClass(env, jobj);
// 獲取對應的Jclass里面的屬性id
jfieldID fid = (*env)->GetFieldID(env, cls, "stringname", "Ljava/lang/String;");
//jstring -> c字符串
//isCopy 是否復制(true代表賦值冬骚,false不復制)
char *c_str = (*env)->GetStringUTFChars(env,jstr,JNI_FALSE);
//c字符串 ->jstring
jstring new_jstr = (*env)->NewStringUTF(env, text);
//Set<Type>Field
(*env)->SetObjectField(env, jobj, fid, "object");
// 獲取對應的Jclass里面的靜態(tài)屬性id
jfieldID fid = (*env)->GetStaticFieldID(env, cls, "count", "I");
//GetStatic<Type>Field
jint count = (*env)->GetStaticIntField(env, cls, fid);
//SetStatic<Type>Field
(*env)->SetStaticIntField(env,cls,fid,count);
//jmethodID
jmethodID mid = (*env)->GetMethodID(env, cls, "methodName", "(I)I");
//Call<Type>Method
jint random = (*env)->CallIntMethod(env, jobj, mid, "argument");
//jmethodID
jmethodID mid = (*env)->GetStaticMethodID(env, cls, "getUUID", "()Ljava/lang/String;");// ()表示參數(shù)的簽名類型,此處無轨蛤,但也要寫出來
//CallStatic<Type>Method
jstring uuid = (*env)->CallStaticObjectMethod(env, cls, mid);