Android Makefile: Android.mk writing method

The makefile Android.mk of the android compilation system is written as follows

1.LOCAL_PATH

The Android.mk file first needs to specify the LOCAL_PATH variable to find the source file. Since Android.mk is generally in the same directory as the source file that needs to be compiled, it is defined as follows: The
LOCAL_PATH:=$(call my-dir)
above statement means to define the LOCAL_PATH variable as the path of the directory where the cost file (current Android.mk) is located.
It is stipulated that LOCAL_PATH must be placed before all include $(CLEAR_VARS)

2.CLEAR_VARS

Multiple compilation modules can be defined in Android.mk, and each compilation module starts with include $(CLEAR_VARS) and ends with include $(BUILD_XXX) .

include $(CLEAR_VARS)
...
include $(BUILD_XXX)

CLEAR_VARSProvided by the compilation system, it is the beginning of a compilation module. Specify to let GNUMAKEFILE clear all LOCAL_XXX variables except LOCAL_PATH for you, such as LOCAL_MODULE, LOCAL_SRC_FILES, LOCAL_SHARED_LIBRARIES, LOCAL_STATIC_LIBRARIES , etc.
BUILD_XXXdescribes the compilation target

include $(BUILD_STATIC_LIBRARY) means to compile into a static library on the target device (Appendix 1);
include $(BUILD_SHARED_LIBRARY) means to compile into a dynamic library on the target device (Appendix 2);
include $(BUILD_EXECUTABLE) means to compile into a target device Executable program (Appendix 3);
include $(BUILD_HOST_STATIC_LIBRARY) means compiled into a static library on the host ; include $
(BUILD_HOST_SHARED_LIBRARY) means compiled into a dynamic library on the host ;
include $(BUILD_HOST_EXECUTABLE) means compiled into an executable file on the host ;
include $(BUILD_JAVA_LIBRARY) means to compile into a JAVA library ;
include $(BUILD_STATIC_JAVA_LIBRARY) means to compile into a static JAVA library ;
include $(BUILD_HOST_JAVA_LIBRARY) means to compile into a JAVA library on the host ;
include $()BUILD_PACKAGE means to compile into an APK program

3. Examples

An example is as follows (frameworks/base/libs/audioflinger/Android.mk):

LOCAL_PATH:= $(call my-dir)

#-----------------------模块一---------------------
include $(CLEAR_VARS)                    #模块一
ifeq ($(AUDIO_POLICY_TEST),true)         #ifeq ($(CC),gcc)  表示判断表达式与某个值是否相等
    ENABLE_AUDIO_DUMP := true            #这里应该是定义一个标志,仅供当前.mk文件使用
endif

#LOCAL_SRC_FILES定义了本模块编译使用的源文件,采用的是基于LOCAL_PATH的相对路径
LOCAL_SRC_FILES:= \           
    AudioHardwareGeneric.cpp \
    AudioHardwareStub.cpp \
    AudioHardwareInterface.cpp

#下面这三句是我自己加的:    
LOCAL_CFLAGS := -Wall -Wextra -Werror   #gcc的编译选项 如:gcc -Wall -Wextra -Werror main.c -o main
#LOCAL_CFLAGS += -DXXX   相当于在  所有源文件  中增加一个宏定义#define XXX
#使用“+=”操作符给变量追加值
LOCAL_CFLAGS += -DSUNDP_FLAG
LOCAL_CFLAGS += -DSUNDPMY_FLAG

#ifeq ($(CC),gcc) 代表判断$(CC)变量是否是“gcc”
#下面这句就是判断ENABLE_AUDIO_DUMP是不是ture
ifeq ($(ENABLE_AUDIO_DUMP),true)
    LOCAL_SRC_FILES += AudioDumpInterface.cpp
    LOCAL_CFLAGS += -DENABLE_AUDIO_DUMP
endif

#表示编译本模块时需要链接的动态库
LOCAL_SHARED_LIBRARIES := \
    libcutils \
    libutils \
    libbinder \
    libmedia \
    libhardware_legacy
    
#strip
#名称:去空格函数——strip。 
#功能:去掉<string>字串中开头和结尾的空字符。 
#返回:返回被去掉空格的字符串值。
#示例: 
#$(strip a b c ) 
#把字串“a b c ”去到开头和结尾的空格,结果是“a b c”。
ifeq ($(strip $(BOARD_USES_GENERIC_AUDIO)),true)
    LOCAL_CFLAGS += -DGENERIC_AUDIO
