JNI learning 3.cpp code prints logs in android studio

To facilitate debugging, logs are usually printed in Java code. But in jni development, if the c++ function also needs to print logs for debugging, additional settings must be made.

1. Open build.gradle in the app directory and add the following code:

    ndk{
            ldLibs "log"
        }

 2. Add the following code to the cpp file:

#include <android/log.h>
#define LOG_TAG "CPPLOG"
#define LOGD(...) __android_log_print(ANDROID_LOG_DEBUG, LOG_TAG, __VA_ARGS__)
#define LOGI(...) __android_log_print(ANDROID_LOG_INFO, LOG_TAG, __VA_ARGS__)
#define LOGE(...) __android_log_print(ANDROID_LOG_ERROR, LOG_TAG, __VA_ARGS__)

It can be seen from the code that "CPPLOG" is the tag of the log, which can be customized and modified. When the code is running, search for CPPLOG in logcat to find the relevant logs.

LOGD, LOGI, and LOGE are three types of logs, which correspond to the java log prototype one by one.

3. Call in the jni function

4. Call the jni function in the Java program, and log the output result

Guess you like

Origin blog.csdn.net/m0_37872216/article/details/126155815