当前位置:网站首页>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
}
边栏推荐
- How to choose fund products? What fund is suitable to buy in July 2022?
- 201215-03-19 - cocos2dx memory management - specific explanation "recommended collection"
- 95年专注安全这一件事 沃尔沃未来聚焦智能驾驶与电气化领域安全
- Numerical method for solving optimal control problem (0) -- Definition
- What stocks can a new account holder buy? Is the stock trading account safe
- Demon daddy A3 stage near normal speed speech flow initial contact
- 开户还得用身份证银行卡安全吗,我是小白不懂
- 恶魔奶爸 A3阶段 近常速语流初接触
- Don't fall behind! Simple and easy-to-use low code development to quickly build an intelligent management information system
- gridView自己定义做时间排版「建议收藏」
猜你喜欢
Magic weapon - sensitive file discovery tool
[paper reading] maps: Multi-Agent Reinforcement Learning Based Portfolio Management System
Klocwork 代码静态分析工具
Solve the problem of using uni app mediaerror mediaerror errorcode -5
万字总结数据存储,三大知识点
Don't fall behind! Simple and easy-to-use low code development to quickly build an intelligent management information system
Is embedded system really safe? [how does onespin comprehensively solve the IC integrity problem for the development team]
Ubuntu安装mysql8遇到的问题以及详细安装过程
【C语言】指针进阶---指针你真的学懂了吗?
SQL注入报错注入函数图文详解
随机推荐
easyui 日期控件清空值
单词反转实现「建议收藏」
Default constraint and zero fill constraint of MySQL constraint
MySQL storage expression error
Cocos2d-x 游戏存档[通俗易懂]
How to choose financial products? Novice doesn't know anything
A brief understanding of the in arc__ bridge、__ bridge_ Retained and__ bridge_ transfer
MySQL约束之默认约束default与零填充约束zerofill
Object-C programming tips timer "suggestions collection"
Intelligent software analysis platform embold
Klocwork code static analysis tool
Alibaba cloud award winning experience: how to mount NAS file system through ECS
Focusing on safety in 1995, Volvo will focus on safety in the field of intelligent driving and electrification in the future
I have to use my ID card to open an account. Is the bank card safe? I don't understand it
Solve the problem of using uni app mediaerror mediaerror errorcode -5
Codeforces Round #275 (Div. 2) C – Diverse Permutation (构造)[通俗易懂]
Don't fall behind! Simple and easy-to-use low code development to quickly build an intelligent management information system
Implement secondary index with Gaussian redis
Codesonar enhances software reliability through innovative static analysis
MinGW MinGW-w64 TDM-GCC等工具链之间的差别与联系「建议收藏」