Introduction to Android.mk

1.Android.mk function

Android.mk is an Android project management file. Its function is equivalent to Makefile in the Linux environment. It compiles the source code you write into a library file or an executable file. One android.mk can contain multiple modules, that is, multiple modules can be compiled. A library file or executable file.

2. A simple case of Android.mk

Android open source code: /system/libvintf/Android.mk


 LOCAL_PATH := $(call my-dir)         #源文件在开发树中的位置

 include $(CLEAR_VARS)                #清除LOCAL_PATH变量之外的LOCAL_XXX变量
 LOCAL_MODULE := libvintf_recovery    #生成的模块名称
 LOCAL_SRC_FILES := VintfObjectRecovery.cpp   #需要编译进这个模块的源文件
 LOCAL_C_INCLUDES := $(LOCAL_PATH)/include/vintf   #头文件
 LOCAL_EXPORT_C_INCLUDE_DIRS := $(LOCAL_PATH)/include  #头文件以及头文件中所用到的头文件
 LOCAL_CFLAGS := -Wall -Werror        #编译源文件时要传递的编译器标志
 LOCAL_STATIC_LIBRARIES := \          #编译这个模块所依赖到的静态库
     libbase \
     libvintf \
     libhidl-gen-utils \
     libfs_mgr

 LOCAL_EXPORT_STATIC_LIBRARY_HEADERS := libhidl-gen-utils

 include $(BUILD_STATIC_LIBRARY)      #把这个模块编译成静态库

 $(call dist-for-goals,dist_files,$(HOST_OUT_EXECUTABLES)/checkvintf)

Through this simple case, I believe you have a preliminary understanding of how to write an Android.mk from scratch

3. Common grammar

grammar explain
call Call a macro function provided by the system, the above is my-dir
$() value
:= assignment
ifeq if equal
ifneq if not equal

4. Commonly used variable names

4.1 Define the current module position

LOCAL_PATH := $(call my-dir)

Every Android.mk file must begin by defining LOCAL_PATH . It is used to find source files in the development tree. The macro function my-dir provided by the system returns the path of the directory containing Android.mk. It will not be cleared by CLEAR_VARS, and the writing method is also very fixed, namely: LOCAL_PATH := $(call my-dir) .

LOCAL_PATH is a variable indicating the location of the current module, my-dir is a macro function provided by the system, which returns the path of the current file, $(call my-dir) means calling this function.

4.2 Clear LOCAL_XXX variables

include $(CLEAR_VARS)

The CLEAR_VARS variable is provided by the system and points to a specified GNU Makefile, which is responsible for cleaning up many LOCAL_xxx.
For example: LOCAL_MODULE , LOCAL_SRC_FILES , LOCAL_STATIC_LIBRARIES and so on. But do not clean LOCAL_PATH . This cleaning action is necessary because all compilation control files are parsed and executed by the same GNU Make, and its variables are global. Therefore, after cleaning, mutual influence can be avoided.

4.3 Source files that need to participate in compilation

LOCAL_SRC_FILES := VintfObjectRecovery.cpp

The LOCAL_SRC_FILES variable represents the files that need to be compiled

There are also other ways of writing:

LOCAL_SRC_FILES := $(call all-subdir-java-files)

The all-subdir-java-files function returns all java files in the LOCAL_PATH subdirectory.

But be careful, add the following statement at the end of the file to indicate the LOCAL_PATH directory.

include $ (call all-makefiles-under,$(LOCAL_PATH))

Or add LOCAL_PATH to each file path :

LOCAL_SRC_FILES := $(LOCAL_PATH)/VintfObjectRecovery.cpp

4.3.1 Several commonly used methods to obtain source files:

LOCAL_SRC_FILES := $(call all-java-files-under, src)

Get all Java files in the specified directory.

LOCAL_SRC_FILES := $(call all-c-files-under, src) 

Get all C language files in the specified directory.

LOCAL_SRC_FILES := $(call all-Iaidl-files-under, src) 

Get all AIDL files in the specified directory.

LOCAL_SRC_FILES := $(call all-makefiles-under, folder)

Get all Make files in the specified directory.

4.4 Define the name of the module generated by compilation

LOCAL_MODULE := libvintf_recovery

