JNI之④C/C++创建Java对象 发表于 2017-01-04 15:41:57 | 分类于 Android , NDK | 步骤 获取Java对象的jclass 获取构造函数的id,方法名使用 NewObject创建Java对象jobject 获取并调用jobject中的方法 示例1234567891011121314151617package com.michael.ndk.write;/** * C访问Java字段和方法 * Created by liuguoquan on 2017/1/3. */public class WriteJava { /** * C/C++ 调用Java对象 */ public native long c2JavaClass(); static { System.loadLibrary("native-lib"); }} 1234567891011121314151617181920extern "C"/** * C/C++ 创建Java对象 */JNIEXPORT jlong JNICALLJava_com_michael_ndk_write_WriteJava_c2JavaClass(JNIEnv *env, jobject instance) { //获取Date jclass jclass clz = env->FindClass("java/util/Date"); //获取构造方法id jmethodID methodId = env->GetMethodID(clz,"<init>","()V"); //创建Date对象 jobject obj = env->NewObject(clz,methodId); //获取getTime的方法ID jmethodID getTimeId = env->GetMethodID(clz,"getTime","()J"); //调用getTime方法 jlong time = env->CallLongMethod(obj,getTimeId); LOGI("time = %ld\n",time); return time;} JNI开发系列④C语言调用构造方法