android 調用c++程序 並使用locat工具輸出

hellojni.c
#include <string.h>
#include <jni.h>

//#include <utils/Log.h>
#include <stdio.h>
<span style="color:#ff0000;">#include<android/log.h></span>

<span style="color:#ff0000;">#define LOG_TAG "debug"
#define LOGI(fmt, args...) __android_log_print(ANDROID_LOG_INFO, LOG_TAG, fmt, ##args)
#define LOGD(fmt, args...) __android_log_print(ANDROID_LOG_DEBUG, LOG_TAG, fmt, ##args)
#define LOGE(fmt, args...) __android_log_print(ANDROID_LOG_ERROR, LOG_TAG, fmt, ##args)</span>

/* This is a trivial JNI example where we use a native method
 * to return a new VM String. See the corresponding Java source
 * file located at:
 *
 *   apps/samples/hello-jni/project/src/com/example/HelloJni/HelloJni.java
 */
jstring
Java_com_example_hellojni_HelloJni_stringFromJNI( JNIEnv* env,
                                                  jobject thiz )
{
	printf("printf from c++\n");
	LOGI("test log!!!!");
    return (*env)->NewStringUTF(env, "Hello from JNI !");
}
Android.mk文件
<pre name="code" class="plain">LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
<span style="color:#ff0000;">#logcat local lib 下面兩句都可以
LOCAL_LDLIBS += -L$(SYSROOT)/usr/lib -llog
#LOCAL_LDLIBS += -lc -lm -llog</span>

LOCAL_MODULE    := hellojni
LOCAL_SRC_FILES := hellojni.c
include $(BUILD_SHARED_LIBRARY)

添加了紅色部分之後就可使用logcat工具輸出

<p style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;"><span style="font-size: 18px;">打印語句</span></p><p style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;"><span style="font-size: 18px;">LOGI("test log!!!!")</span></p><p style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;"><span style="font-size: 18px;">LOGI("the string is: %s \n",buff);</span></p><p style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;">
</p><p style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;">
</p><p style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;"></p><p style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;"><span style="font-size: 18px;">錯誤輸出到日誌</span></p><p style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;"><span style="font-size: 18px;">LOGI(strerror(errno))</span></p>

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章