一般我们NDK编程都是Java层调用C++的接口,但其实才C++层也可以调用Java的函数。实现方法如下:
1、获取类名:jclass cls = env->FindClass
2、获取类方法:jmethodID mid = env->GetMethodID
3、获取类成员变量:fieldID fid=env->GetFieldID
4、生成类对象:jobject obj=env->NewObject (jobect也可以从Java层传下来)
5、调用类成员方法:env->CallXXXMethod(XXX为Java方法的返回值类型)
下面是一个例子:
首先是Java的代码,首先生成一个JniTest类,里面有个sayHelloFromJava的方法,我们要实现的目标是在C++里面赋值(String str),两个整形值(int index1, int index2),一个整形数组(int[] intArray),然后在Java里面将这些数值打印出来。
[java] view plain copy print ?
- public class JniTest extends Activity {
-
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- JNI j = new JNI();
- j.write();
- }
-
- public JniTest()
- {
- Log.i("TEST","JniTest Constructor");
- }
-
- public int sayHelloFromJava(String str, int index1, int index2, int[] intArray)
- {
- Log.i("TEST", str + " But I am show in java");
- Log.i("TEST", "index1 = " + index1 + " index2 = " + index2 );
- int javaIndex = 5;
- for(int i = 0; i < intArray.length; ++i)
- {
- Log.i("TEST", "intArray[i] = " + intArray[i]);
- }
- return javaIndex;
- }
- }
public class JniTest extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); JNI j = new JNI(); j.write(); } public JniTest() { Log.i("TEST","JniTest Constructor"); } public int sayHelloFromJava(String str, int index1, int index2, int[] intArray) { Log.i("TEST", str + " But I am show in java"); Log.i("TEST", "index1 = " + index1 + " index2 = " + index2 ); int javaIndex = 5; for(int i = 0; i < intArray.length; ++i) { Log.i("TEST", "intArray[i] = " + intArray[i]); } return javaIndex; } }
[java] view plain copy print ?
- public class JNI {
- static
- {
- System.loadLibrary("myjni");
- }
- public native void write();
- }
public class JNI { static { System.loadLibrary("myjni"); } public native void write(); }
然后是C++里面的代码
[cpp] view plain copy print ?
- JNIEXPORT void JNICALL Java_cc_androidos_jni_JNI_write
- (JNIEnv *env, jobject j) {
- LOGI("calltest");
- jstring str = NULL;
-
- jclass clz = env->FindClass("cc/androidos/jni/JniTest");
-
- jmethodID ctor = env->GetMethodID(clz, "<init>", "()V");
- jobject obj = env->NewObject(clz, ctor);
-
-
- jmethodID mid = env->GetMethodID(clz, "sayHelloFromJava", "(Ljava/lang/String;II[I)I");
- if (mid)
- {
- LOGI("mid is get");
- jstring str1 = env->NewStringUTF("I am Native");
- jint index1 = 10;
- jint index2 = 12;
-
-
-
- jintArray testIntArray = env->NewIntArray(10);
- jint *test = new jint[10];
- for(int i = 0; i < 10; ++i)
- {
- *(test+i) = i + 100;
- }
- env->SetIntArrayRegion(testIntArray, 0, 10, test);
-
-
- jint javaIndex = env->CallIntMethod(obj, mid, str1, index1, index2, testIntArray);
- LOGI("javaIndex = %d", javaIndex);
- delete[] test;
- test = NULL;
- }
- }
JNIEXPORT void JNICALL Java_cc_androidos_jni_JNI_write (JNIEnv *env, jobject j) { LOGI("calltest"); jstring str = NULL; jclass clz = env->FindClass("cc/androidos/jni/JniTest"); //获取clz的构造函数并生成一个对象 jmethodID ctor = env->GetMethodID(clz, "<init>", "()V"); jobject obj = env->NewObject(clz, ctor); // 如果是数组类型,则在类型前加[,如整形数组int[] intArray,则对应类型为[I,整形数组String[] strArray对应为[Ljava/lang/String; jmethodID mid = env->GetMethodID(clz, "sayHelloFromJava", "(Ljava/lang/String;II[I)I"); if (mid) { LOGI("mid is get"); jstring str1 = env->NewStringUTF("I am Native"); jint index1 = 10; jint index2 = 12; //env->CallVoidMethod(obj, mid, str1, index1, index2); // 数组类型转换 testIntArray能不能不申请内存空间 jintArray testIntArray = env->NewIntArray(10); jint *test = new jint[10]; for(int i = 0; i < 10; ++i) { *(test+i) = i + 100; } env->SetIntArrayRegion(testIntArray, 0, 10, test); jint javaIndex = env->CallIntMethod(obj, mid, str1, index1, index2, testIntArray); LOGI("javaIndex = %d", javaIndex); delete[] test; test = NULL; } }
通过这个例子我们基本上就可以了解C++层是如何回调Java函数的了。另外,这里还有一个小技巧,如果你不知道你Java层的在C++中的类型是什么,你可以native方法中将这个类型写进去,然后用javah方法生成.h文件,只要查看.h文件的对应的类型注释就可以知道结果了。例如:我们想知道String、整形数组对应的类型怎么写,我们在native中加入一个public native void type(String str, int[] arrayInt)方法
[java] view plain copy print ?
- public class JNI {
- static
- {
- System.loadLibrary("myjni");
- }
- public native void write();
- public native void type(String str, int[] arrayInt);
- }
public class JNI { static { System.loadLibrary("myjni"); } public native void write(); public native void type(String str, int[] arrayInt); }
然后生成对应的.h文件:
[cpp] view plain copy print ?
-
-
-
-
-
- JNIEXPORT void JNICALL Java_cc_androidos_jni_JNI_type
- (JNIEnv *, jobject, jstring, jintArray);
/* * Class: cc_androidos_jni_JNI * Method: type * Signature: (Ljava/lang/String;[I)V */ JNIEXPORT void JNICALL Java_cc_androidos_jni_JNI_type (JNIEnv *, jobject, jstring, jintArray);
我们注意看注释中的“Signature: (Ljava/lang/String;[I)V”,其中Ljava/lang/String;Ljava/lang/String;就是String的类型(注意分号不能丢),[I则是整形数组对应的类型。