Eclipse NDK环境搭建

Eclipse NDK环境搭建

前言

    使用最新NDK,直接抛弃cygwin,以前做Android的项目要用到NDK就必须要下载安装Cygwin(模拟Linux环境用的),下载CDT(Eclipse C/C++开发插件),下载NDK,还要配置编译器,环境变量,特别麻烦,新版就不需要了。


工具

    NDK,官方下载地址由于墙的原因不好打开,就在http://www.androiddevtools.cn/下载


配置

1.系统NDK环境变量配置

Eclipse NDK环境搭建


2.Eclipse配置NDK

Eclipse NDK环境搭建


3.Eclipse配置javah工具自动生成jni头文件

Eclipse NDK环境搭建

#工作空间
${project_loc}
 
#工具执行参数
-v -classpath ${project_loc}/bin/classes -d ${project_loc}/jni -jni ${java_type_name}

使用时选中包含native方法的java文件,运行javah,即可在jni目录中生成对应的jni头文件


4.新建带有jni工程

新建一个android工程,右键添加native支持,将会在工程目录中生成Android.mk文件和cpp文件

Eclipse NDK环境搭建


5.配置C、CPP头文件及ndk-build工具

右键工程--->Properties

Eclipse NDK环境搭建


添加环境变量NDKROOT,值为NDK的根目录

Eclipse NDK环境搭建


Eclipse NDK环境搭建


6.LOG信息打印

若有源码环境,拷贝system/core/inclue/cutils目录拷贝到{ndk-dir}/platforms/android-xx/arch-arm/usr/include

拷贝out/target/product/tcc893x/system/lib/libcutils.so、libutils.so到{ndk-dir}/platforms/android-xx/arch-arm/usr/lib

Android.mk中添加

LOCAL_SHARED_LIBRARIES:= libutils libcutils 
LOCAL_LDLIBS    := -llog

然后在jni中添加头文件cutils/log.h,就可以使用ALOGE/ALOGD/ALOGD


若没有就自己定义吧

#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)



以上就是关于Eclipse NDK的搭建,挺简单的,完成之后直接运行就会自动编译JNI原生代码。