endif

#LOCAL_MODULE:本模块的模块名,也就是说模块一被编译成libaudiointerface.so
LOCAL_MODULE:= libaudiointerface   
ifeq ($(BOARD_HAVE_BLUETOOTH),true)
    LOCAL_SRC_FILES += A2dpAudioInterface.cpp
    LOCAL_SHARED_LIBRARIES += liba2dp
    LOCAL_CFLAGS += -DWITH_BLUETOOTH -DWITH_A2DP
    #LOCAL_C_INCLUDES:本模块需要引用的include文件
    LOCAL_C_INCLUDES += $(call include-path-for, bluez)
endif
include $(BUILD_STATIC_LIBRARY)     #模块一编译成静态库

#-----------------------模块二---------------------
include $(CLEAR_VARS)    #模块二

LOCAL_SRC_FILES:=    \
    AudioPolicyManagerBase.cpp
LOCAL_SHARED_LIBRARIES := \
    libcutils \
    libutils \
    libmedia
    
ifeq ($(TARGET_SIMULATOR),true)
    LOCAL_LDLIBS += -ldl
else
    LOCAL_SHARED_LIBRARIES += libdl
endif

LOCAL_MODULE:= libaudiopolicybase
ifeq ($(BOARD_HAVE_BLUETOOTH),true)
    LOCAL_CFLAGS += -DWITH_A2DP
endif
ifeq ($(AUDIO_POLICY_TEST),true)
    LOCAL_CFLAGS += -DAUDIO_POLICY_TEST
endif
include $(BUILD_STATIC_LIBRARY)     #模块二编译成静态库

#-----------------------模块二---------------------
include $(CLEAR_VARS)           #模块三
LOCAL_SRC_FILES:=    \
    AudioFlinger.cpp     \
    AudioMixer.cpp.arm   \
    AudioResampler.cpp.arm   \
    AudioResamplerSinc.cpp.arm   \
    AudioResamplerCubic.cpp.arm \
    AudioPolicyService.cpp
LOCAL_SHARED_LIBRARIES := \
    libcutils \
    libutils \
    libbinder \
    libmedia \
    libhardware_legacy
ifeq ($(strip $(BOARD_USES_GENERIC_AUDIO)),true)
    LOCAL_STATIC_LIBRARIES += libaudiointerface libaudiopolicybase
    LOCAL_CFLAGS += -DGENERIC_AUDIO
else
    LOCAL_SHARED_LIBRARIES += libaudio libaudiopolicy
endif

ifeq ($(TARGET_SIMULATOR),true)
    LOCAL_LDLIBS += -ldl
else
    LOCAL_SHARED_LIBRARIES += libdl
endif

LOCAL_MODULE:= libaudioflinger
ifeq ($(BOARD_HAVE_BLUETOOTH),true)
    LOCAL_CFLAGS += -DWITH_BLUETOOTH -DWITH_A2DP
    LOCAL_SHARED_LIBRARIES += liba2dp
endif

ifeq ($(AUDIO_POLICY_TEST),true)
    LOCAL_CFLAGS += -DAUDIO_POLICY_TEST
endif

ifeq ($(TARGET_SIMULATOR),true)
    ifeq ($(HOST_OS),linux)
        LOCAL_LDLIBS += -lrt -lpthread
    endif
endif
ifeq ($(BOARD_USE_LVMX),true)
    LOCAL_CFLAGS += -DLVMX
    LOCAL_C_INCLUDES += vendor/nxp
    LOCAL_STATIC_LIBRARIES += liblifevibes
    LOCAL_SHARED_LIBRARIES += liblvmxservice
#   LOCAL_SHARED_LIBRARIES += liblvmxipc
endif
include $(BUILD_SHARED_LIBRARY) #模块三编译成动态库

4. Compile an application (APK)

LOCAL_PATH := $(call my-dir)
  include $(CLEAR_VARS)
   
  # Build all java files in the java subdirectory
  LOCAL_SRC_FILES := $(call all-subdir-java-files)
   
  # Name of the APK to build
  LOCAL_PACKAGE_NAME := LocalPackage
   
  # Tell it to build an APK
  include $(BUILD_PACKAGE)

5. Compile an application that depends on a static Java library (static.jar)

