JNI函数的2种书写方式

一、静态注册

  • 原理:
  1. 根据函数名来建立 java 方法与 JNI 函数的一一对应关系;
  2. 以Java为前缀,并且用“_”下划线,将包名、类名以及native方法名连接起来;
  • 实现流程:
  1. 编写 java 代码;
  2. 利用 javah 指令生成对应的 c 文件;
  3. 对 c 文件中的声明进行实现;
 public class MainActivity extends AppCompatActivity {
 
     // Used to load the 'native-lib' library on application startup.
     static {
         System.loadLibrary("native-lib");
     }
 
     @Override
     protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // Example of a call to a native method
        TextView tv = findViewById(R.id.sample_text);
        tv.setText(stringFromJNI());
    }

    /**
     * A native method that is implemented by the 'native-lib' native library,
     * which is packaged with this application.
     */
    public native String stringFromJNI();
}

 

 #include <jni.h>
 #include <string>
 
 extern "C" JNIEXPORT jstring JNICALL
 Java_com_example_myapplication_MainActivity_stringFromJNI(
         JNIEnv *env,
         jobject /* this */) {
     std::string hello = "Hello from C++";
     return env->NewStringUTF(hello.c_str());
}

二、动态注册

  • 原理:
  1. 利用 RegisterNatives 方法来注册 java 方法与 JNI 函数的一一对应关系;
  • 实现流程:
  1. 利用结构体 JNINativeMethod 数组记录 java 方法与 JNI 函数的对应关系;
  2. 实现 JNI_OnLoad 方法,在加载动态库后,执行动态注册;
  3. 调用 FindClass 方法,获取 java 对象;
  4. 调用 RegisterNatives 方法,传入 java 对象,以及 JNINativeMethod 数组,以及注册数目完成注册;
 public class JniUtils {
 
     static {
         System.loadLibrary("native-lib");
     }
 
     public static native String stringFromJNI();
 
     public static native int calculateByJNI(int count);
}

 

 #include <jni.h>
 #include <string>
 
 // 指定要注册的类
 #define JNIREG_CLASS "com/example/jnilibrary/JniUtils"
 
 // 指定代码所在的段。在编译时,把该函数编译到自定义的section里。
 // 由于在java层没有定义该函数,因此需要写到一个自定义的section里。
 extern "C"
__attribute__((section(".mysection"))) JNICALL jstring getStr1(JNIEnv *env, jobject obj) {
    return env->NewStringUTF("Hello from C++");
}

extern "C"
__attribute__((section(".mysection"))) JNICALL jint getInt1(JNIEnv *env, jobject obj, jint count) {
    return (count + 1);
}

// 第一个参数:Java层的方法名
// 第二个参数:方法的签名,括号内为参数类型,后面为返回类型
// 第三个参数:需要重新注册的方法名
static JNINativeMethod getMethods[] = {
        {"stringFromJNI",  "()Ljava/lang/String;", (void *) getStr1},
        {"calculateByJNI", "(I)I",                 (void *) getInt1}
};

extern "C"
int registerNativeMethods(JNIEnv *env, const char *className, JNINativeMethod *getMethods,
                          int numMethods) {
    jclass clazz;
    clazz = env->FindClass(className);
    if (clazz == NULL) {
        return JNI_FALSE;
    }
    if (env->RegisterNatives(clazz, getMethods, numMethods) < 0) {
        return JNI_FALSE;
    }
    return JNI_TRUE;
}

extern "C"
int registerNatives(JNIEnv *env) {
    if (!registerNativeMethods(env, JNIREG_CLASS, getMethods,
                               sizeof(getMethods) / sizeof(getMethods[0]))) {
        return JNI_FALSE;
    }
    return JNI_TRUE;
}

extern "C"
jint JNI_OnLoad(JavaVM *vm, void *reserved) {
    JNIEnv *env;
    if (vm->GetEnv((void **) (&env), JNI_VERSION_1_6) != JNI_OK) {
        return -1;
    }
    if (!registerNatives(env)) {
        return -1;
    }
    return JNI_VERSION_1_6;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章