在 JNI 里调用 Java 的方法得通过反射,而且使用起来比直接在 Java 里用反射更麻烦,因此 Natiflect 就是为了解决这个问题而写的,把 JNI 提供的一些函数进行了封装,利用 C++ 面向对象的特性,使 API 更加简单。
Android NDK 是使用 JNI 的,因此 Natiflect 也可以用于 NDK。
注意:Natiflect 用到了一些 C++ 的特性,所以只支持 C++。
在源文件头部包含头文件 natiflect.h
,并根据需要添加 using namespace natiflect;
。
jstring str1 = env->NewStringUTF("abc");
Class clz_string(env, "java/lang/String");
jstring str2 = (jstring) clz_string.NewInstance("(Ljava/lang/String;)V", str1);
Class clz(env, "java/util/Arrays");
clz.CallStatic_V("sort", "([I)V", j_int_array);
Class clz(env, "im/r_c/java/StaticFieldTest");
clz.SetStatic_Z("sBoolean", 1);
jboolean b = clz.GetStatic_Z("sBoolean");
Class clz(env, "im/r_c/java/ObjectTest");
Object<jobject> obj(env, clz.NewInstance());
jint i = obj.Call_I("testInt");
Class clz(env, "im/r_c/java/ObjectTest");
Object<jobject> obj(env, clz.NewInstance());
jstring str = env->NewStringUTF("abc");
obj.Set_L("mString", "Ljava/lang/String;", str);
str = (jstring) obj.Get_L("mString", "Ljava/lang/String;");
还有一些其它函数的用法可以查看源码或在 test 分支查看 natiflect_test.cpp
文件。
It's quite a trouble to call Java methods in JNI and Natiflect is written to make that easy.
Android NDK is using JNI, thus Natiflect can be used in NDK development as well.
Note: Some C++ features are used, so you must use Natiflect in C++.
Include header file natiflect.h
at the beginning of your source file, and add using namespace natiflect;
as you need.
jstring str1 = env->NewStringUTF("abc");
Class clz_string(env, "java/lang/String");
jstring str2 = (jstring) clz_string.NewInstance("(Ljava/lang/String;)V", str1);
Class clz(env, "java/util/Arrays");
clz.CallStatic_V("sort", "([I)V", j_int_array);
Class clz(env, "im/r_c/java/StaticFieldTest");
clz.SetStatic_Z("sBoolean", 1);
jboolean b = clz.GetStatic_Z("sBoolean");
Class clz(env, "im/r_c/java/ObjectTest");
Object<jobject> obj(env, clz.NewInstance());
jint i = obj.Call_I("testInt");
Class clz(env, "im/r_c/java/ObjectTest");
Object<jobject> obj(env, clz.NewInstance());
jstring str = env->NewStringUTF("abc");
obj.Set_L("mString", "Ljava/lang/String;", str);
str = (jstring) obj.Get_L("mString", "Ljava/lang/String;");
You can refer to the source code for usage of some other functions.