about 5 years ago

In Android JNI programming, it can also use Log function like android.util.Log

There are two main steps to reach propose.



1. Modify Android.mk
Android.mk
# Copyright (C) 2009 The Android Open Source Project
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
LOCAL_PATH := $(call my-dir)

include $(CLEAR_VARS)

LOCAL_MODULE    := libITRI
LOCAL_SRC_FILES := libitri.c

LOCAL_LDLIBS := -L$(SYSROOT)/usr/lib -llog

include $(BUILD_SHARED_LIBRARY)
  • add LOCAL_LDLIBS := -L$(SYSROOT)/usr/lib -llog

2. Define Log Function in Your JNI Program

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

#include <android/log.h>

#define LOG_TAG "wii.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)

jint
Java_your_package_name_JNIClassName_Function1(JNIEnv* env, jobject thiz)
{
        LOGE("[***DVB JNI***] Hellow Wii");
        return 0;
}
  • add #include<android/log.h>
  • define Log type, like Log.d, Log.e, etc.
    • #define LOGI(fmt, args...) _android_log_print(ANDROID_LOG_INFO, LOGTAG, fmt, ##args)
    • #define LOGD(fmt, args...) _android_log_print(ANDROID_LOG_DEBUG, LOGTAG, fmt, ##args)
    • #define LOGE(fmt, args...) _android_log_print(ANDROID_LOG_ERROR, LOGTAG, fmt, ##args)

If you'd define Log type, you can use Log function easily just like sample program.

← Ubuntu 13.04 Set Up VPN Server Android Studio Git Tutorial (Part 1) →
 
comments powered by Disqus