- The Android NDK is not a single tool; it is a comprehensive set of APIs, cross-compilers, linkers, debuggers, build tools, documentation, and sample applications. The following are some of the key components of Android NDK:
- ARM, x86, and MIPS cross-compilers
- Build system
- Java Native Interface headers
- C library
- Math library
- POSIX threads
- Minimal C++ library
- ZLib compression library
- Dynamic linker library
- Android logging library
- Android pixel buffer library
- Android native application APIs
- OpenGL ES 3D graphics library
- OpenSL ES native audio library
- OpenMAX AL minimal support
- Build system
-
The primary goal of this build system is to allow developers to only write very short build files to describe their native Android applications; the build system handles many details including the toolchain, platform, CPU,and ABI specifics on behalf of the developer。
-
the Android NDK build system relies on two other files that are expected to be provided by
the developer as a part of the NDK project:
Android.mk and
Application.mk
- Android.mk---a makefile用來將本NDK 工程「描述」到Android NDK build system。該文件是由developer提供的,放在工程的jni目錄下,Android NDK build system只會從該目錄下尋找該文件。
- build a shared library
#let android build system to locate the source file;
#call my-dir是經過調用MACRO來動態獲取路徑,而不是經過寫死(hard-coded)
LOCAL_PATH
:
= $(call my
-dir)
#刪除文件,避免衝突
include $(CLEAR_VARS)
#指定生成module的名字,如hello-jni.so
LOCAL_MODULE
:
= hello
-jni
#指定要生成module而須要進行build的源文件
#這裏只須要一個源文件,當須要多個源文件時,各個文件之間用空格隔開
LOCAL_SRC_FILES
:
= hello
-jni.c
#UILD_SHARED_LIBRARYvariable is set by the Android NDK build system to the location of build-shared-library.mkfile
include $(BUILD_SHARED_LIBRARY)
- build multiple shared library
LOCAL_PATH
:
= $(call my
-dir)
#
# Module 1
#
include $(CLEAR_VARS)
LOCAL_MODULE
:
= module1
LOCAL_SRC_FILES
:
= module1.c
include $(BUILD_SHARED_LIBRARY)
#
# Module 2
#
include $(CLEAR_VARS)
LOCAL_MODULE
:
= module2
LOCAL_SRC_FILES
:
= module2.c
include $(BUILD_SHARED_LIBRARY)
-
build static library--->Static libraries can be used to build shared libraries. For example, when integrating third party code into an existing native project, instead of including the source code directly, the third party code can be compiled as a static library and then combined into the shared library.
LOCAL_PATH
:
= $(call my
-dir)
#
# the 3rd party AVI library
#
include $(CLEAR_VARS)
LOCAL_MODULE
:
= avilib
LOCAL_SRC_FILES
:
= avilib.c platform_posix.c
include $(BUILD_STATIC_LIBRARY)
#
# Native module
#
include $(CLEAR_VARS)
LOCAL_MODULE
:
= module
LOCAL_SRC_FILES
:
= module.c
LOCAL_STATIC_LIBRARIES
:
= avilib
include $(BUILD_SHARED_LIBRARY)
-
- Application.mk--->一個可選的makefile,位於jni目錄下,用來描述 一個application須要使用哪些modules
- 是
nkd-build script--->位於android ndk安裝路徑下,但執行時要切換到所要編譯的project根目錄下。該腳本經過傳入一些參數來控制盒維護編譯過程。
- 經常使用參數或命令
- -C :默認狀況下,ndk-build script要在project根目錄下執行,但能夠經過使用「-C」選項,顯示地指定NDK project的路徑
- -B :unconditionally make all targets 無條件地編譯全部目標,不管是否有源文件被修改
- -j :nkd-build是調用make進行build,默認狀況下make一次只執行一條命令,在帶-j選項時,make能夠並行處理多條指令(應該是利用了cpu多核的特性吧)
- ndk-build clean : 刪除build過程當中生成的二進制文件。
-