The LOCAL_MODULE variable must be defined and unique. As a module identifier, the compilation system will automatically generate an appropriate prefix and suffix.

4.5 Compiled tags

LOCAL_MODULE_TAGS := optional

Commonly used are: debug , eng , user , development or optional (default).

4.6 Signature properties

The commonly used ones are:
platform : The APK completes some core functions of the system. Tested for access to folders present in the system.
shared : The APK needs to share data with the home/contacts process.
media : The APK is part of the media/download system.

LOCAL_CERTIFICATE := platform

4.7 Reference static jar library

LOCAL_STATIC_JAVA_LIBRARIES := jar1 jar2

jar1 and jar2 are the aliases of the third-party Java package, which need to be defined, see below.
LOCAL_JAVA_LIBRARIES is used to reference dynamic jars.

4.8 What is compiled into

4.8.1 Compile into apk

include $(BUILD_PACKAGE)

4.8.2 Compile into a static library

include $(BUILD_STATIC_LIBRARY)

4.8.3 Compile into a dynamic library

include $(BUILD_SHARED_LIBRARY)

4.8.4 Compile into executable program

include $(BUILD_EXECUTABLE)

4.8.5 Compile into Java static library

include $(BUILD_STATIC_JAVA_LIBRARY)

4.9 Libraries that need to be precompiled

LOCAL_PREBUILT_STATIC_JAVA_LIBRARIES := jar1:path1 \
										jar2:path2

jar1, jar2 define static library aliases, path1, path2 is the path of static library, pay attention to write all the way to the suffix .jar.

4.10 Copy to local compilation

include $(BUILD_MULTI_PREBUILT) 

Copy the library defined by prebuild to local for compilation.

4.11 Specify the build directory

LOCAL_MODULE_PATH := $(TARGET_OUT)/

$(TARGET_OUT) stands for /system

$(TARGET_OUT_DATA_APPS) represents the data/app directory

4.12 Compile under what version

LOCAL_MODULE_TAGS

user : means that the module is only compiled under the user version
eng : means that the module is only compiled under the eng version
tests : means that the module is only compiled under the tests version
optional : means that the module is compiled under all versions

4.13 How many bits are built into

LOCAL_MULTILIB

both: build both 32-bit and 64-bit
32:build only 32-bit.
64: build only 64-bit.

4.14 Execution of pre-installed configuration files

include $(BUILD_PREBUILT)

The execution of the pre-installed configuration file is to copy the dynamic library to the specified directory, use this label;

For example the following code:

#是将libtsldc.so 预装到(其实就是拷贝到$(TARGET_OUT_VENDOR)/lib下面)
include $(CLEAR_VARS)
LOCAL_SRC_FILES     := TS_LDC/C626/tsldc/lib/libtsldc.so
LOCAL_MULTILIB      := 32
LOCAL_MODULE        := libtsldc
LOCAL_MODULE_SUFFIX := .so
LOCAL_MODULE_TAGS   := optional
LOCAL_MODULE_CLASS  := SHARED_LIBRARIES
LOCAL_MODULE_PATH   := $(TARGET_OUT_VENDOR)/lib
include $(BUILD_PREBUILT)

4.15 Header files

LOCAL_INC_FILES

The example of adding the .h header file written by yourself is as follows;LOCAL_INC_FILES := person.h

LOCAL_C_INCLUDES

Specifies the search path for header files.LOCAL_C_INCLUDES := $(LOCAL_PATH)/include/vintf

4.16 Compiler Flags

LOCAL_CFLAGS

This optional variable sets the compiler flags for the build system to pass when building C and C++ source files. This feature may be useful for specifying additional macro definitions or compile options

LOCAL_CPPFLAGS

An optional set of compiler flags is passed only when building C++ source files. They will appear after LOCAL_CFLAGS on the compiler command line

4.17 List of additional linker flags required for compilation

LOCAL_LDLIBS

List of additional linker flags required for compilation. It lets you pass the name of a specific system library using the -l prefix.

Example:LOCAL_LDLIBS += -llog -ldl -lz

5.0 Acknowledgments

This article refers to the following two blogs:

Android.mk parsing and using
Android.mk grammar and variable introduction

Guess you like

Origin blog.csdn.net/qq_45458713/article/details/129092447