首页 > 代码库 > Android.mk解析

Android.mk解析

Android.mk文件用来向编译系统描述如何编译你的源代码。在编译整个工程的情况下,系统所找到的所有的Android.mk将会先存入subdir_makefiles变量中,随后一次性一次性include进整个编译文件中。

(1)Android.mk文件首先需要指定LOCAL_PATH变量,用于查找源文件。

由于一般情况下Android.mk和需要编译的源文件在同一目录下,所以定义成如下形式:
LOCAL_PATH:=$(call my-dir)
上面的语句的意思是将LOCAL_PATH变量定义成本文件所在目录路径。
(2)Android.mk中可以定义多个编译模块,每个编译模块都是以include $(CLEAR_VARS)开始以include $(BUILD_XXX)结束。
①include $(CLEAR_VARS)
CLEAR_VARS的定义在build/core/clear_vars.mk中,它清除了上百个除LOCAL_PATH外的变量,因而LOCAL_PATH通常被认为是一个编译模块的开始标志。
②include $(BUILD_XXX)
这个是Android.mk的重点,它表示将源码编译成什么目标文件,如动态库、可执行程序、APK等,它一般被认为是一个编译模块的结束标志。
(3)编译一个应用程序(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)

(4)编译一个依赖于静态Java库(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)

(5)编译一个需要用平台的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)

(6)编译一个需要用特定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)

(7)添加一个预编译应用程序

  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)

(8)添加一个静态JAVA库

  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)

(9)编译生成可执行文件

 

LOCAL_PATH:= $(call my-dir)    include $(CLEAR_VARS)    LOCAL_MODULE_TAGS := optional    LOCAL_C_INCLUDES += \       $(LOCAL_PATH)         LOCAL_SRC_FILES:= \          gpio-server.c    LOCAL_SHARED_LIBRARIES := \      libcurl \      libcutils    LOCAL_MODULE:= gpio-server    include $(BUILD_EXECUTABLE)    include $(call all-makefiles-under,$(LOCAL_PATH))

 

(10)编译生成动态库

 

LOCAL_PATH:= $(call my-dir)  include $(CLEAR_VARS)    LOCAL_SRC_FILES:= \      com_android_server_AlarmManagerService.cpp \      com_android_server_BatteryService.cpp \      com_android_server_InputApplicationHandle.cpp \      com_android_server_InputManager.cpp \      com_android_server_InputWindowHandle.cpp \      com_android_server_LightsService.cpp \      com_android_server_PowerManagerService.cpp \      com_android_server_SystemServer.cpp \      com_android_server_UsbDeviceManager.cpp \      com_android_server_UsbHostManager.cpp \      com_android_server_VibratorService.cpp \      com_android_server_location_GpsLocationProvider.cpp \      com_android_server_connectivity_Vpn.cpp \      com_android_server_ConnectivityService.cpp \      onl oad.cpp    LOCAL_C_INCLUDES += \      $(JNI_H_INCLUDE) \      frameworks/base/services \      frameworks/base/core/jni \      external/skia/include/core    LOCAL_SHARED_LIBRARIES := \      libandroid_runtime \      libcutils \      libhardware \      libhardware_legacy \      libnativehelper \      libsystem_server \      libutils \      libui \      libinput \      libskia \      libgui \      libusbhost    ifeq ($(WITH_MALLOC_LEAK_CHECK),true)      LOCAL_CFLAGS += -DMALLOC_LEAK_CHECK  endif    LOCAL_MODULE:= libandroid_servers    include $(BUILD_SHARED_LIBRARY) 

 

(11)通过Android.mk添加一个编译模块到系统中的顺序如下

①LOCAL_PATH
②CLEAR_VARS
③LOCAL_SRC_FILES
④LOCAL_CFLAGS(可选)
⑤LOCAL_MODULE
⑥LOCAL_STATIC_LIBRARIES(可选)
⑦BUILD_XXX
 
(12)Android.mk常用的变量
变量名说明
LOCAL_PATH用于确定源码所在的目录,最好把它放在CLEAR_VARS变量引用的前面,因为它不会被清除,每个Android.mk只需要定义
一次即可。
CLEAR_VARS它清空了很多以LOCAL_开头的变量(LOCAL_PATH除外)。由于所有的MakeFile都是在一个编译环境下执行的,因此
变量的定义理论上是全局的,在每个模块编译开始前进行清理工作是必要的。
LOCAL_MODULE模块名,需保证在整个编译系统中是唯一存在的,而且中间不可以有空格
LOCAL_MODULE_PATH模块的输出路径
LOCAL_SRC_FILES模块编译过程中涉及到的源文件。如果是JAVA程序,可以考虑调用all-subdir-java-files来一次性添加目录(包括子目录)下所有的java文件
因为有LOCAL_PATH,这里只需要给出文件名(相对路径)即可;而且编译系统有比较强的推导功能,可以自动计算依赖关系。
LOCAL_CC用于指定C编译器
LOCAL_CXX用于指定C++编译器
LOCAL_CPP_EXTENSION用于指定特殊的C++文件后缀名
LOCAL_CFLAGSC语言编译时的额外选项
LOCAL_CXXFLAGSC++语言编译时的额外选项
LOCAL_C_INCLUDE编译C和C++程序所需要的额外头文件
LOCAL_STATIC_LIBRARIES编译所需的静态库列表
LOCAL_SHARED_LIBRARIES编译时所需要的动态库列表
LOCAL_JAVA_LIBRARIES编译时所需要的JAVA类库
LOCAL_LDLIBS编译时所需要的链接选项
LOCAL_COPY_HEADERS安装应用程序时所需要复制的头文件列表,需要和LOCAL_COPY_HEARERS_TO变量配合使用
LOCAL_MODULE_CLASS标识所编译模块最后放置的位置。ETC表示放置在/system/etc.目录下,APPS表示放置在/system/app目录下,SHARED_LIBRARIES表示放置在/system/lib目录下。如果具体指定,则编译的模块不会放到编译系统中,最后会在out对应product的obj目录下的对应目录中。
LOCAL_MODULE_TAGS当前模块所包含的标签,一个模块可以包含多个标签。标签的值可能是eng、user、debug、development、optional。其中,optional是默认标签。
LOCAL_DEX_PREOPTapk的odex优化开关,默认是false
$(call my-dir)获取当前文件夹的路径
$(call all-java-files-under,<src>)获取指定目录下的所有java文件
$(call all-c-files-under, <src>)获取指定目录下的所有c文件
$(call all-Iaidl-files-under, <src>)获取指定目录下的所有AIDL文件
$(call all-makefiles-under, <folder>)获取指定目录下的所有Make文件
$(call intermediates-dir-for, <class>, <app_name>, <host or target>, <common?>)获取Build输入的目标文件夹路径
BUILD_HOST_STATIC_LIBRARY
BUILD_HOST_SHARED_LIBRARY
BUILD_STATIC_LIBRARY
BUILD_RAW_STATIC-LIBRARY
BUILD_SHARED_LIBRARY
BUILD_EXECUTABLE
BUILD_RAW_EXECUTABLE
BUILD_HOST_EXECUTABLE
BUILD_PACKAGE
BUILD_HOST_PREBUILT
BUILD_PREBUILT
BUILD_MULTI_PREBUILT
BUILD_JAVA_LIBRARY
BUILD_STATIC_JAVA_LIBRARY
BUILD_HOST_JAVA_LIBRARY
BUILD_DROIDDOC
BUILD_COPY_HEADERS
BUILD_KEY_CHAR_MAP
各种形式的编译模板,如静态、动态库文件,可执行文件,文档等

 

 

Android.mk解析