Implement Android Studio JNI development C/C ++ use _ android_log_print to output Log and androidjnilog
I believe that many people need to output logs when learning Android JNI programming at the beginning. When searching for Baidu and Google, they all say they need to use Android. add LOCAL_LDLIBS + =-L $ (SYSROOT)/usr/lib-llog to mk. In fact, this is the method of eclipse development, and Android Studio is not used in this way.
Android. mk of Android Studio is automatically generated, and even modification is useless. In actual Android Studio, Android. mk is generated based on the gradle file, so you need to modify the gradle file.
If gradle is not modified, an error is reported if _ android_log_print is used directly.
Error:(36) undefined reference to '__android_log_print'
Now you only need to add some code in build. gradle in the jni Module to implement the output Log.
Build. gradle file completion code
Apply plugin: 'com. android. library 'android {compileSdkVersion 21 buildToolsVersion "21.1.2" defaultConfig {minSdkVersion 14 targetSdkVersion 21 versionCode 1 versionName "1.0"} buildTypes {release false proguardFiles done'), 'proguard-rules. pro 'ndk {moduleName "moduleName"LdLibs "log" // implement _ android_log_printAbiFilters "armeabi", "armeabi-v7a", "x86" }}productflavors {}} dependencies {compile fileTree (dir: 'libs', include :['*. jar ']) compile 'com. android. support: support-v13: 21.0.3 '}
LdLibs "log" is the key code
#include <string.h>#include <android/log.h>#include <jni.h>#include <stdio.h>#define LOG_TAG "native-dev"#define LOGI(...) __android_log_print(ANDROID_LOG_INFO, LOG_TAG, __VA_ARGS__)#define LOGE(...) __android_log_print(ANDROID_LOG_ERROR, LOG_TAG, __VA_ARGS__)#define LOGI(...) __android_log_print(ANDROID_LOG_INFO, LOG_TAG, __VA_ARGS__)void testlog(){ LOGI("test");}