LOCAL_PATH := $(call my-dir)
  include $(CLEAR_VARS)
   
  # List of static libraries to include in the package
  LOCAL_STATIC_JAVA_LIBRARIES := static-library
   
  # Build all java files in the java subdirectory
  LOCAL_SRC_FILES := $(call all-subdir-java-files)
   
  # Name of the APK to build
  LOCAL_PACKAGE_NAME := LocalPackage
   
  # Tell it to build an APK
  include $(BUILD_PACKAGE)

6. Compile an application that needs to be signed with the platform key

LOCAL_PATH := $(call my-dir)
  include $(CLEAR_VARS)
   
  # Build all java files in the java subdirectory
  LOCAL_SRC_FILES := $(call all-subdir-java-files)
   
  # Name of the APK to build
  LOCAL_PACKAGE_NAME := LocalPackage
   
  LOCAL_CERTIFICATE := platform
   
  # Tell it to build an APK
  include $(BUILD_PACKAGE)

7. Compile an application that requires a specific key

LOCAL_PATH := $(call my-dir)
  include $(CLEAR_VARS)
   
  # Build all java files in the java subdirectory
  LOCAL_SRC_FILES := $(call all-subdir-java-files)
   
  # Name of the APK to build
  LOCAL_PACKAGE_NAME := LocalPackage
   
  LOCAL_CERTIFICATE := vendor/example/certs/app
   
  # Tell it to build an APK
  include $(BUILD_PACKAGE)

8. Add a precompiled application

LOCAL_PATH := $(call my-dir)
  include $(CLEAR_VARS)
   
  # Module name should match apk name to be installed.
  LOCAL_MODULE := LocalModuleName
  LOCAL_SRC_FILES := $(LOCAL_MODULE).apk
  LOCAL_MODULE_CLASS := APPS
  LOCAL_MODULE_SUFFIX := $(COMMON_ANDROID_PACKAGE_SUFFIX)
   
  include $(BUILD_PREBUILT)

9. Add a static JAVA library

LOCAL_PATH := $(call my-dir)
  include $(CLEAR_VARS)
   
  # Build all java files in the java subdirectory
  LOCAL_SRC_FILES := $(call all-subdir-java-files)
   
  # Any libraries that this library depends on
  LOCAL_JAVA_LIBRARIES := android.test.runner
   
  # The name of the jar file to create
  LOCAL_MODULE := sample
   
  # Build a static jar file.
  include $(BUILD_STATIC_JAVA_LIBRARY)

10. Related variables

The relevant compilation content can be defined in the Android.mk compilation module, that is, the relevant variables are specified as follows:
LOCAL_AAPT_FLAGS
LOCAL_ACP_UNAVAILABLE 
LOCAL_ADDITIONAL_JAVA_DIR 
 
LOCAL_AIDL_INCLUDES 
LOCAL_ALLOW_UNDEFINED_SYMBOLS 
LOCAL_ARM_MODE 
LOCAL_ASFLAGS 
LOCAL_ASSET_DIR

LOCAL_ASSET_FILES Set this variable when compiling the application (BUILD_PACKAGE) in the Android.mk file, indicating the resource file, usually defined as LOCAL_ASSET_FILES += $(call find-subdir-assets)

LOCAL_BUILT_MODULE_STEM

LOCAL_C_INCLUDES Additional C/C++ compilation header file path, use LOCAL_PATH to indicate the directory where this file is located,
                 for example:
                 LOCAL_C_INCLUDES += extlibs/zlib-1.2.3
                 LOCAL_C_INCLUDES += $(LOCAL_PATH)/src

LOCAL_CC specifies the C compiler
LOCAL_CERTIFICATE signature certification
LOCAL_CFLAGS defines additional flags (such as macro definitions) for the C/C++ compiler, for example: LOCAL_CFLAGS += -DLIBUTILS_NATIVE=1

LOCAL_CLASSPATH
LOCAL_COMPRESS_MODULE_SYMBOLS
LOCAL_COPY_HEADERS The header files that need to be copied when installing the application must also define LOCAL_COPY_HEADERS_TO

LOCAL_COPY_HEADERS_TO copy the destination path of the header file when installing the application
LOCAL_CPP_EXTENSION If your C++ file does not have cpp as the file suffix, you can specify the C++ file suffix name through LOCAL_CPP_EXTENSION
                    such as: LOCAL_CPP_EXTENSION := .cc
                    Note that the C++ file suffix must be consistent in the unified module .
LOCAL_CPPFLAGS passes additional flags to the C++ compiler, such as: LOCAL_CPPFLAGS += -ffriend-injection
LOCAL_CXX specifies the C++ compiler

