1.封装个log.h
#ifndef __MULTI_TRACE_H__
#define __MULTI_TRACE_H__
#ifdef ANDROID_NDK_BUILD
#define LOG_TAG "NATIVE NDK INFO"
#include <android/log.h> // only use in NDK
#define LOGE(...) ((void)__android_log_print(ANDROID_LOG_ERROR, LOG_TAG, __VA_ARGS__))
#define LOGD(...) ((void)__android_log_print(ANDROID_LOG_DEBUG, LOG_TAG, __VA_ARGS__))
#define LOGI(...) ((void)__android_log_print(ANDROID_LOG_INFO, LOG_TAG, __VA_ARGS__))
#define LOGW(...) ((void)__android_log_print(ANDROID_LOG_WARN, LOG_TAG, __VA_ARGS__))
#elif defined ANDROID_SOURCE_BUILD
#define LOG_TAG "NATIVE SRC INFO"
extern "C" {
#include <cutils/log.h> // only use in ANDROID SOURCE
}
#define LOGE(...) LOG(LOG_ERROR, LOG_TAG, __VA_ARGS__)
#define LOGD(...) LOG(LOG_DEBUG, LOG_TAG, __VA_ARGS__)
#define LOGI(...) LOG(LOG_INFO, LOG_TAG, __VA_ARGS__)
#define LOGW(...) LOG(LOG_WARN, LOG_TAG, __VA_ARGS__)
#elif defined IOS_PLATFORM_BUILD
#define LOGE printf
#define LOGD printf
#define LOGI printf
#define LOGW printf
#elif defined WP_PLATFORM_BUILD
#define LOG_TAG "NATIVE WINDOWS PHONE"
#define LOG_ERROR "ERROR"
#define LOG_DEBUG "DEBUG"
#define LOG_INFOR "INFOR"
#define LOG_WARN "WARN"
void LOG(const char* status, const char* tag, const char* format, ...);
#define LOGE(...) LOG(LOG_ERROR, LOG_TAG, __VA_ARGS__)
#define LOGD(...) LOG(LOG_DEBUG, LOG_TAG, __VA_ARGS__)
#define LOGI(...) LOG(LOG_INFO, LOG_TAG, __VA_ARGS__)
#define LOGW(...) LOG(LOG_WARN, LOG_TAG, __VA_ARGS__)
#else
#define LOGE printf
#define LOGD printf
#define LOGI printf
#define LOGW printf
#endif
#endif
2.在 Android.mk 中配置
LOCAL_PATH := $(call my-dir) // 设置为当前目录
include $(CLEAR_VARS) // 清空变量
LOCAL_MODULE := ApkPatchLibrary // 最终生成的 .so 文件 会是次名钱夹 lib (如这个名为 libxxx, 生成的.so文件包名就是此名)
LOCAL_CFLAGS = -DANDROID_NDK_BUILD -D__STDC_FORMAT_MACROS -D__STDC_INT64__ // 设置这句相当于每个c/c++文件中声明了 xxxxx(-Dxxxxx)
LOCAL_CXXFLAGS :=
LOCAL_C_INCLUDES := $(LOCAL_PATH)
LOCAL_SRC_FILES := com_lib_utils_Patch.c // 编译的c/c++文件
LOCAL_LDLIBS := -lz -llog // 需要加上 这个日志才会打印出来,-l 是固定声明,(如:-lz标示加载系统 z.so包, -llog标示加载系统 log.so包)
include $(BUILD_SHARED_LIBRARY) // 设置声明 动态库
3.打印日志,请看例子:
JNIEXPORT jint JNICALL Java_com_lib_utils_PatchUtils_patch(JNIEnv *env,
jobject obj, jstring old, jstring new, jstring patch) {
char * ch[4];
ch[0] = "bspatch";
ch[1] = (char*) ((*env)->GetStringUTFChars(env, old, 0));
ch[2] = (char*) ((*env)->GetStringUTFChars(env, new, 0));
ch[3] = (char*) ((*env)->GetStringUTFChars(env, patch, 0));
LOGD("11111111111111111111");
const char *str = (*env)->GetStringUTFChars(env, old, 0);
const char *strnew = (*env)->GetStringUTFChars(env, new, 0);
const char *strpatch = (*env)->GetStringUTFChars(env, patch, 0);
LOGD("The first is %s", str);
LOGD("The second is %s", strnew);
LOGD("The third is %s", strpatch);
LOGD("11111111111111111111");
int ret = applypatch(4, ch);
(*env)->ReleaseStringUTFChars(env, old, ch[1]);
(*env)->ReleaseStringUTFChars(env, new, ch[2]);
(*env)->ReleaseStringUTFChars(env, patch, ch[3]);
return ret;
}
这样就可以打印出 old, new,patch 这三个参数(变量)的值了。