使用NDK 开发C/C++ 项目需注意以下几点规则:
NDK 工具原目录规则;
在NDK 上自建目录及文件规则;
NDK 工具的Make File 规则;
NDK 工具的 编译规则;
1. NDK 工具的目录规则
apps # java 项目目录、 编译启动文件存放各项目目录中 、
# 最终编译结果将从out 目录中拷贝一份到apps 目录下的libs 目录中
build # 编译环境目录
docs # 文档目录
GNUmakefile # makefile 启始文件
out # 编译生成后存放目录
README.TXT # readme 说明
sources # C++ 项目目录
2. 自建目录及文件规则
apps/< 项目名> # Java 项目
sources/< 项目名> # C++ 项目
apps/< 项目名>/Application.mk # 启动编译项目列表 (必须有此文件)
apps/< 项目名>/default.properties # android 编译版本 (必须有此文件)
sources/< 项目名>/Android.mk # 此文件会寻找当前目录下所有子目录中的Android.mk 文件
sources/< 项目名>/< 子项目>/Android.mk # 编译子项目的makefile (必须有此文件)
3. NDK 工具的Make File 规则
创建文件 apps/ < 项目名> /default.properties
~/android-ndk-1.5_r1/apps/< 项目名> $ gvim default.properties
### apps/ < 项目名> /default.properties ###
# 这里指定使用 android-3 是指 android sdk 1.5 版本
target=android-3
创建文件 apps/ < 项目名> /Application.mk
~/android-ndk-1.5_r1/apps/< 项目名> $ gvim Application.mk
### apps/ < 项目名> /Application.mk ###
APP_PROJECT_PATH := $(call my-dir)
#########################################################
# 子项目输出名列表
#########################################################
# 是每一个 sources/< 项目名>/ 的所有子项目中Android.mk 指定的输出名
APP_MODULES := < 子项目输出名列表>
建立sources/< 项目名> /Android.mk 文件
~/android-ndk-1.5_r1/sources/< 项目名> $ gvim Android.mk
### sources /project_test /Android.mk ###
# 寻找当前目录下的所有子目录中的Android.mk
include $(call all-subdir-makefiles)
建立sources/< 项目名>/< 子项目> /Android.mk 文件
~/android-ndk-1.5_r1/sources/< 项目名>/< 子项目> $ gvim Android.mk
### sources /< 项目名>/< 子项目> /Android.mk ###
#########################################################
# 进入当前目录并取得当前目录的路径
#########################################################
# 其结果为: ~/android-ndk-1.5_r1/sources/ < 项目名>/< 子项目>
LOCAL_PATH:= $(call my-dir)
#########################################################
# 清除NDK 全局变量
#########################################################
include $(CLEAR_VARS)
#########################################################
# 项目需要编译源文件列表
#########################################################
LOCAL_SRC_FILES:=
#########################################################
# 项目需要的.h 头文件目录列表
#########################################################
LOCAL_CFLAGS :=
#########################################################
# 项目需要的链接自编译出的.so 文件列表
#########################################################
LOCAL_SHARED_LIBRARIES :=
#########################################################
# 项目需要的链接非自编译出的.so 文件目录及列表
#########################################################
LOCAL_LDLIBS :=
#########################################################
# 在执行此Android.mk 文件时,显示指定的变量信息
#########################################################
# 如无需显示则可在之前加“# ”符号
# 此次则显示 LOCAL_SRC_FILE 变量内容在控制台上
$(info $(LOCAL_SRC_FILES))
#########################################################
# 子项目编译出输文件名
#########################################################
LOCAL_MODULE := < 子项目输出名>
#########################################################
# 项目编译出输引用文件名,编译成哪种文件,
#########################################################
# BUILD_EXECUTABLE 是指.bin 二进制执行文件
# BUILD_SHARED_LIBRARY 是指.so 动态库文件
# BUILD_STATIC_LIBRARY 是指.a 静态库文件
# 例如 输出二进制执行文件
# include $( BUILD_EXECUTABLE )
include $( < 项目编译出输文引用件名 > )
########## ######## ####################################################
# 注:上以所有*.mk 文件中的 “XXX := ”这样的变量赋值操作格式如下: #
# 每一个*.c/*.cpp 都使用空格分开,如果换行,则用 “/ ” #
# 例如同行 : LOCAL_SRC_FILES := a.c b.cpp #
# 例如换行 : LOCAL_SRC_FILES := a.c / #
# b.cpp #
######################################################################
4. NDK 工具的编译 规则
# 技巧:强制重新编译,则在最后加“ -B”,注意减号前有空格
<!-- /* Font Definitions */ @font-face {font-family:宋体; panose-1:2 1 6 0 3 1 1 1 1 1; mso-font-alt:SimSun; mso-font-charset:134; mso-generic-font-family:auto; mso-font-pitch:variable; mso-font-signature:3 135135232 16 0 262145 0;}@font-face {font-family:"/@宋体"; panose-1:2 1 6 0 3 1 1 1 1 1; mso-font-charset:134; mso-generic-font-family:auto; mso-font-pitch:variable; mso-font-signature:3 135135232 16 0 262145 0;} /* Style Definitions */ p.MsoNormal, li.MsoNormal, div.MsoNormal {mso-style-parent:""; margin:0cm; margin-bottom:.0001pt; text-align:justify; text-justify:inter-ideograph; mso-pagination:none; font-size:10.5pt; mso-bidi-font-size:12.0pt; font-family:"Times New Roman"; mso-fareast-font-family:宋体; mso-font-kerning:1.0pt;} /* Page Definitions */ @page {mso-page-border-surround-header:no; mso-page-border-surround-footer:no;}@page Section1 {size:612.0pt 792.0pt; margin:72.0pt 90.0pt 72.0pt 90.0pt; mso-header-margin:36.0pt; mso-footer-margin:36.0pt; mso-paper-source:0;}div.Section1 {page:Section1;}-->
~/android-ndk-1.5_r1 $ make APP=<项目名>
By Guilh