Implement Android Studio JNI development C/C ++ use _ android_log_print to output Log and androidjnilog

Source: Internet
Author: User

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");}

 

 

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.