Android development practice: Android. mk Template
There have been many articles on Android NDK development. My blog also shares many experiences and skills related to NDK development. Today I wrote a simple Android program. mk sample template for beginners.
This template provides examples of the following issues in Android NDK development:
1. How to automatically add the list of source files to be compiled
2. How to add third-party static library and dynamic library Dependencies
3. How to construct a complete NDK Engineering Framework
Suppose our project depends on libmath. a, libjson. a, libffmpeg. so these third-party library files, the project contains the following modules: algorithm, core, network, utils, tests, each module has several. c /. h file, we need to compile the entire project into a dynamic library, and provide the calling interface to the Java layer through the functions in native_sdk.c.
We can divide the jni directory of the Android project as follows:
Corresponding Android. an example of mk file writing is as follows (Note: The project file can be viewed online in my Github (@ Jhuster/Android) or downloaded in the final attachment of this blog ):
LOCAL_PATH:=$(callmy-dir)3RD_INC_DIR=$(LOCAL_PATH)/3rd/inc3RD_LIB_DIR=$(LOCAL_PATH)/3rd/libs#Prebuildthe3rdlibrariesinclude$(CLEAR_VARS)LOCAL_MODULE:=mathLOCAL_SRC_FILES:=$(3RD_LIB_DIR)/libmath.ainclude$(PREBUILT_STATIC_LIBRARY)include$(CLEAR_VARS)LOCAL_MODULE:=jsonLOCAL_SRC_FILES:=$(3RD_LIB_DIR)/libjson.ainclude$(PREBUILT_STATIC_LIBRARY)include$(CLEAR_VARS)LOCAL_MODULE:=ffmpegLOCAL_SRC_FILES:=$(3RD_LIB_DIR)/libffmpeg.soinclude$(PREBUILT_SHARED_LIBRARY)#Buildnativesdkinclude$(CLEAR_VARS)LOCAL_MODULE:=native_sdkLOCAL_SRC_FILES:=\$(subst$(LOCAL_PATH)/,,$(wildcard$(LOCAL_PATH)/src/algorithm/*.c))\$(subst$(LOCAL_PATH)/,,$(wildcard$(LOCAL_PATH)/src/core/*.c))\$(subst$(LOCAL_PATH)/,,$(wildcard$(LOCAL_PATH)/src/network/*.c))\$(subst$(LOCAL_PATH)/,,$(wildcard$(LOCAL_PATH)/src/utils/*.c))\$(subst$(LOCAL_PATH)/,,$(wildcard$(LOCAL_PATH)/src/*.c))LOCAL_C_INCLUDES:=$(3RD_INC_DIR)LOCAL_C_INCLUDES:=$(LOCAL_PATH)/srcLOCAL_C_INCLUDES:=$(LOCAL_PATH)/src/algorithmLOCAL_C_INCLUDES+=$(LOCAL_PATH)/src/coreLOCAL_C_INCLUDES+=$(LOCAL_PATH)/src/networkLOCAL_C_INCLUDES+=$(LOCAL_PATH)/src/utilsLOCAL_CFLAGS:=-DANDROIDLOCAL_LDLIBS:=-llogLOCAL_STATIC_LIBRARIES:=mathjsonLOCAL_SHARED_LIBRARIES:=ffmpeginclude$(BUILD_SHARED_LIBRARY)#Buildtestsinclude$(CLEAR_VARS)LOCAL_MODULE:=test.outLOCAL_SRC_FILES:=$(subst$(LOCAL_PATH)/,,$(wildcard$(LOCAL_PATH)/tests/*.c))LOCAL_CFLAGS:=-DANDROIDLOCAL_C_INCLUDES:=$(LOCAL_PATH)/srcLOCAL_LDLIBS:=-llog-fPIE-pieLOCAL_SHARED_LIBRARIES:=native_sdkinclude$(BUILD_EXECUTABLE)
For Android. if mk is to depend on a third-party library file, PREBUILT_XXXX_LIBRARY must be added for pre-compilation. The LOCAL_SRC_FILES macro provides the source file list to be compiled. Here I use the subst and wildcard functions, the system automatically scans the source files under the specified directory. Finally, it compiles executable programs that can run on the adb shell command line through BUILD_EXECUTABLE for code unit testing.