AndroidStudio jni C++ and Java call each other

1. Java calls C++

After C++ registers the Native method, remember to load your corresponding C++ library in the Java class.

static {
System.loadLibrary(“native-lib”);
}

1. Statically register Native functions

Declare a native method in a Java class, and AndroidStudio will remind that the method is undefined, and put the mouse on it to display the generated JNI function:
insert image description here

Clicking will automatically generate the corresponding native method in native-lib.cpp, just implement it in the method:
insert image description here

2. Dynamic Registration

Also declare the native method in the Java class.
After the declaration, implement the method yourself in native-lib.cpp.

extern "C" JNICALL void TestJni(){
    
    

}

Register the method after implementation:

static JNINativeMethod getMethods[] = {
    
    
        {
    
    "nativeTestJni","()V",(void*)TestJni},
        {
    
    "nativeGetOffer","()Ljava/lang/String;", reinterpret_cast<void*>(getOffer)},

};

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

extern "C" jint JNIEXPORT
JNICALL JNI_OnLoad(JavaVM* jvm, void* reserved) {
    
    
    JNIEnv* env = NULL;
    if (jvm->GetEnv(reinterpret_cast<void**>(&env), JNI_VERSION_1_6) != JNI_OK) {
    
    
        return -1;
    }
    assert(env != NULL);
    //注册函数 registerNatives ->registerNativeMethods ->env->RegisterNatives
    const char* className  = "com/begoit/testjni/MainActivity";
    if(!registerNativeMethods(env,className,getMethods, sizeof(getMethods)/ sizeof(getMethods[0])))
        return -1;
    //返回jni 的版本
    return JNI_VERSION_1_6;
}

Note:
1. The first parameter in JNINativeMethod is the name of the native method to be registered, which must be consistent with the method name defined in the Java class.
2. The second parameter in JNINativeMethod is the parameter and return value of the native method. The parameters in the brackets are the parameters, and the return value outside the brackets. There can be multiple parameters. Java type corresponding signature
ava type type signature

Java type type signature
boolean Z
byte B
char C
short S
int I
long J
float F
double D
kind L fully qualified name; such as String, its signature is Ljava/lang/util/String;
array [Type signature, such as [B

3. className is the Java class you want to register, pay attention to the format.

2. C++ calls Java

extern "C"
JNIEXPORT void JNICALL
Java_com_begoit_xiurtcserver_XiuRTCService_OnProcessCameraData(JNIEnv *env, jobject thiz) {
    
    
    // TODO: implement OnProcessData()
    jclass myClass = env->FindClass("com/begoit/xiurtcserver/CameraProcessor");
    if (myClass == nullptr) {
    
    
        LOGE("can not find CameraProcessor class");
        return;
    }
    LOGI("find CameraProcessor class");
    
	method = env->GetMethodID(myClass, "TestJni", "()V");
    LOGI("find GetMethodID TestJni");

    jclass xiurtc = env->GetObjectClass(thiz);
    LOGI("GetObjectClass XiuRTCService");
    
    jfieldID fid = env->GetFieldID(xiurtc, "cameraProcessor",
                                   "Lcom/begoit/xiurtcserver/CameraProcessor;");
    LOGI("GetFieldID cameraProcessor");
    
    jobject camera = env->GetObjectField(thiz, fid);
    LOGI("GetObjectField cameraProcessor");

    env->CallVoidMethod(camera, method);
    LOGI("CallVoidMethod");
}

Explanation:
1. I have a CameraProcessor type cameraProcessor in XiuRTCService class;
there is a function TestJni in CameraProcessor.
2. The native method OnProcessCameraData defined in the XiuRTCService class.

The above function can be implemented to call the TestJni method of the cameraProcessor object in XiuRTCService.

Guess you like

Origin blog.csdn.net/qq_36383272/article/details/126369969