当前位置:网站首页>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
}
边栏推荐
- AADL inspector fault tree safety analysis module
- Details of C language integer and floating-point data storage in memory (including details of original code, inverse code, complement, size end storage, etc.)
- DataTable数据转换为实体
- Do you have to make money in the account to open an account? Is the fund safe?
- 华泰证券可以做到万一佣金吗,万一开户安全嘛
- Implement secondary index with Gaussian redis
- Flask1.1.4 werkzeug1.0.1 source code analysis: Routing
- Make this crmeb single merchant wechat mall system popular, so easy to use!
- What are the official stock trading apps in the country? Is it safe to use
- Write a jump table
猜你喜欢
目标:不排斥 yaml 语法。争取快速上手
Default constraint and zero fill constraint of MySQL constraint
H3C s7000/s7500e/10500 series post stack BFD detection configuration method
MySQL约束之默认约束default与零填充约束zerofill
Usage of MySQL subquery keywords (exists)
C语言多角度帮助你深入理解指针(1. 字符指针2. 数组指针和 指针数组 、数组传参和指针传参3. 函数指针4. 函数指针数组5. 指向函数指针数组的指针6. 回调函数)
Is embedded system really safe? [how does onespin comprehensively solve the IC integrity problem for the development team]
How to meet the dual needs of security and confidentiality of medical devices?
C language helps you understand pointers from multiple perspectives (1. Character pointers 2. Array pointers and pointer arrays, array parameter passing and pointer parameter passing 3. Function point
Solve the problem of using uni app mediaerror mediaerror errorcode -5
随机推荐
Apifox interface integrated management new artifact
权限不足
单词反转实现「建议收藏」
The little money made by the program ape is a P!
EasyUI date control emptying value
恶魔奶爸 B2 突破语法,完成正统口语练习
How to choose financial products? Novice doesn't know anything
MySQL storage expression error
恶魔奶爸 C
Is it safe to open an account of BOC shares in kainiu in 2022?
Is it safe to open an account online now? I want to know where I can open an account in Nanning now?
How to meet the dual needs of security and confidentiality of medical devices?
FatMouse&#39; Trade(杭电1009)
Codeforces 474 F. Ant colony
easyui 日期控件清空值
OpenGL super classic learning notes (1) the first triangle "suggestions collection"
万字总结数据存储,三大知识点
使用高斯Redis实现二级索引
华泰证券可以做到万一佣金吗,万一开户安全嘛
Description of the difference between character varying and character in PostgreSQL database