JNI开发有时需要C/C++传递对象到Java,此篇主要讲解C/C++传递Student对象到Java;
1. 定义com.niubashaoye.simple.jni.StuInfo类
public class StuInfo {
private int stuId;
private String stuName;
private int stuAge;
private String className;
public StuInfo(int stuId, String stuName, int stuAge, String className) {
super();
this.stuId = stuId;
this.stuName = stuName;
this.stuAge = stuAge;
this.className = className;
}
//getter()
//setter()
@Override
public String toString() {
return "StuInfo [stuId=" + stuId + ", stuName=" + stuName + ", stuAge=" + stuAge + ", className=" + className
+ "]";
}
}
2. 添加native函数
public class JNITools {
static {
System.loadLibrary("TestDemo");
}
/**
* 获取学生信息
*
* @param stuInfo
* @return
*/
public native StuInfo getStuInfo();
} * 获取学生信息
*
* @param stuInfo
* @return
*/
public native StuInfo getStuInfo();
}
3. C/C++文件
3.1 添加StuInfo结构体
typedef struct {
int stuId;
char stuName[50];
int stuAge;
char className[50];
} StuInfo;
3.2 获取StuInfo对象
JNIEXPORT jobject JNICALL Java_com_niubashaoye_simple_jni_JNITools_getStuInfo(
JNIEnv *env, jobject obj) {
//给StuInfo结构体赋值
StuInfo stuInfo;
stuInfo.stuId = 100123;
const char *name = "我是我自己!";
strcpy(stuInfo.stuName, name);
const char *className = "高三六班";
strcpy(stuInfo.className, className);
//获取jclass的实例
jclass jcs = env->FindClass("com/niubashaoye/simple/jni/StuInfo");
//获取StuInfo的字段ID
jfieldID fileId = env->GetFieldID(jcs, "stuId", "I");
jfieldID nameId = env->GetFieldID(jcs, "stuName", "Ljava/lang/String;");
jfieldID ageId = env->GetFieldID(jcs, "stuAge", "I");
jfieldID classId = env->GetFieldID(jcs, "className", "Ljava/lang/String;");
//创建StuInfo对象
jobject stuObje = env->AllocObject(jcs);
//设置变量值
env->SetIntField(stuObje, fileId, stuInfo.stuId);
env->SetIntField(stuObje, ageId, stuInfo.stuAge);
env->SetObjectField(stuObje, nameId, env->NewStringUTF(stuInfo.stuName));
env->SetObjectField(stuObje, classId, env->NewStringUTF(stuInfo.className));
return stuObje;
}JNIEnv *env, jobject obj) {
//给StuInfo结构体赋值
StuInfo stuInfo;
stuInfo.stuId = 100123;
const char *name = "我是我自己!";
strcpy(stuInfo.stuName, name);
const char *className = "高三六班";
strcpy(stuInfo.className, className);
//获取jclass的实例
jclass jcs = env->FindClass("com/niubashaoye/simple/jni/StuInfo");
//获取StuInfo的字段ID
jfieldID fileId = env->GetFieldID(jcs, "stuId", "I");
jfieldID nameId = env->GetFieldID(jcs, "stuName", "Ljava/lang/String;");
jfieldID ageId = env->GetFieldID(jcs, "stuAge", "I");
jfieldID classId = env->GetFieldID(jcs, "className", "Ljava/lang/String;");
//创建StuInfo对象
jobject stuObje = env->AllocObject(jcs);
//设置变量值
env->SetIntField(stuObje, fileId, stuInfo.stuId);
env->SetIntField(stuObje, ageId, stuInfo.stuAge);
env->SetObjectField(stuObje, nameId, env->NewStringUTF(stuInfo.stuName));
env->SetObjectField(stuObje, classId, env->NewStringUTF(stuInfo.className));
return stuObje;
}
4. Java调用native函数
StuInfo stuInfo = jniTools.getStuInfo();
Toast.makeText(getApplicationContext(), stuInfo.toString(), Toast.LENGTH_SHORT).show();
代码下载:https://download.csdn.net/download/niuba123456/10533705