LOCAL_DX_FLAGS
LOCAL_EXPORT_PACKAGE_RESOURCES
LOCAL_FORCE_STATIC_EXECUTABLE If the compiled executable program needs to be statically linked (execution does not depend on any dynamic library), then set LOCAL_FORCE_STATIC_EXECUTABLE:=true Currently only
                              libc has a static library form, this is only the application program under the /sbin directory in the file system It will be used. The applications in this directory are usually
                              not loaded in other parts of the file system when they are running, so they must be statically linked.

LOCAL_GENERATED_SOURCES

LOCAL_INSTRUMENTATION_FOR
LOCAL_INSTRUMENTATION_FOR_PACKAGE_NAME
LOCAL_INTERMEDIATE_SOURCES
LOCAL_INTERMEDIATE_TARGETS
LOCAL_IS_HOST_MODULE
LOCAL_JAR_MANIFEST
LOCAL_JARJAR_RULES
LOCAL_JAVA_LIBRARIES when compiling java applications and libraries Specify the included java class library. Currently, there are two types of core and framework. In most
                     cases, it is defined as: LOCAL_JAVA_LIBRARIES := core framework
                     Note that LOCAL_JAVA_LIBRARIES is not necessary, and compile APK is not allowed to be defined (the system will add it automatically)

LOCAL_JAVA_RESOURCE_DIRS
LOCAL_JAVA_RESOURCE_FILES
LOCAL_JNI_SHARED_LIBRARIES
LOCAL_LDFLAGS Pass additional parameters to the linker (be sure to pay attention to the order of parameters)

LOCAL_LDLIBS specifies additional libraries for the compilation of executable programs or libraries, and the specified libraries are in "-lxxx" format, for example:
             LOCAL_LDLIBS += -lcurses -lpthread
             LOCAL_LDLIBS += -Wl,-z,origin
 
The name of the generated module of LOCAL_MODULE (note Use LOCAL_PACKAGE_NAME instead of LOCAL_MODULE for application name)
LOCAL_MODULE_PATH Generate module path
 
LOCAL_MODULE_STEM
 
LOCAL_MODULE_TAGS Generate module flags 
 
LOCAL_NO_DEFAULT_COMPILER_FLAGS 
LOCAL_NO_EMMA_COMPILE 
LOCAL_NO_EMMA_INSTRUMENT 
LOCAL_NO_ST ANDARD_LIBRARIES 
LOCAL_OVERRIDES_PACKAGES 
LOCAL_PACKAGE_NAME APK application name 
LOCAL_POST_PROCESS_COMMAND
 
LOCAL_PREBUILT_EXECUTABLES precompiled including ( BUILDPREBUILT ) or (BUILD_PREBUILT ) or(BUILDPREB U I L T ) or (BUILD_HOST_PREBUILT), specify the executable file to be copied
LOCAL_PREBUILT_JAVA_LIBRARIES 
LOCAL_PREBUILT_LIBS precompiled including(BUILDPREBUILT) or (BUILD_PREBUILT) or(BUILDPREB U I L T ) or (BUILD_HOST_PREBUILT), specify the library that needs to be copied.
LOCAL_PREBUILT_OBJ_FILES 
LOCAL_PREBUILT_STATIC_JAVA_LIBRARIES  
 
LOCAL_PRELINK_MODULE Whether pre-connection processing is required (required by default, used for dynamic library optimization)
LOCAL_REQUIRED_MODULES Specifies the modules that the operation depends on module (when the module is installed The modules it depends on will be installed synchronously)
 
LOCAL_RESOURCE_DIR
LOCAL_SDK_VERSION
LOCAL_SHARED_LIBRARIES Linkable dynamic library
 
LOCAL_SRC_FILES Compile source files LOCAL_STATIC_JAVA_LIBRARIES
 
LOCAL_STATIC_LIBRARIES 
Linkable static library 
 
LOCAL_UNINSTALLABLE_MODULE 
LOCAL_UNSTRIPPED _PATH
 
LOCAL_WHOLE_STATIC_LIBRARIES specifies the complete static library that the module needs to load (these proficient libraries are not allowed to be linked when linking remove useless code)
 
LOCAL_YACCFLAGS
 
OVERRIDE_BUILT_MODULE_PATH

Guess you like

Origin blog.csdn.net/weixin_42581177/article/details/128687825