当前位置:网站首页>NDK learning notes (I)
NDK learning notes (I)
2022-06-11 05:33:00 【Come and go】
List of articles
- 1.java The relationship between method and native instance method parameters .
- 2.java And c/c++ Basic data type relationships for
- 3.java The relationship between application type and native
- 4. String related operations
- 5. Array operation
- 6.NIO operation
- 7. access domains
- 8. Calling method
- 9. Domain and method descriptors
- 10. Use in native code log
1.java The relationship between method and native instance method parameters .
java The instance method corresponds to jobject,java Static method correspondence jclass.
public static native String stringFromJNI();
extern "C"
JNIEXPORT jstring JNICALL
Java_com_example_testndk_JniUtil_stringFromJNI(JNIEnv *env, jclass thiz){
}
public native String stringFromJNI();
extern "C"
JNIEXPORT jstring JNICALL
Java_com_example_testndk_JniUtil_stringFromJNI(JNIEnv *env, jobject thiz) {
}
2.java And c/c++ Basic data type relationships for

3.java The relationship between application type and native

4. String related operations
hold java The string is converted to C character string
//JNI_TRUE The returned string address points to the copy
//JNI_FALSE The returned string address points to a fixed object in the heap
jboolean bo = JNI_FALSE;
// Create string
jstring ss = env->NewStringUTF("hello world");
// take jstring Convert to an operable string
const char *str = env->GetStringUTFChars(ss, &bo);
if (str != 0) {
printf("111111111111111%s", str);
}
Release string ( Unlike java There's a garbage collector , Application requires release )
env->ReleaseStringUTFChars(ss,str);
5. Array operation
(1) Create array
New<Type>Array
Type: Corresponding Int、Char、Bollean etc.
Using examples :
jintArray array = env->NewIntArray(10);
(2) Access array ( copy )
// Convert array
jint nativeARRAY[5];
env->GetIntArrayRegion(array, 0, 5, nativeARRAY);
// Operation array , from C Array orientation java Array commit changes
nativeARRAY[0] = 100;
env->SetIntArrayRegion(array, 0, 5, nativeARRAY);
java
int[] array = {
10, 11, 12, 13, 14};
arrayOperation(array);
for (int i = 0; i < 5; i++) {
Log.i("mainActivity", array[i] + "");
}
Output results
2020-03-14 17:35:12.173 18150-18150/com.example.testndk I/mainActivity: 100
2020-03-14 17:35:12.173 18150-18150/com.example.testndk I/mainActivity: 11
2020-03-14 17:35:12.173 18150-18150/com.example.testndk I/mainActivity: 12
2020-03-14 17:35:12.173 18150-18150/com.example.testndk I/mainActivity: 13
2020-03-14 17:35:12.175 18150-18150/com.example.testndk I/mainActivity: 14
(3) Access array ( Operation on direct pointer )
// Get a direct pointer
jint *myInt = env->GetIntArrayElements(array, JNI_FALSE);
// Operation array , Commit changes and free memory
myInt[0] = 1555;
env->ReleaseIntArrayElements(array, myInt, 0);
java
int[] array = {
10, 11, 12, 13, 14};
arrayOperation(array);
for (int i = 0; i < 5; i++) {
Log.i("mainActivity", array[i] + "");
}
Output results
2020-03-14 20:45:50.064 19088-19088/com.example.testndk I/mainActivity: 1555
2020-03-14 20:45:50.065 19088-19088/com.example.testndk I/mainActivity: 11
2020-03-14 20:45:50.065 19088-19088/com.example.testndk I/mainActivity: 12
2020-03-14 20:45:50.065 19088-19088/com.example.testndk I/mainActivity: 13
2020-03-14 20:45:50.065 19088-19088/com.example.testndk I/mainActivity: 14
ReleaseIntArrayElements The third parameter mode explain .
6.NIO operation
// Create a direct byte buffer
unsigned char *buffer = (unsigned char *) malloc(1024);
jobject objBuffer = env->NewDirectByteBuffer(buffer, 1024);
// Get buffer
unsigned char *buffer2 = (unsigned char *)env->GetDirectBufferAddress(objBuffer);
7. access domains
extern "C" JNIEXPORT void JNICALL
Java_com_example_testndk_MainActivity_fieldOperation(
JNIEnv *env,
jobject /* this */, jobject obj) {
// Instance domain
// Get domain id,Ljava/lang/String;
jclass cls = env->GetObjectClass(obj);
jfieldID fId = env->GetFieldID(cls, "size", "Ljava/lang/String;");
// Get domain
jstring str = (jstring) env->GetObjectField(obj, fId);
const char *c = env->GetStringUTFChars(str, JNI_FALSE);
LOGI("%s", c);
// Static domain
// Get domain id
jfieldID fId2 = env->GetStaticFieldID(cls, "staticSize", "Ljava/lang/String;");
// Get domain
jstring str2 = (jstring) env->GetStaticObjectField(cls, fId2);
const char *c2 = env->GetStringUTFChars(str2, JNI_FALSE);
LOGI("%s", c2);
}
java
public class MyBox {
private String size = "instance";
private static String staticSize = "static";
}
MyBox myBox = new MyBox();
fieldOperation(myBox);
Output results
2020-03-14 23:53:00.647 21624-21624/com.example.testndk I/test-jni: instance
2020-03-14 23:53:00.647 21624-21624/com.example.testndk I/test-jni: static
8. Calling method
extern "C" JNIEXPORT void JNICALL
Java_com_example_testndk_MainActivity_methodOperation(
JNIEnv *env,
jobject /* this */, jobject obj) {
// obtain class
jclass cls = env->GetObjectClass(obj);
// Get instance method id
jmethodID mId = env->GetMethodID(cls, "instanceMethod", "()Ljava/lang/String;");
// Invoking an instance method
jstring result = (jstring) env->CallObjectMethod(obj, mId);
const char *c = env->GetStringUTFChars(result, JNI_FALSE);
LOGI("%s",c);
// Get static methods id
jmethodID mId2 = env->GetStaticMethodID(cls, "staticMethod", "()Ljava/lang/String;");
// Call static methods
jstring result2 = (jstring) env->CallStaticObjectMethod(cls, mId2);
const char *c2 = env->GetStringUTFChars(result2, JNI_FALSE);
LOGI("%s",c2);
}
java
public class MyBox {
private String instanceMethod() {
return "instance";
}
private static String staticMethod() {
return "static";
}
}
MyBox myBox = new MyBox();
methodOperation(myBox);
Output content
2020-03-15 13:50:54.937 21991-21991/com.example.testndk I/test-jni: instance
2020-03-15 13:50:54.937 21991-21991/com.example.testndk I/test-jni: static
9. Domain and method descriptors
This type of data is a descriptor
()Ljava/lang/String;
Ljava/lang/String;
android studio Descriptor configuration for , Makes it easier to get descriptors .
file→setting→tools→external tools
Click on + Number ,name at will , Others are as follows :
program:$JDKPath$/bin/javap
arguments:-s -p $FileClass$
working directory:$ModuleFileDir$\build\intermediates\javac\debug\classes
Click on ok, complete .
Use : Click menu tools→external tools→ Your name
effect :
D:\Android_develop\android_studio\jre/bin/javap -s -p com.example.testndk.MyBox
Compiled from "MyBox.java"
public class com.example.testndk.MyBox {
public com.example.testndk.MyBox();
descriptor: ()V
private java.lang.String instanceMethod();
descriptor: ()Ljava/lang/String;
private static java.lang.String staticMethod();
descriptor: ()Ljava/lang/String;
}
Process finished with exit code 0
10. Use in native code log
#include <android/log.h>
#define LOG "test-jni" // This is a custom LOG The logo of
#define LOGD(...) __android_log_print(ANDROID_LOG_DEBUG,LOG,__VA_ARGS__) // Definition LOGD type
#define LOGI(...) __android_log_print(ANDROID_LOG_INFO,LOG,__VA_ARGS__) // Definition LOGI type
#define LOGW(...) __android_log_print(ANDROID_LOG_WARN,LOG,__VA_ARGS__) // Definition LOGW type
#define LOGE(...) __android_log_print(ANDROID_LOG_ERROR,LOG,__VA_ARGS__) // Definition LOGE type
#define LOGF(...) __android_log_print(ANDROID_LOG_FATAL,LOG,__VA_ARGS__) // Definition LOGF type
Use
LOGI("%s",c);
边栏推荐
- getBackgroundAudioManager控制音乐播放(类名的动态绑定)
- 1. use alicloud object OSS (basic)
- Section II: structural composition characteristics of asphalt pavement (1) structural composition
- [project - what are the supporting materials in the annexes? (18 kinds of 2000 word summary)] project plan of innovation and entrepreneurship competition and supporting materials of Challenge Cup entr
- 自定义View之基础篇
- Common methods of tool class objectutil
- Opencv learning path (2-5) -- Deep parsing imwrite function
- Start the project using the locally configured gradle
- Paper recommendation: relicv2, can the new self supervised learning surpass supervised learning on RESNET?
- Section IV: composition and materials of asphalt mixture (1) -- structure composition and classification
猜你喜欢

6 questions to ask when selecting a digital asset custodian

Combien de courant le câblage des PCB peut - il supporter?

getBackgroundAudioManager控制音乐播放(类名的动态绑定)

22、生成括号

Bert knowledge distillation

Customize the layout of view Foundation

袋鼠雲數棧基於CBO在Spark SQL優化上的探索

PCB走線到底能承載多大電流

Es IK installation error

Topological sorting
随机推荐
wxParse解析iframe播放视频
BERT知识蒸馏
The programmers of a large factory after 95 were dissatisfied with the department leaders, and were sentenced for deleting the database and running away
Share | defend against physically realizable image classification attacks
PCB走线到底能承载多大电流
推荐一款免费的内网穿透开源软件,可以在测试本地开发微信公众号使用
Dongmingzhu said that "Gree mobile phones are no worse than apple". Where is the confidence?
工具类ObjectUtil常用的方法
Deep search + backtracking
Overview of self attention acceleration methods: Issa, CCNET, cgnl, linformer
Introduction to coordinate system in navigation system
SwiftUI: Navigation all know
Conversion relationship between coordinate systems (ECEF, LLA, ENU)
Click the icon is not sensitive how to adjust?
Manually splicing dynamic JSON strings
Recherche sur l'optimisation de Spark SQL basée sur CBO pour kangourou Cloud Stack
Intercept file extension
Vins fusion GPS fusion part
QT Road (2) -- HelloWorld
Some details about memory