其他分享
首页 > 其他分享> > JNI函数加载

JNI函数加载

作者:互联网

注册native函数

当Java代码中执行Native的代码的时候,首先是通过一定的方法来找到这些native方法。JNI有如下两种注册native方法:

  1. 编写Java代码编译成.class文件;
  2. 使用Javah,例(javah -o output packagename.classname),这样他会生成output.h的JNI层头文件。其中packagename.classname是Java代码编译后的.class文件,而在生成的output.h文件里,声明了对应JNI函数,只要实现里面的函数即可。一般头文件名字会使用packagename_class.h的样式,例如JNIEXPORT void JNICALL Java_android_media_MediaScanner_processFile

下面主要讲一下动态注册,动态注册比较方便jni的移植,而静态注册如果移植到其他的packet,需要修改所有的jni函数名.加载的效率上,动态注册也优于静态注册.

更多教程请访问码农之家

动态注册

  1. System.loadLibrary 首先需要Java层代码通过System.loadLibrary函数加载so库
static {
        System.loadLibrary("nativeffmpeg");
        native_init();
    }

执行System.loadLibrary()函数时,第一件事是调用JNI_OnLoad()函数,如果JNI Lib没有定义JNI_OnLoad,则dvm调用dvmResolveNativeMethod进行动态解析.

  1. JNI_OnLoad 在jni的文件中重载JNI_OnLoad函数
jint JNI_OnLoad(JavaVM* vm, void* /* reserved */)
{
    JNIEnv* env = NULL;
    jint result = -1;

    javaVM = vm;

    if (vm->GetEnv((void**) &env, JNI_VERSION_1_4) != JNI_OK) {
        ALOGE("ERROR: GetEnv failedn");
        goto bail;
    }
    assert(env != NULL);

    if (register_native_MediaPlayer(env) < 0) {
        ALOGE("ERROR: MediaPlayer native registration failed");
        goto bail;
    }
    /* success -- return valid version number */
    result = JNI_VERSION_1_4;

    bail:
    return result;

JNI_OnLoad函数主要分成两部分:

  1. 获取JNIEnv
vm->GetEnv((void**) &env, JNI_VERSION_1_4)

这里调用了GetEnv函数时为了获取JNIEnv结构体指针, 后面通过JNIEnv提供的函数注册native 方法

  1. 注册native方法
static int registerNativeMethods(JNIEnv* env, const char* className, JNINativeMethod* gMethods, int methodsNum) {
    jclass clazz;
    //找到声明native方法的类
    clazz = env->FindClass(className);
    if(clazz == NULL){
        return JNI_FALSE;
    }
    //注册函数 参数:java类 所要注册的函数数组 注册函数的个数
    if(env->RegisterNatives(clazz, gMethods, methodsNum) < 0){
        return JNI_FALSE;
    }
    return JNI_TRUE;
}

static int register_native_MediaPlayer(JNIEnv* env){
    //指定类的路径,通过FindClass 方法来找到对应的类
    const char* className  = "pri/tool/ffmediaplayer/MediaPlayer";
    return registerNativeMethods(env, className, gMethods, sizeof(gMethods)/ sizeof(gMethods[0]));
}
static JNINativeMethod gMethods[] = {
        {"native_init",         "()V",                              (void *)pri_tool_MediaPlayer_native_init},
        {"native_setup",        "(Ljava/lang/Object;)V",            (void *)pri_tool_MediaPlayer_native_setup},
        {"native_setDataSource",        "(Ljava/lang/String;)V",            (void *)pri_tool_MediaPlayer_native_setDataSource},
        {"native_prepareAsync",        "()V",            (void *)pri_tool_MediaPlayer_native_prepareAsync},
        {"native_testCallback",        "(Z)V",            (void *)pri_tool_MediaPlayer_native_testCallback},
        {"native_setSurface",        "(Ljava/lang/Object;)V",            (void *)pri_tool_MediaPlayer_native_setSurface},
        {"native_start",        "()V",            (void *)pri_tool_MediaPlayer_native_start},
        {"native_stop",        "()V",            (void *)pri_tool_MediaPlayer_native_stop},
        {"native_reset",        "()V",            (void *)pri_tool_MediaPlayer_native_reset},
        {"native_release",        "()V",            (void *)pri_tool_MediaPlayer_native_release},
        {"native_pause",        "()V",            (void *)pri_tool_MediaPlayer_native_pause},
};
  1. 在Java文件声明对应的native方法
 private static native final void native_init();
    private native final void native_setup(Object mediaplayer_this);
    private native final void native_setDataSource(String filePath);
    private native final void native_testCallback(boolean bNewThread);
    private native final void native_prepareAsync();
    private native final void native_setSurface(Object surface);
    private native final void native_start() throws IllegalStateException;
    private native final void native_stop() throws IllegalStateException;
    private native final void native_reset() throws IllegalStateException;
    private native final void native_release() throws IllegalStateException;
    private native final void native_pause() throws IllegalStateException;

函数签名

在注册native函数时有这样的定义

 {"native_setSurface",        "(Ljava/lang/Object;)V",            (void *)pri_tool_MediaPlayer_native_setSurface},

其中(Ljava/lang/Object;)V为对应的函数的签名. Java支持函数重载,可以定义相同方法名,但是不同参数的方法,然后Java根据其不同的参数,找到其对应的实现的方法.

函数签名具体格式如下: (参数1类型标示;参数2类型标示;参数3类型标示...)返回值类型标示

类型标示

Java类型

Z

boolean

B

byte

C

char

S

short

I

int

J

long

F

float

D

double

V

void

[数组

数组

[i

int[]

[Ljava/lang/Object

String[]

参考 https://www.jianshu.com/p/fe9ad8e5e77a

标签:函数,MediaPlayer,void,JNI,加载,tool,native
来源: https://www.cnblogs.com/myhomepages/p/16505302.html