JNI 编译方法
Android下编译jni库的二种方法
1. 使用Android源码中的Make系统
device/mstar/common/apps/jni
├── Android.mk
├── Application.mk
├── SerialPort.c
└── SerialPort.h
make例子:
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS) -->用于重置除LOCAL_PATH变量外的,所有LOCAL_XXX系列变量。
TARGET_PLATFORM := android-3
LOCAL_MODULE := serial_port
LOCAL_SRC_FILES := SerialPort.c
#LOCAL_PRELINK_MODULE := false
LOCAL_C_INCLUDES :=$(LOCAL_PATH)
LOCAL_LDLIBS +=-L$(SYSROOT)/usr/lib -llog
LOCAL_SHARED_LIBRARIES :=libcutils
include $(BUILD_SHARED_LIBRARY) -->指定编译出的是动态库
SerialPort.c :
#include <termios.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <string.h>
#include <jni.h>
#include "SerialPort.h"
#include <cutils/log.h> // for Android Make system: $Android_SRC/system/core/include/cutils/log.h
//#include <android/log.h> // for NDK compiler
//#include "android/log.h"
static const char *TAG="serial_port";
#define LOGI(fmt, args...) __android_log_print(ANDROID_LOG_INFO, TAG, fmt, ##args)
#define LOGD(fmt, args...) __android_log_print(ANDROID_LOG_DEBUG, TAG, fmt, ##args)
#define LOGE(fmt, args...) __android_log_print(ANDROID_LOG_ERROR, TAG, fmt, ##args)
2. 使用NDK(从NDK r5开始)
1. 下载 linux 版本NDK
2. $vi ~/.bashrc
#set NDK env
export NDK_HOME=~/tools/ndk/android-ndk-r9
export PATH=$PATH:$NDK_HOME
3. 测试 sample中的hell-jni 例子
$ ndk-build