android NDK 实用学习(一)-获取java端类及其类变量

时间:2024-05-31 18:04:56

近期为android 端项目包装一些c++代码,故学习ndk相关知识,现总结如下:

1,java与c++类型参照图:

android NDK 实用学习(一)-获取java端类及其类变量

android NDK 实用学习(一)-获取java端类及其类变量

2,此测试中使用的java类:

 package com.dasea.test.core;
public class TestSetData {
// 主要是类ID和字段ID,方法ID的缓存
static {
OnNative();
} public native static void OnNative(); public boolean bData;
public double dData;
public int iData;
public byte cData;
public String sData;
public byte byteArr[]; public int intArr[]; public TestSetData() {
// TODO Auto-generated constructor stub
bData = true;
dData = 100.11;
iData = 333;
cData = 100;
sData = "20150204";
byteArr = new byte[10];
for (int i = 0; i < byteArr.length; i++) {
byteArr[i] = '2';
} intArr = new int[10];
for (int i = 0; i < intArr.length; i++) {
intArr[i] = i * 10;
}
}
}

3,使用类接口定义:

①java端:

  public native TestSetData testData(TestSetData obj);

②c++端:

   JNIEXPORT jobject JNICALL Java_com_dasea_test_core_JniDemo_testData(JNIEnv* env, jobject obj, jobject jobj) ;

4,完整c++端接口实现:

 JNIEXPORT jobject JNICALL Java_com_dasea_test_core_JniDemo_testData(JNIEnv* env, jobject obj, jobject jobj){
DEBUG_OUT("test set data start"); jclass jcSetDataMgr = env->FindClass("com/dasea/test/core/TestSetData");
if(NULL == jcSetDataMgr){
DEBUG_OUT("Not find class!");
return NULL;
}
DEBUG_OUT("Find class succ!"); jfieldID jfbdata = env->GetFieldID(jcSetDataMgr, "bData", "Z");
if(NULL == jfbdata){
DEBUG_OUT("jfbdata fail!");
}
jfieldID jfddata = env->GetFieldID(jcSetDataMgr, "dData", "D");
if(NULL == jfbdata){
DEBUG_OUT("jfddata fail!");
}
jfieldID jfidata = env->GetFieldID(jcSetDataMgr, "iData", "I");
if(NULL == jfbdata){
DEBUG_OUT("jfidata fail!");
}
jfieldID jfcdata = env->GetFieldID(jcSetDataMgr, "cData", "B");
if(NULL == jfbdata){
DEBUG_OUT("jfcdata fail!");
}
jfieldID jfsdata = env->GetFieldID(jcSetDataMgr, "sData", "Ljava/lang/String;");
if(NULL == jfbdata){
DEBUG_OUT("jfsdata fail!");
} DEBUG_OUT("Get end!"); env->SetBooleanField(jobj, jfbdata, false);
DEBUG_OUT("SetBooleanField end!"); env->SetDoubleField(jobj, jfddata, 209.22);
DEBUG_OUT("SetDoubleField end!"); env->SetIntField(jobj, jfidata, );
DEBUG_OUT("SetIntField end!"); env->SetByteField(jobj, jfcdata, );
DEBUG_OUT("Set field succ!"); char data[] = "";
env->SetObjectField(jobj, jfsdata, env->NewStringUTF(data)); jfieldID jiarr = env->GetFieldID(jcSetDataMgr, "intArr", "[I");
if(NULL == jiarr){
DEBUG_OUT("jfbdata fail!");
} // 获取Java中数组属性arrays的对象
jintArray jint_arr = (jintArray)env->GetObjectField(jobj, jiarr); int arrInt[] = {};
for(int i = ; i < ; ++i){
arrInt[i] = +i;
}
env->SetIntArrayRegion(jint_arr, , , arrInt); jfieldID jbyteArr = env->GetFieldID(jcSetDataMgr, "byteArr", "[B");
char arrByte[] = "";
jbyteArray jbyte_arr = (jbyteArray)env->GetObjectField(jobj, jbyteArr);
env->SetByteArrayRegion(jbyte_arr, , , (const signed char*)arrByte); DEBUG_OUT("test set data end!"); return NULL;
}

5,单独讲解:

①查找java类:

jclass jcSetDataMgr = env->FindClass("com/dasea/test/core/TestSetData");

②查找类成员变量:

 jfieldID jfbdata = env->GetFieldID(jcSetDataMgr, "bData", "Z");
jfieldID jfddata = env->GetFieldID(jcSetDataMgr, "dData", "D");
jfieldID jfidata = env->GetFieldID(jcSetDataMgr, "iData", "I");
jfieldID jfcdata = env->GetFieldID(jcSetDataMgr, "cData", "B");
jfieldID jfsdata = env->GetFieldID(jcSetDataMgr, "sData", "Ljava/lang/String;");
jfieldID jbyteArr = env->GetFieldID(jcSetDataMgr, "byteArr", "[B"