当前位置:网站首页>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);
边栏推荐
- 使用acme.sh自动申请免费SSL证书
- If the MAC fails to connect with MySQL, it will start and report an error
- How much current can PCB wiring carry
- WinForm (I) introduction to WinForm and use of basic controls
- Topological sorting
- Preliminary understanding of DFS and BFS
- Opencv learning path (2-2) -- Deep parsing namedwindow function
- Poverty has nothing to do with suffering
- [opencv learning problems] 1 Namedwindow() and imshow() show two windows in the picture
- Click the icon is not sensitive how to adjust?
猜你喜欢

Wechat applet text built-in component newline character does not newline reason

Es IK installation error

Why is the smart door lock so popular? What about the smart door locks of MI family and zhiting?

Bert knowledge distillation

Customize the layout of view Foundation

Cocoapods installation error

智能门锁为什么会这么火,米家和智汀的智能门锁怎么样呢?

Exploration of kangaroo cloud data stack on spark SQL optimization based on CBO

Zed2 camera calibration -- binocular, IMU, joint calibration

NVIDIA SMI has failed because it could't communicate with the NVIDIA driver
随机推荐
27、移除元素
Intercept file extension
Oh my Zsh correct installation posture
Wechat applet uploads the data obtained from database 1 to database 2
Solving graph problems with union search set
GAMES101作业7-Path Tracing实现过程&代码详细解读
Big meal count (time complexity) -- leetcode daily question
Multithreading tutorial (XXV) atomic array
Project - Smart City
Section I: classification and classification of urban roads
MySQL string to array, merge result set, and convert to array
Traversal of binary tree -- restoring binary tree by two different Traversals
在未来,机器人或 AI 还有多久才能具备人类的创造力?
Basics of customized view
Preliminary test of running vins-fusion with zed2 binocular camera
[opencv learning problems] 1 Namedwindow() and imshow() show two windows in the picture
lower_ Personal understanding of bound function
Combien de courant le câblage des PCB peut - il supporter?
6 questions to ask when selecting a digital asset custodian
Redis setup (sentinel mode)