实现JNI中本地函数注册可以两种方式:
(1)采用默认的本地函数注册流程。
(2)自己重写JNI_OnLoad()函数。(本文介绍)(Android中采用这种)
Java端代码:
package com.jni; public class JavaHello { public static native String hello(); static { // load library: libtest.so try { System.loadLibrary("test"); } catch (UnsatisfiedLinkError ule) { System.err.println("WARNING: Could not load library!"); } } public static void main(String[] args) { String s = new JavaHello().hello(); System.out.println(s); } }
本地C语言代码