JNI's JNINativeMethod - Android

Reprinted from: JNI------JNINativeMethod_private kitchen blog-CSDN blog

 

Official definition of JNINativeMethod structure 

typedef struct {  
    const char* name;  
    const char* signature;  
    void* fnPtr;  
} JNINativeMethod;

The first variable name is the name of the function in Java.

The second variable, signature, uses a string to describe the parameters and return value of the function in Java.

The third variable fnPtr is a function pointer, pointing to the native function. It must be preceded by (void *)

The first variable corresponds to the third variable. One is the java layer method name, which corresponds to the native method name of the third parameter.

Example:

static JNINativeMethod methods[] = {  
  { "printJNI", "()I",        (void*)printForTest},  
    
}; 

 

Introduction to the second parameter

Mainly because the second parameter is more complicated:

The type of parameter is indicated inside the brackets, and the return value is indicated after the brackets.

The characters in "()" represent parameters, and the following characters represent return values. For example, "()V" means void Fun();

"(II)V" 表示 void Fun(int a, int b);

The basic type of the second parameter

 

The object type and array type of the second parameter

Object type : starts with "L", ends with ";", and is separated by "/". As shown in the first row of the table.

Array type : starts with "[", as shown in the second row of the table (for an n-dimensional array, there will be n "["s in front, such as "[[[D" means double[ ][ ][ ]).

Object array type : It is a combination of the above two, as shown in the third row of the table.
 

Example

 

 Take a look combined with Media

static JNINativeMethod gMethods[] = {
    {
        "_setDataSource",
        "(Ljava/lang/String;[Ljava/lang/String;[Ljava/lang/String;)V",
        (void *)android_media_MediaPlayer_setDataSourceAndHeaders
    },
 
    {"_setDataSource",       "(Ljava/io/FileDescriptor;JJ)V",    (void *)android_media_MediaPlayer_setDataSourceFD},
    {"_setVideoSurface",    "(Landroid/view/Surface;)V",        (void *)android_media_MediaPlayer_setVideoSurface},
    {"prepare",             "()V",                              (void *)android_media_MediaPlayer_prepare},
    {"prepareAsync",        "()V",                              (void *)android_media_MediaPlayer_prepareAsync},
    {"_start",              "()V",                              (void *)android_media_MediaPlayer_start},
    {"_stop",               "()V",                              (void *)android_media_MediaPlayer_stop},
    {"getVideoWidth",       "()I",                              (void *)android_media_MediaPlayer_getVideoWidth},
    {"getVideoHeight",      "()I",                              (void *)android_media_MediaPlayer_getVideoHeight},
    {"seekTo",              "(I)V",                             (void *)android_media_MediaPlayer_seekTo},
    {"_pause",              "()V",                              (void *)android_media_MediaPlayer_pause},
    {"isPlaying",           "()Z",                              (void *)android_media_MediaPlayer_isPlaying},
    {"getCurrentPosition",  "()I",                              (void *)android_media_MediaPlayer_getCurrentPosition},
    {"getDuration",         "()I",                              (void *)android_media_MediaPlayer_getDuration},
    {"_release",            "()V",                              (void *)android_media_MediaPlayer_release},
    {"_reset",              "()V",                              (void *)android_media_MediaPlayer_reset},
    {"setAudioStreamType",  "(I)V",                             (void *)android_media_MediaPlayer_setAudioStreamType},
    {"setLooping",          "(Z)V",                             (void *)android_media_MediaPlayer_setLooping},
    {"isLooping",           "()Z",                              (void *)android_media_MediaPlayer_isLooping},
    {"setVolume",           "(FF)V",                            (void *)android_media_MediaPlayer_setVolume},
    {"native_invoke",       "(Landroid/os/Parcel;Landroid/os/Parcel;)I",(void *)android_media_MediaPlayer_invoke},
    {"native_setMetadataFilter", "(Landroid/os/Parcel;)I",      (void *)android_media_MediaPlayer_setMetadataFilter},
    {"native_getMetadata", "(ZZLandroid/os/Parcel;)Z",          (void *)android_media_MediaPlayer_getMetadata},
    {"native_init",         "()V",                              (void *)android_media_MediaPlayer_native_init},
    {"native_setup",        "(Ljava/lang/Object;)V",            (void *)android_media_MediaPlayer_native_setup},
    {"native_finalize",     "()V",                              (void *)android_media_MediaPlayer_native_finalize},
    {"getAudioSessionId",   "()I",                              (void *)android_media_MediaPlayer_get_audio_session_id},
    {"setAudioSessionId",   "(I)V",                             (void *)android_media_MediaPlayer_set_audio_session_id},
    {"setAuxEffectSendLevel", "(F)V",                           (void *)android_media_MediaPlayer_setAuxEffectSendLevel},
    {"attachAuxEffect",     "(I)V",                             (void *)android_media_MediaPlayer_attachAuxEffect},
    {"native_pullBatteryData", "(Landroid/os/Parcel;)I",        (void *)android_media_MediaPlayer_pullBatteryData},
    {"setParameter",        "(ILandroid/os/Parcel;)Z",          (void *)android_media_MediaPlayer_setParameter},
    {"getParameter",        "(ILandroid/os/Parcel;)V",          (void *)android_media_MediaPlayer_getParameter},
    {"native_setRetransmitEndpoint", "(Ljava/lang/String;I)I",  (void *)android_media_MediaPlayer_setRetransmitEndpoint},
    {"setNextMediaPlayer",  "(Landroid/media/MediaPlayer;)V",   (void *)android_media_MediaPlayer_setNextMediaPlayer},
    {"updateProxyConfig", "(Landroid/net/ProxyProperties;)V", (void *)android_media_MediaPlayer_updateProxyConfig},
};

like,

{"getParameter",        "(ILandroid/os/Parcel;)V", 
 (void *)android_media_MediaPlayer_getParameter},


It represents the java layer function void getParameter(int a, Parcel b), the return value is void, and the formal parameters are init type and Parcel type.
Another example,

{"native_setRetransmitEndpoint", "(Ljava/lang/String;I)I", 
 (void *)android_media_MediaPlayer_setRetransmitEndpoint},


It represents the java layer function int native_setRetransmitEndpoint(String a, int b), the return value is int, and the formal parameters are String type and int type.
 

Guess you like

Origin blog.csdn.net/xiaopei_yan/article/details/130885366