6月 282018
 

1.Android.mk是Android源码编译系统的Makefile,用于编译系统中的C++和Java的动态库\静态库\可执行文件和apk等.
其大概格式如下:

LOCAL_PATH := $(call my-dir)

include $(CLEAR_VARS)
LOCAL_MODULE_TAGS := optional
LOCAL_STATIC_JAVA_LIBRARIES := libarity android-support-v4 guava
LOCAL_SRC_FILES := $(call all-java-files-under, src)
LOCAL_SDK_VERSION := current
LOCAL_PACKAGE_NAME := Calculator
include $(BUILD_PACKAGE)

##################################################
include $(CLEAR_VARS)
LOCAL_PREBUILT_STATIC_JAVA_LIBRARIES := libarity:arity-2.1.2.jar
include $(BUILD_MULTI_PREBUILT)

# Use the folloing include to make our test apk.
include $(call all-makefiles-under,$(LOCAL_PATH))

2.常用语法格式介绍
上面的示例中共涉及到三类语法
1)变量的定义,比如其中的LOCAL_PATH为定义的变量,可以在其他地方被调用
变量不是必须全部大写或者必须以LOCAL_开头,可以是其他形式的,因为安卓的编译系统会加载所有的mk文件到一个环境里面,所以定义变量应该加上一个特殊的前缀,防止覆盖一些全局的变量.
2)my-dir为mk系统中预定义的宏,通过$(call my-dir)来调用
mk系统中还定义了很多其他的宏,这些定义均在build/core/definitions.mk中,有兴趣的可以浏览一下这个文件
3)include语法包含其他的mk文件
我们可以看到示例中有一个include $(BUILD_PACKAGE),其实BUILD_PACKAGE是在buile/core/config.mk中定义一个变量,如下:
BUILD_PACKAGE:= $(BUILD_SYSTEM)/package.mk
这一行代码其实是把package.mk包含到了编译步骤中,用于编译Calculator这个apk
4)shell命令

3.mk文件结构介绍
1)通常都以LOCAL_PATH开始,指定当前工程的路径,一般也是使用my-dir宏指定,如下:
LOCAL_PATH := $(call my-dir)

2)编写具体模块编译脚本
首先确定编译模块的类型,是静态c++库\动态C++库还是可执行程序,或者jar包或者apk,参见"模块编译类型定义"部分.
a. 一个mk文件中可以编译多种类型的模块,每个模块一般都是以include $(CLEAR_VARS)开始
CLEAR_VARS实际上是引用的build/core/clear_vars.mk文件,用于清除部分以LOCAL_开始的变量(不会清除LOCAL_PATH),具体可以浏览clear_vars.mk

b. 然后定义编译这个模块所需要用到的一些LOCAL_变量,比如如下:
LOCAL_STATIC_JAVA_LIBRARIES := libarity android-support-v4 guava  #编译apk用到的静态库
LOCAL_SRC_FILES := $(call all-java-files-under, src) # 要编译的源码
LOCAL_PACKAGE_NAME := Calculator #包名

c. include + 编译类型 结尾,如:
include $(BUILD_PACKAGE)

4.模块编译类型定义
mk系统支持多种类型的编译,这些类型都在build/core/config.mk中定义,每种编译类型由一个特定的mk文件去处理,如下是在Android4.4系统中抓取的编译类型定义.
BUILD_HOST_STATIC_LIBRARY:= $(BUILD_SYSTEM)/host_static_library.mk
BUILD_HOST_SHARED_LIBRARY:= $(BUILD_SYSTEM)/host_shared_library.mk
BUILD_STATIC_LIBRARY:= $(BUILD_SYSTEM)/static_library.mk
BUILD_RAW_STATIC_LIBRARY := $(BUILD_SYSTEM)/raw_static_library.mk
BUILD_SHARED_LIBRARY:= $(BUILD_SYSTEM)/shared_library.mk
BUILD_EXECUTABLE:= $(BUILD_SYSTEM)/executable.mk
BUILD_RAW_EXECUTABLE:= $(BUILD_SYSTEM)/raw_executable.mk
BUILD_HOST_EXECUTABLE:= $(BUILD_SYSTEM)/host_executable.mk
BUILD_PACKAGE – 构建一个Android apk
BUILD_PHONY_PACKAGE:= $(BUILD_SYSTEM)/phony_package.mk
BUILD_HOST_PREBUILT:= $(BUILD_SYSTEM)/host_prebuilt.mk
BUILD_PREBUILT:= $(BUILD_SYSTEM)/prebuilt.mk
BUILD_MULTI_PREBUILT:= $(BUILD_SYSTEM)/multi_prebuilt.mk
BUILD_JAVA_LIBRARY:= $(BUILD_SYSTEM)/java_library.mk
BUILD_STATIC_JAVA_LIBRARY:= $(BUILD_SYSTEM)/static_java_library.mk
BUILD_HOST_JAVA_LIBRARY:= $(BUILD_SYSTEM)/host_java_library.mk
BUILD_DROIDDOC:= $(BUILD_SYSTEM)/droiddoc.mk
BUILD_COPY_HEADERS := $(BUILD_SYSTEM)/copy_headers.mk
BUILD_NATIVE_TEST := $(BUILD_SYSTEM)/native_test.mk
BUILD_HOST_NATIVE_TEST := $(BUILD_SYSTEM)/host_native_test.mk
BUILD_NOTICE_FILE := $(BUILD_SYSTEM)/notice_files.mk

打赏
 Posted by on 2018-06-28

 Leave a Reply

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">

* Copy This Password *

* Type Or Paste Password Here *

13,252迄今阻止垃圾评论Spam Free Wordpress