概述:
Android NDK是一系列工具的集合。帮助开发者快速开发C(或C++)的动态库,并能自动将so和java应用一起打包成apk。NDK集成了交叉编译器,并提供了相应的mk文件隔离平台、CPU、API等差异,开发人员只需要简单修改mk文件(指出“哪些文件需要编译”、“编译特性要求”等),就可以创建出so。NDK可以自动地将so和Java应用一起打包,极大地减轻了开发人员的打包工作。
android NDK下载地址: http://dl.google.com/android/ndk/android-ndk-r7-windows.zip
创建目录jni,比如F:\NDK-Build\jni,要编译的C/C++代码在该目录的外层(即F:\ClientSocketEngine)
jni目录下为mk文件
其中Android.mk文件如下
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)LOCAL_MODULE := u3dtools_shared
LOCAL_MODULE_FILENAME := libu3dtools# 配置自己的源文件目录和源文件后缀名
MY_FILES_PATH := $(LOCAL_PATH) \$(LOCAL_PATH)/../../ClientSocketEngineMY_FILES_SUFFIX := %.cpp %.c# 递归遍历目录下的所有的文件
rwildcard=$(wildcard $1$2) $(foreach d,$(wildcard $1*),$(call rwildcard,$d/,$2))# 获取相应的源文件
MY_ALL_FILES := $(foreach src_path,$(MY_FILES_PATH), $(call rwildcard,$(src_path),*.*) )
MY_ALL_FILES := $(MY_ALL_FILES:$(MY_CPP_PATH)/./%=$(MY_CPP_PATH)%)
MY_SRC_LIST := $(filter $(MY_FILES_SUFFIX),$(MY_ALL_FILES))
MY_SRC_LIST := $(MY_SRC_LIST:$(LOCAL_PATH)/%=%)# 去除字串的重复单词
define uniq =$(eval seen :=)$(foreach _,$1,$(if $(filter $_,${seen}),,$(eval seen += $_)))${seen}
endef# 递归遍历获取所有目录
MY_ALL_DIRS := $(dir $(foreach src_path,$(MY_FILES_PATH), $(call rwildcard,$(src_path),*/) ) )
MY_ALL_DIRS := $(call uniq,$(MY_ALL_DIRS))# 赋值给NDK编译系统
LOCAL_SRC_FILES := $(MY_SRC_LIST)
#LOCAL_C_INCLUDES := $(MY_ALL_DIRS)LOCAL_C_INCLUDES := $(LOCAL_PATH)/../../ClientSocketEngineinclude $(BUILD_SHARED_LIBRARY)
Application.mk文件如下
APP_STL := gnustl_static
APP_CPPFLAGS := -frtti -DCOCOS2D_DEBUG=1 -fexceptions
build_native.bat脚本如下
@set "NDK_ROOT=E:/InstallPack/android-ndk-r9d"@set "DIR=%cd%"
@REM ... use paths relative to current directory
@set "APP_ROOT=%DIR%/.."
@set "APP_ANDROID_ROOT=%DIR%"@echo NDK_ROOT = %NDK_ROOT%
@echo APP_ROOT = %APP_ROOT%
@echo APP_ANDROID_ROOT = %APP_ANDROID_ROOT%@echo Using prebuilt externals
@"%NDK_ROOT%/ndk-build.cmd"
pause
运行build_native.bat脚本,生成libu3dtools.so文件,搞定