当前位置:网站首页>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
}
边栏推荐
- awk处理JSON处理
- Demon daddy B2 breaks through grammar and completes orthodox oral practice
- 死锁的产生条件和预防处理[通俗易懂]
- How to choose fund products? What fund is suitable to buy in July 2022?
- The latest version of codesonar has improved functional security and supports Misra, c++ parsing and visualization
- 华泰证券可以做到万一佣金吗,万一开户安全嘛
- gridView自己定义做时间排版「建议收藏」
- Make this crmeb single merchant wechat mall system popular, so easy to use!
- Is it safe to open an account of BOC shares in kainiu in 2022?
- 恶魔奶爸 B2 突破语法,完成正统口语练习
猜你喜欢

Magic weapon - sensitive file discovery tool

How to meet the dual needs of security and confidentiality of medical devices?

Tensorflow2. How to run under x 1 Code of X

使用高斯Redis实现二级索引

Ubuntu安装mysql8遇到的问题以及详细安装过程

Tensorflow2.x下如何运行1.x的代码

AADL inspector fault tree safety analysis module

Demon daddy B3 read extensively in a small amount, and completed 20000 vocabulary+

恶魔奶爸 B3 少量泛读,完成两万词汇量+
MySQL storage expression error
随机推荐
阿里云有奖体验:如何通过ECS挂载NAS文件系统
使用枚举实现英文转盲文
Make this crmeb single merchant wechat mall system popular, so easy to use!
I wrote a markdown command line gadget, hoping to improve the efficiency of sending documents by garden friends!
目标:不排斥 yaml 语法。争取快速上手
[matrix multiplication] [noi 2012] [cogs963] random number generator
Object-C programming tips timer "suggestions collection"
Codesonar enhances software reliability through innovative static analysis
EasyUI date control emptying value
Implement secondary index with Gaussian redis
部署、收回和删除解决方式—-STSADM和PowerShell「建议收藏」
UVA 11080 – Place the Guards(二分图判定)
刚开户的能买什么股票呢?炒股账户安全吗
Phoenix JDBC
Micro service remote debug, nocalhost + rainbow micro service development second bullet
Codeforces 474 F. Ant colony
Intelligent transportation is full of vitality. What will happen in the future? [easy to understand]
Lex & yacc of Pisa proxy SQL parsing
How to meet the dual needs of security and confidentiality of medical devices?
恶魔奶爸 B2 突破语法,完成正统口语练习