当前位置:网站首页>Static registration and dynamic registration of JNI

Static registration and dynamic registration of JNI

2022-06-26 17:53:00 Novice Xiaowang

Static registration

according to JNI Find according to the standard naming rules , This method is called static registration

JNI Writing function name Java_ Definition native Full path of method _ Method name

extern "C" JNIEXPORT jint JNICALL Java_com_example_opentr069_OpenTR069Native_getNatDetected(JNIEnv * env,
       jclass clazz){
    return sk_get_nat_stun_flag();
}

Dynamic registration

call JNI Provided RegisterNatives function , Register local functions with JVM in , This method is called dynamic registration .

When the library is loaded, it will automatically call JNI_OnLoad() function , Developers often JNI_OnLoad() Function to do some initialization , Dynamic registration is done here . call API yes env->RegisterNatives(clazz, gMethods, numMethods).

env->RegisterNatives(clazz, gMethods, numMethods) Is a function that accepts three parameters , The first parameter is Java The corresponding class , The second parameter is JNINativeMethod Array , The third parameter is JNINativeMethod Length of array , That is, the number of methods that need to be registered .
among JNINativeMethod It represents the mapping relationship of methods , It includes Java Method name in , Corresponding method signature and Native Mapped function method .

Compared with static registration , Dynamic registration is more flexible , If it changes java native Package name or class name of the class where the function is located , Adjust only Java native Function signature information .

//  Automatically call... When the class library is loaded 
JNIEXPORT jint JNICALL JNI_OnLoad(JavaVM *vm, void *reversed)
{
    JNIEnv *env = NULL;
    //  initialization JNIEnv
    if(vm->GetEnv(reinterpret_cast<void **>(&env), JNI_VERSION_1_6) != JNI_OK){
        return JNI_FALSE;
    }
    //  Find the one that needs dynamic registration Jni class 
    jclass jniClass = env->FindClass("com/fly/jnitest/MainActivity");
    if(nullptr == jniClass){
        return JNI_FALSE;
    }
    //  Dynamic registration 
    env->RegisterNatives(jniClass,nativeMethod,sizeof(JNINativeMethod)/sizeof(nativeMethod));
    //  return JNI Version used 
    return JNI_VERSION_1_6;
}

jstring stringFromJNI(JNIEnv *jniEnv,jobject jobj){
    return jniEnv->NewStringUTF("hello from C++ string");
}

static const JNINativeMethod nativeMethod[] = {
        // Java Function name in 
        {"stringFromJNI",
            //  Function signature information 
            "()Ljava/lang/String;",
            // native Function pointer of 
            (void *) (stringFromJNI)
        }
};

 JNI Property description

JNI The attribute descriptor, that is, the variable type, is in JNI The representation in , It is determined by the declaration type of the attribute . For example, using "I" Express int attribute , Use "F" Express float attribute , Use "D" Express double attribute , Use "Z" Express boolean Properties, etc .

For descriptors that refer to various types of attributes , such as java.lang.String, Need to be written in letters "L" start , Analytically, it is JNI Class descriptor and end with a semicolon ,Java In the complete class name ".“ By ”/" Replaced . therefore , about java.lang.String Type requires the following form of property descriptor :
Ljava/lang/String;
The descriptor of array type consists of "[" And the descriptor of array element type , for example ,[I Represents the property descriptor of an integer array

Function Descriptor

Usually we call it function signature , A function descriptor consists of its parameter type and return value type , The parameter type comes first , And use a pair of parentheses , Parameter types are listed in the order they appear in the function declaration , There is no separator between multiple parameter types , If a method has no parameters , Use a pair of empty parentheses to indicate . The return value type of the function is immediately followed by the closing bracket of the wrapped parameter type .

for example (I)V Refers to a function that receives an integer parameter and returns an empty value .()D It means that there are no input parameters , The return value is one double Function of type .

Be careful : Don't be C Functions like "int f(void)“ Such function prototypes are misleading , Misconception ”(V)I" Is its method descriptor , Actually "()I" Talent function f The function descriptor of
 

原网站

版权声明
本文为[Novice Xiaowang]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/177/202206261749039261.html