当前位置:网站首页>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
}
边栏推荐
- POJ 3140 Contestants Division「建议收藏」
- I wrote a markdown command line gadget, hoping to improve the efficiency of sending documents by garden friends!
- C语言多角度帮助你深入理解指针(1. 字符指针2. 数组指针和 指针数组 、数组传参和指针传参3. 函数指针4. 函数指针数组5. 指向函数指针数组的指针6. 回调函数)
- sqlHelper的增删改查
- 神兵利器——敏感文件发现工具
- Contour layout of margin
- Small guide for rapid formation of manipulator (11): standard nomenclature of coordinate system
- 寫一下跳錶
- 【OpenCV 例程200篇】223. 特征提取之多边形拟合(cv.approxPolyDP)
- UVA 11080 – Place the Guards(二分图判定)
猜你喜欢
The little money made by the program ape is a P!
Solve the problem of using uni app mediaerror mediaerror errorcode -5
万字总结数据存储,三大知识点
Ubuntu安装mysql8遇到的问题以及详细安装过程
C语言 整型 和 浮点型 数据在内存中存储详解(内含原码反码补码,大小端存储等详解)
Codesonar enhances software reliability through innovative static analysis
Details of C language integer and floating-point data storage in memory (including details of original code, inverse code, complement, size end storage, etc.)
[paper reading] maps: Multi-Agent Reinforcement Learning Based Portfolio Management System
How to meet the dual needs of security and confidentiality of medical devices?
神兵利器——敏感文件发现工具
随机推荐
Demon daddy B2 breaks through grammar and completes orthodox oral practice
POJ 3140 Contestants Division「建议收藏」
AADL inspector fault tree safety analysis module
What are the official stock trading apps in the country? Is it safe to use
国家正规的股票交易app有哪些?使用安不安全
Codeforces 474 F. Ant colony
Codeforces round 275 (Div. 2) C – diverse permutation (construction) [easy to understand]
万字总结数据存储,三大知识点
使用高斯Redis实现二级索引
Write a jump table
开户必须往账户里面赚钱吗,资金安全吗?
开户还得用身份证银行卡安全吗,我是小白不懂
Magic weapon - sensitive file discovery tool
I have to use my ID card to open an account. Is the bank card safe? I don't understand it
Using enumeration to realize English to braille
Unity3d 4.3.4f1 execution project
Datatable data conversion to entity
Addition, deletion, modification and query of sqlhelper
恶魔奶爸 A1 语音听力初挑战
HOJ 2245 浮游三角胞(数学啊 )