NDK Native Development in the static registration and dynamic registration method

Disclaimer: This article is a blogger original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
This link: https://blog.csdn.net/Kennethdroid/article/details/88356400

Truly important gains, often from continuous hard thinking.

This article first appeared in the public micro-channel number "byte floating"

NDK development of this blog series:

Native method static registration

NDK development by javah -jnigenerating command contains JNI header file, the interface is generally named Java_<PackageName>_<ClassName>_<MethodName>, the system calls corresponding to this naming Native method during program execution, this way is called static registration register.

package com.haohao.framework;

public class NDKFramework {

    private native int native_CreateFramework(String packageName);

    private native void native_DestroyFramework();
}
/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* Header for class com_haohao_framework_NDKFramework */

#ifndef _Included_com_haohao_framework_NDKFramework
#define _Included_com_haohao_framework_NDKFramework
#ifdef __cplusplus
extern "C" {
#endif
/*
 * Class:     com_haohao_framework_NDKFramework
 * Method:    native_CreateFramework
 * Signature: (Ljava/lang/String;)I
 */
JNIEXPORT jint JNICALL Java_com_haohao_framework_NDKFramework_native_1CreateFramework
  (JNIEnv *, jobject, jstring);

/*
 * Class:     com_haohao_framework_NDKFramework
 * Method:    native_DestroyFramework
 * Signature: ()V
 */
JNIEXPORT void JNICALL Java_com_haohao_framework_NDKFramework_native_1DestroyFramework
  (JNIEnv *, jobject);

#ifdef __cplusplus
}
#endif
#endif

Registration advantage of static methods: easy and convenient, IDE (high version of AndroidStudio) can automatically help you complete; drawback: JNI (Native Method) name is too long, poor readability, because the rules of naming restrictions, can not be flexible to change.

Native dynamic registration method

Due to the limitations of static registration name exists, the production environment is generally not used as static registration. The advantages of dynamic registration is free to name Native method, the disadvantage is that if too many Native method, the operation is too much trouble.

Dynamic registration time is registered when loading libraries (.a or .so), that is JNI_OnLoadregistered in the method.

After renaming Native method:

#include <jni.h>
#include <string>

#define CLASS_NAME_NDK_FRAMEWORK      "com/haohao/framework/NDKFramework"

extern "C"
JNIEXPORT jint JNICALL
CreateFramework(JNIEnv *env, jobject instance, jstring jPackageName)
{
    LOGCATE("native_CreateFramework");
	jint jRet = JNI_ERR;
	const char *packageName = env->GetStringUTFChars(jPackageName, 0);

    //if(NDKFramework::CreateFramework(packageName) != NULL)
    //	jRet = JNI_OK;

    env->ReleaseStringUTFChars(jPackageName, packageName);
	return jRet;
}

extern "C"
JNIEXPORT void JNICALL
DestroyFramework(JNIEnv *env, jobject instance)
{
    LOGCATE("native_DestroyFramework");
	//NDKFramework::DestroyFramework();
}

Native method defined array:

//{"Java 方法名", "JNI 签名", "重命名的 Native 方法"}
static JNINativeMethod g_NDKFrameMethods[] = {
        { "native_CreateFramework", 				"(Ljava/lang/String;)I", 		(void *)CreateFramework},
        { "native_DestroyFramework",				"()V",					 		(void *)DestroyFramework}

};

//定义注册函数
static int RegisterNativeMethods(JNIEnv* env, const char* className, JNINativeMethod* methods, int methodNum)
{
    LOGCATE("RegisterNativeMethods");
    jclass clazz = env->FindClass(className);
    if (clazz == NULL)
    {
        return JNI_FALSE;
    }
    if (env->RegisterNatives(clazz, methods, methodNum) < 0)
    {
        return JNI_FALSE;
    }
    return JNI_TRUE;
}

In the JNI_OnLoadregistration method in:

extern "C" jint JNI_OnLoad(JavaVM *jvm, void *p)
{
    LOGCATE("================ JNI_OnLoad ================");

    jint jniRet = JNI_ERR;
    JNIEnv *env = NULL;
    if (jvm->GetEnv((void **)(&env), JNI_VERSION_1_6) != JNI_OK)
        return jniRet;

    jint regRet = RegisterNativeMethods(env, CLASS_NAME_NDK_FRAMEWORK, g_NDKFrameMethods, sizeof(g_NDKFrameMethods) /
            sizeof(g_NDKFrameMethods[0]));
    if(regRet != JNI_TRUE)
        return JNI_ERR;

    return JNI_VERSION_1_6;
}

3 or more steps can be dynamically register.

Contacts and exchanges

Micro-channel public number of
My public number
individual micro letter
My WeChat

Guess you like

Origin blog.csdn.net/Kennethdroid/article/details/88356400