1.获取逻辑:
2.获取逻辑实现代码:
extern "C"
JNIEXPORT void JNICALL
Java_com_example_jnibasetypedemo_MainActivity_getAnimalsNameFromJNI(JNIEnv *env, jobject thiz,jobjectArray animals) {jobject animalObj;int len = env->GetArrayLength(animals);//获取数组长度jclass animalClass = env->FindClass("com/example/jnibasetypedemo/Animal");//获取类对象//获取类字段jfieldID animalClassNameFiels = env->GetFieldID(animalClass, //类对象"name", //字段名"Ljava/lang/String;");//字段签名jstring _jni_string;const char *cplusplus_string;//遍历数组for (int i = 0; i < len; ++i) {animalObj = env->GetObjectArrayElement(animals, i);//取得元素//取类对象字段_jni_string = (jstring) (env->GetObjectField(animalObj, //类对象animalClassNameFiels));//类对字段对象//jstring转char*cplusplus_string = env->GetStringUTFChars(_jni_string, NULL);if (cplusplus_string == NULL) {continue;}LOGD("===>Animal类字段name值:%s", cplusplus_string);//释放内存env->ReleaseStringUTFChars(_jni_string, cplusplus_string);}
}
3.java实现:
声明JNI原生方法
创建Animal类:
package com.example.jnibasetypedemo;public class Animal{public String name;public Animal(String name) {this.name = name;}public String getName() {return name;}public void setName(String name) {this.name = name;}public Animal() {}
}
调用原生方法并输入Animal类数组
Animal[] animals = new Animal[30];for (int i = 0; i < 30; i++) {Animal ani = new Animal();ani.setName("animal name"+(i+1));animals[i]=ani;}getAnimalsNameFromJNI(animals);
4.完整源码地址:
https://github.com/tonyimax/JniBaseTypeDemo.git
5. 输出:
2025-01-12 13:29:51.211 29675-29675 JNI_LOG com.example.jnibasetypedemo D ===>Animal类字段name值:animal name1
2025-01-12 13:29:51.211 29675-29675 JNI_LOG com.example.jnibasetypedemo D ===>Animal类字段name值:animal name2
2025-01-12 13:29:51.211 29675-29675 JNI_LOG com.example.jnibasetypedemo D ===>Animal类字段name值:animal name3
2025-01-12 13:29:51.211 29675-29675 JNI_LOG com.example.jnibasetypedemo D ===>Animal类字段name值:animal name4
2025-01-12 13:29:51.211 29675-29675 JNI_LOG com.example.jnibasetypedemo D ===>Animal类字段name值:animal name5
2025-01-12 13:29:51.211 29675-29675 JNI_LOG com.example.jnibasetypedemo D ===>Animal类字段name值:animal name6
2025-01-12 13:29:51.212 29675-29675 JNI_LOG com.example.jnibasetypedemo D ===>Animal类字段name值:animal name7
2025-01-12 13:29:51.212 29675-29675 JNI_LOG com.example.jnibasetypedemo D ===>Animal类字段name值:animal name8
2025-01-12 13:29:51.212 29675-29675 JNI_LOG com.example.jnibasetypedemo D ===>Animal类字段name值:animal name9
2025-01-12 13:29:51.212 29675-29675 JNI_LOG com.example.jnibasetypedemo D ===>Animal类字段name值:animal name10
2025-01-12 13:29:51.212 29675-29675 JNI_LOG com.example.jnibasetypedemo D ===>Animal类字段name值:animal name11
2025-01-12 13:29:51.212 29675-29675 JNI_LOG com.example.jnibasetypedemo D ===>Animal类字段name值:animal name12
2025-01-12 13:29:51.212 29675-29675 JNI_LOG com.example.jnibasetypedemo D ===>Animal类字段name值:animal name13
2025-01-12 13:29:51.212 29675-29675 JNI_LOG com.example.jnibasetypedemo D ===>Animal类字段name值:animal name14
2025-01-12 13:29:51.212 29675-29675 JNI_LOG com.example.jnibasetypedemo D ===>Animal类字段name值:animal name15
2025-01-12 13:29:51.212 29675-29675 JNI_LOG com.example.jnibasetypedemo D ===>Animal类字段name值:animal name16
2025-01-12 13:29:51.212 29675-29675 JNI_LOG com.example.jnibasetypedemo D ===>Animal类字段name值:animal name17
2025-01-12 13:29:51.212 29675-29675 JNI_LOG com.example.jnibasetypedemo D ===>Animal类字段name值:animal name18
2025-01-12 13:29:51.212 29675-29675 JNI_LOG com.example.jnibasetypedemo D ===>Animal类字段name值:animal name19
2025-01-12 13:29:51.212 29675-29675 JNI_LOG com.example.jnibasetypedemo D ===>Animal类字段name值:animal name20
2025-01-12 13:29:51.212 29675-29675 JNI_LOG com.example.jnibasetypedemo D ===>Animal类字段name值:animal name21
2025-01-12 13:29:51.212 29675-29675 JNI_LOG com.example.jnibasetypedemo D ===>Animal类字段name值:animal name22
2025-01-12 13:29:51.213 29675-29675 JNI_LOG com.example.jnibasetypedemo D ===>Animal类字段name值:animal name23
2025-01-12 13:29:51.213 29675-29675 JNI_LOG com.example.jnibasetypedemo D ===>Animal类字段name值:animal name24
2025-01-12 13:29:51.213 29675-29675 JNI_LOG com.example.jnibasetypedemo D ===>Animal类字段name值:animal name25
2025-01-12 13:29:51.213 29675-29675 JNI_LOG com.example.jnibasetypedemo D ===>Animal类字段name值:animal name26
2025-01-12 13:29:51.213 29675-29675 JNI_LOG com.example.jnibasetypedemo D ===>Animal类字段name值:animal name27
2025-01-12 13:29:51.213 29675-29675 JNI_LOG com.example.jnibasetypedemo D ===>Animal类字段name值:animal name28
2025-01-12 13:29:51.213 29675-29675 JNI_LOG com.example.jnibasetypedemo D ===>Animal类字段name值:animal name29
2025-01-12 13:29:51.213 29675-29675 JNI_LOG com.example.jnibasetypedemo D ===>Animal类字段name值:animal name30