当前位置:网站首页>JNI primary contact

JNI primary contact

2022-07-07 21:19:00 Spades_ K_

What is? JNI?

JNI(Java Native Interface),(JNI) The standard is java Part of the platform , It allows the Java Code interacts with code written in other languages .JNI It's a local programming interface , It makes in Java virtual machine (VM) Running internally Java The code can be used with other programming languages ( Such as C 、C++ And assembly language ) The written application and library interact .

Andoird Simultaneous interpreting is a different tradition Java JNI To define it native Function of .
The important difference is Andorid Used a Java and C Function mapping table array , The parameters and return values of the function are described . The type of this array is JNINativeMethod, The definition is as follows :

typedef struct {
    
  const char* name; /*Java  The name of the function in */
  const char* signature; /*  Describes the parameters and return values of the function */
  void* fnPtr; /*  A function pointer , Point to C  function */
} JNINativeMethod;

Signature explain :
Example :

--"()V"
--"(II)V"
--"(Ljava/lang/String;Ljava/lang/String;)V"

explain :

--"()"  The character in represents the parameter , The following represents the return value . for example "()V"  It means void Func();
--"(II)V"  Express  void Func(int, int);
--"(Ljava/lang/String;Ljava/lang/String;)V“  Express void Func(String, String);

stay java of use native Statement declaration , Call the function name written in the specified format .

CMakeLists.txt

cmake_minimum_required(VERSION 3.18.1) # CMake  The minimum version of 
project("myapplication")	# Project name 

add_library( 
           		myapplication
             SHARED
             myapplication.cpp )
                       
 find_library( 
              log-lib
              log )
              
target_link_libraries( 
                       myapplication
                       ${
    log-lib} )

myapplication.cpp:

#include<jni.h>

extern "C"
JNIEXPORT jint JNICALL
Java_com_example_myapplication_MainActivity_catTest(JNIEnv *env, jobject thiz) {
    
    // TODO: implement cat_test()
    std::cout<<"test function__"<<std::endl;
        return 0;
}

JNIWXPORT: stay Jni All local language implementations in programming Jni There is a method in front of the interface "JNIEXPORT", This can be seen as Jni A sign of .

JNICALL : This can be interpreted as Jni and Call Two parts , Together means Jni call XXX( hinder XXX Namely JAVA Method name of ).

Java_com_example_myapplication_MainActivity_catTest: This is the part called in the previous step , That is to say Java Medium native Method name , The way of naming here is quite special , yes : Package name + Class name + Method name .

JNIEnv* env: This env You can view it as Jni An object of the interface itself ,jni.h There are a lot of encapsulated functions in the header file , These functions are also Jni It is often used in programming , To call these functions, you need to use JNIEnv This object . for example :env->GetObjectClass().( Please check the details jni.h)

jobject thiz: Represents the native The caller of the method , This example is new NativeDemo(); But if native Is static , That's it NativeDemo.class

MainActivity:

package com.example.myapplication;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;

public class MainActivity extends AppCompatActivity {
    

    @Override
    protected void onCreate(Bundle savedInstanceState) {
    
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        catTest();
    }
    static{
    
        System.loadLibrary("myapplication"); // load C++ Generate dynamic library 
    }
    
    public native int catTest();// call C++ Code 
}
原网站

版权声明
本文为[Spades_ K_]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/188/202207071813076677.html