AOSP——Android.mk解析

Android.mk

  • Android.mk是什么?
  • Android.mk结构
    • 设置当前路径
    • 清空所有LOCAL变量
    • 重新设置LOCAL变量
    • 选择构建方式
    • 调用当前目录下的所有mk文件

Android.mk是什么?

Android.mk是用于控制Android代码的编译流程,用shell编写

Android.mk结构

设置当前路径

除带#的注释,Android.mk第一句话都是

LOCAL_PATH := $(call my-dir)

即将my-dir方法的返回值赋值给LOCAL_PATH,让其等于当前目录,其中的my-dir方法在build/core/defintions.mk中定义,源码如下

###########################################################
## Retrieve the directory of the current makefile
## Must be called before including any other makefile!!
###########################################################

# Figure out where we are.
define my-dir
$(strip \
  $(eval LOCAL_MODULE_MAKEFILE := $$(lastword $$(MAKEFILE_LIST))) \
  $(if $(filter $(BUILD_SYSTEM)/% $(OUT_DIR)/%,$(LOCAL_MODULE_MAKEFILE)), \
    $(error my-dir must be called before including any other makefile.) \
   , \
    $(patsubst %/,%,$(dir $(LOCAL_MODULE_MAKEFILE))) \
   ) \
 )
endef

清空所有LOCAL变量

接下来是

include $(CLEAR_VARS)

CLEAR_VARS的值在/build/core/config.mk,源码如下:

CLEAR_VARS := $(BUILD_SYSTEM)/clear_vars.mk

BUILD_SYSTEM在/build/core/cleanbuild.mk及/build/core/config.mk中均有定义,源码如下:

BUILD_SYSTEM := $(TOPDIR)build/core

TOPDIR为空,所以BUILD_SYSTEM:=build/core,故CLEAR_VARS=build/core/clear_vars.mk,其内容为清空所有LOCAL变量,部分源码如下:

###########################################################
## Clear out values of all variables used by rule templates.
###########################################################

# '',true
LOCAL_2ND_ARCH_VAR_PREFIX:=
LOCAL_32_BIT_ONLY:=
LOCAL_AAPT2_ONLY:=
LOCAL_AAPT_FLAGS:=
LOCAL_AAPT_INCLUDE_ALL_RESOURCES:=
LOCAL_AAPT_NAMESPACES:=
LOCAL_ADDITIONAL_CERTIFICATES:=
LOCAL_ADDITIONAL_CHECKED_MODULE:=
LOCAL_ADDITIONAL_DEPENDENCIES:=
LOCAL_ADDITIONAL_HTML_DIR:=
LOCAL_ADDITIONAL_JAVA_DIR:=

重新设置LOCAL变量

接下来是为自己的代码设置编译流程,常用的变量及取值有

变量 取值及意义
LOCAL_PACKAGE_NAME 包名,自定义
LOCAL_MODULE_TAGS optional
LOCAL_SRC_FILES $(call all-java-files-under, src)
LOCAL_STATIC_JAVA_LIBRARIES android-support-v4
LOCAL_JAVA_LIBRARIES
LOCAL_CERTIFICATE platform
LOCAL_SDK_VERSION current
LOCAL_PRODUCT_MODULE true
LOCAL_OVERRIDES_PACKAGES
LOCAL_STATIC_ANDROID_LIBRARIES \
android-support-v7-appcompat \
android-support-v7-recyclerview

选择构建方式

接下来是为自己的代码设置构建方法,常用的构建方式及意义有

构建方式 意义
include $(BUILD_PACKAGE) 构建为app
BUILD_PACKAGE的值在/build/core/config.mk
BUILD_PACKAGE := $(BUILD_SYSTEM)/package.mk
意为引进build/core/package.mk为程序打包

调用当前目录下的所有mk文件

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

all-makefiles-under方法在在build/core/defintions.mk中定义,源码如下

你可能感兴趣的:(#,AOSP,android)