当前位置:   article > 正文

Android 编译之android.mk_build_phony_package

build_phony_package

1. android系统源码的编译流程

来回顾一下常见的编译步骤:
source build/envsetup.sh
lunch xxx
make -j8 2>&1 | tee build.log
这三步究竟做了什么呢?我们来逐步分析一下。

1.1 source build/envsetup.sh

build/envsetup.sh这个文件中定义了一些变量和函数,执行source build/envsetup.sh之后,envsetup.sh中的变量成了全局变量,而其中的函数也可以直接在当前终端命令行中使用了。这些函数可以帮助我们切换目录,查找文件,可以使我们在编译源码时更方便。这些函数可通过hmm函数来查看,下面列出了一些常用的函数:

  • lunch: lunch <product_name>-<build_variant> (选择要编译的目标产品和版本)
  • tapas: tapas [<App1> <App2> ...] [arm|x86|mips|armv5] [eng|userdebug|user]
  • croot: Changes directory to the top of the tree.(切换到源码的顶层目录)
  • m: Makes from the top of the tree.(从顶层目录build整个系统)
  • mm: Builds all of the modules in the current directory, but not their dependencies.(构建当前目录下所有的模块,但不包括它们的依赖)
  • mmm: Builds all of the modules in the supplied directories, but not their dependencies.(构建指定目录下所有的模块,但不包括它们的依赖)
  • mma: Builds all of the modules in the current directory, and their dependencies.(构建当前目录下所有的模块以及它们所依赖的模块)
  • mmma: Builds all of the modules in the supplied directories, and their dependencies.(构建指定目录下所有的模块以及它们所依赖的模块)
  • provision: Flash device with all required partitions. Options will be passed on to fastboot.(将设备所有需要的分区刷入,选项将传递给fastboot)
  • cgrep: Greps on all local C/C++ files.(在C,C++文件中搜索指定关键字)
  • ggrep: Greps on all local Gradle files.(在gradle文件中搜索指定关键字)
  • jgrep: Greps on all local Java files.(在java文件中搜索指定关键字)
  • resgrep: Greps on all local res/*.xml files.(在资源xml文件中搜索指定关键字)
  • mangrep: Greps on all local AndroidManifest.xml files.(在AndroidManifest.xml文件中搜索指定关键字)
  • mgrep: Greps on all local Makefiles files.(在Makefiles和android.mk文件中搜索指定关键字)
  • sepgrep: Greps on all local sepolicy files.(在sepolicy文件中搜索指定关键字)
  • sgrep: Greps on all local source files.(在所有本地文件中搜索指定关键字)
  • godir: Go to the directory containing a file.(切换到包含某个文件的目录下)

除了hmm提示的这些,还有一些方法,具体可以直接查看build/envsetup.sh文件:

  • cproj: 向上切换到最近包含Android.mk的目录下
  • findmakefile: 打印当前目录所在工程的Android.mk的文件路径
  • getsdcardpath: 获取Sd卡路径
  • getscreenshotpath: 获取屏幕截图的路径
  • getlastscreenshot: 获取最后一张截图,导出到当前目录下
  • getbugreports: 将bug报告从设备上导出到本地,bug报告存放于目录/sdcard/bugreports
  • gettop: 获取Android源码根目录
  • pid: pid processname 查看某个可执行程序对应的进程id
  • key_back: 模拟按返回键
  • key_home: 模拟按Home键
  • key_menu: 模拟按菜单键

envsetup.sh中还定义了add_lunch_combo函数,并且多次执行了add_lunch_combo函数,将自身定义的所有product添加到LUNCH_MENU_CHOICES中:

 

  1. # Clear this variable. It will be built up again when the vendorsetup.sh
  2. # files are included at the end of this file.
  3. unset LUNCH_MENU_CHOICES
  4. function add_lunch_combo()
  5. {
  6. local new_combo=$1
  7. local c
  8. for c in ${LUNCH_MENU_CHOICES[@]} ; do
  9. if [ "$new_combo" = "$c" ] ; then
  10. return
  11. fi
  12. done
  13. LUNCH_MENU_CHOICES=(${LUNCH_MENU_CHOICES[@]} $new_combo)
  14. }
  15. # add the default one here
  16. add_lunch_combo aosp_arm-eng
  17. add_lunch_combo aosp_arm64-eng
  18. add_lunch_combo aosp_mips-eng
  19. add_lunch_combo aosp_mips64-eng
  20. add_lunch_combo aosp_x86-eng
  21. add_lunch_combo aosp_x86_64-eng

最后,envsetup.sh中还遍历并执行vendor和device目录下的所有vendorsetup.sh文件:

 

  1. # Execute the contents of any vendorsetup.sh files we can find.
  2. for f in `test -d device && find -L device -maxdepth 4 -name 'vendorsetup.sh' 2> /dev/null | sort` \
  3. `test -d vendor && find -L vendor -maxdepth 4 -name 'vendorsetup.sh' 2> /dev/null | sort` \
  4. `test -d product && find -L product -maxdepth 4 -name 'vendorsetup.sh' 2> /dev/null | sort`
  5. do
  6. echo "including $f"
  7. . $f
  8. done
  9. unset f

这些vendorsetup.sh中也定义了的一些product,执行这些vendorsetup.sh后,也会将它们当中定义的product添加到LUNCH_MENU_CHOICES中:

 

  1. add_lunch_combo full_k63v2_64_bsp-eng
  2. add_lunch_combo full_k63v2_64_bsp-user
  3. add_lunch_combo full_k63v2_64_bsp-userdebug

在此之后,我们就可以在命令行用lunch函数从LUNCH_MENU_CHOICES中选择需要编译的product。

1.2 lunch

执行lunch函数时,如果用户指定了product,获取指定的product。如果用户未指定product,调用print_lunch_menu函数输出上一步生成的lunch menu choices让用户选择。如用户指定了product或者提示后选择了product,会获取用户指定的product,并提取$product和$variant。然后检查是否支持product,如不支持,提示不支持并退出。如支持,接着会调用set_stuff_for_environment函数设置一系列环境变量,还会调用printconfig输出相关变量和配置信息。

 

  1. function lunch()
  2. {
  3. local answer
  4. if [ "$1" ] ; then
  5. answer=$1
  6. else
  7. print_lunch_menu
  8. echo -n "Which would you like? [aosp_arm-eng] "
  9. read answer
  10. fi
  11. local selection=
  12. if [ -z "$answer" ]
  13. then
  14. selection=aosp_arm-eng
  15. elif (echo -n $answer | grep -q -e "^[0-9][0-9]*$")
  16. then
  17. if [ $answer -le ${#LUNCH_MENU_CHOICES[@]} ]
  18. then
  19. selection=${LUNCH_MENU_CHOICES[$(($answer-1))]}
  20. fi
  21. else
  22. selection=$answer
  23. fi
  24. export TARGET_BUILD_APPS=
  25. local product variant_and_version variant version
  26. product=${selection%%-*} # Trim everything after first dash
  27. variant_and_version=${selection#*-} # Trim everything up to first dash
  28. if [ "$variant_and_version" != "$selection" ]; then
  29. variant=${variant_and_version%%-*}
  30. if [ "$variant" != "$variant_and_version" ]; then
  31. version=${variant_and_version#*-}
  32. fi
  33. fi
  34. if [ -z "$product" ]
  35. then
  36. echo
  37. echo "Invalid lunch combo: $selection"
  38. return 1
  39. fi
  40. TARGET_PRODUCT=$product \
  41. TARGET_BUILD_VARIANT=$variant \
  42. TARGET_PLATFORM_VERSION=$version \
  43. build_build_var_cache
  44. if [ $? -ne 0 ]
  45. then
  46. return 1
  47. fi
  48. export TARGET_PRODUCT=$(get_build_var TARGET_PRODUCT)
  49. export TARGET_BUILD_VARIANT=$(get_build_var TARGET_BUILD_VARIANT)
  50. if [ -n "$version" ]; then
  51. export TARGET_PLATFORM_VERSION=$(get_build_var TARGET_PLATFORM_VERSION)
  52. else
  53. unset TARGET_PLATFORM_VERSION
  54. fi
  55. export TARGET_BUILD_TYPE=release
  56. echo
  57. set_stuff_for_environment
  58. printconfig
  59. destroy_build_var_cache
  60. }

1.3 make -j8 2>&1 | tee build.log

  • -j8是用于指定编译的cpu核心,如果编译服务器配置好,可以略为改大一些。
  • 2>&1 | tee build.log则是同时输出编译的提示、异常信息到终端和build.log文件中。
  • make则是执行make命令,寻找源码根目录下的Makefile,解析Makefile并开始整个源码的编译。

接下来我们就从源码根目录下的Makefile开始逐步解析。

2. android的Makefile和android.mk

执行make之后,从源码根目录下的Makefile开始逐步解析。该Makefile的内容很少,只是导入了build/make/core/main.mk。
Makefile:

 

  1. ### DO NOT EDIT THIS FILE ###
  2. include build/make/core/main.mk
  3. ### DO NOT EDIT THIS FILE ###

main.mk中有两个最重要的部分,一个是导入build/make/core/config.mk和build/make/core/definitions.mk。
main.mk:

 

  1. ......
  2. BUILD_SYSTEM := $(TOPDIR)build/make/core
  3. ......
  4. # Set up various standard variables based on configuration
  5. # and host information.
  6. include $(BUILD_SYSTEM)/config.mk
  7. ......
  8. # Bring in standard build system definitions.
  9. include $(BUILD_SYSTEM)/definitions.mk
  10. ......

config.mk中定义了一系列编译需要用到的变量,比如常用的CLEAR_VARS、BUILD_PACKAGE。这些变量实际上是每一个变量导入另一个.mk文件。每一个被导入的.mk完成一个基本功能,比如,CLEAR_VARS对应的build/make/core/clear_vars.mk是清除编译的临时变量,BUILD_PACKAGE对应的build/make/core/package.mk是编译APK。
config.mk:

 

  1. # Set up efficient math functions which are used in make.
  2. # Here since this file is included by envsetup as well as during build.
  3. include $(BUILD_SYSTEM)/math.mk
  4. # Various mappings to avoid hard-coding paths all over the place
  5. include $(BUILD_SYSTEM)/pathmap.mk
  6. # Allow projects to define their own globally-available variables
  7. include $(BUILD_SYSTEM)/project_definitions.mk
  8. # ###############################################################
  9. # Build system internal files
  10. # ###############################################################
  11. BUILD_COMBOS:= $(BUILD_SYSTEM)/combo
  12. CLEAR_VARS:= $(BUILD_SYSTEM)/clear_vars.mk
  13. BUILD_HOST_STATIC_LIBRARY:= $(BUILD_SYSTEM)/host_static_library.mk
  14. BUILD_HOST_SHARED_LIBRARY:= $(BUILD_SYSTEM)/host_shared_library.mk
  15. BUILD_STATIC_LIBRARY:= $(BUILD_SYSTEM)/static_library.mk
  16. BUILD_HEADER_LIBRARY:= $(BUILD_SYSTEM)/header_library.mk
  17. BUILD_AUX_STATIC_LIBRARY:= $(BUILD_SYSTEM)/aux_static_library.mk
  18. BUILD_AUX_EXECUTABLE:= $(BUILD_SYSTEM)/aux_executable.mk
  19. BUILD_SHARED_LIBRARY:= $(BUILD_SYSTEM)/shared_library.mk
  20. BUILD_EXECUTABLE:= $(BUILD_SYSTEM)/executable.mk
  21. BUILD_HOST_EXECUTABLE:= $(BUILD_SYSTEM)/host_executable.mk
  22. BUILD_PACKAGE:= $(BUILD_SYSTEM)/package.mk
  23. BUILD_PHONY_PACKAGE:= $(BUILD_SYSTEM)/phony_package.mk
  24. BUILD_RRO_PACKAGE:= $(BUILD_SYSTEM)/build_rro_package.mk
  25. BUILD_HOST_PREBUILT:= $(BUILD_SYSTEM)/host_prebuilt.mk
  26. BUILD_PREBUILT:= $(BUILD_SYSTEM)/prebuilt.mk
  27. BUILD_MULTI_PREBUILT:= $(BUILD_SYSTEM)/multi_prebuilt.mk
  28. BUILD_JAVA_LIBRARY:= $(BUILD_SYSTEM)/java_library.mk
  29. BUILD_STATIC_JAVA_LIBRARY:= $(BUILD_SYSTEM)/static_java_library.mk
  30. BUILD_HOST_JAVA_LIBRARY:= $(BUILD_SYSTEM)/host_java_library.mk
  31. BUILD_DROIDDOC:= $(BUILD_SYSTEM)/droiddoc.mk
  32. BUILD_APIDIFF:= $(BUILD_SYSTEM)/apidiff.mk
  33. BUILD_COPY_HEADERS := $(BUILD_SYSTEM)/copy_headers.mk
  34. BUILD_NATIVE_TEST := $(BUILD_SYSTEM)/native_test.mk
  35. BUILD_NATIVE_BENCHMARK := $(BUILD_SYSTEM)/native_benchmark.mk
  36. BUILD_HOST_NATIVE_TEST := $(BUILD_SYSTEM)/host_native_test.mk
  37. BUILD_FUZZ_TEST := $(BUILD_SYSTEM)/fuzz_test.mk
  38. BUILD_HOST_FUZZ_TEST := $(BUILD_SYSTEM)/host_fuzz_test.mk
  39. BUILD_SHARED_TEST_LIBRARY := $(BUILD_SYSTEM)/shared_test_lib.mk
  40. BUILD_HOST_SHARED_TEST_LIBRARY := $(BUILD_SYSTEM)/host_shared_test_lib.mk
  41. BUILD_STATIC_TEST_LIBRARY := $(BUILD_SYSTEM)/static_test_lib.mk
  42. BUILD_HOST_STATIC_TEST_LIBRARY := $(BUILD_SYSTEM)/host_static_test_lib.mk
  43. BUILD_NOTICE_FILE := $(BUILD_SYSTEM)/notice_files.mk
  44. BUILD_HOST_DALVIK_JAVA_LIBRARY := $(BUILD_SYSTEM)/host_dalvik_java_library.mk
  45. BUILD_HOST_DALVIK_STATIC_JAVA_LIBRARY := $(BUILD_SYSTEM)/host_dalvik_static_java_library.mk
  46. BUILD_HOST_TEST_CONFIG := $(BUILD_SYSTEM)/host_test_config.mk
  47. BUILD_TARGET_TEST_CONFIG := $(BUILD_SYSTEM)/target_test_config.mk

而definitions.mk中用define也定义了一系列变量,比如常用的my-dir、all-subdir-makefiles、all-subdir-java-files等等。这些编译主要用于处理目录相关的功能,比如my-dir是获取当前目录路径,all-subdir-makefiles调用所有子目录的android.mk。definitions.mk中有很多变量,列举如下:
definitions.mk:

 

  1. define print-vars
  2. define true-or-empty
  3. define gcno-touch-rule
  4. define my-dir
  5. define all-makefiles-under
  6. define first-makefiles-under
  7. define all-subdir-makefiles
  8. define all-named-subdir-makefiles
  9. define all-named-dirs-under
  10. define all-subdir-named-dirs
  11. define all-named-files-under
  12. define all-subdir-named-files
  13. define all-java-files-under
  14. define all-subdir-java-files
  15. define all-c-files-under
  16. define all-subdir-c-files
  17. define all-cpp-files-under
  18. define all-subdir-cpp-files
  19. define all-Iaidl-files-under
  20. define all-subdir-Iaidl-files
  21. define all-vts-files-under
  22. define all-subdir-vts-files
  23. define all-logtags-files-under
  24. define all-proto-files-under
  25. define all-renderscript-files-under
  26. define all-S-files-under
  27. define all-html-files-under
  28. define all-subdir-html-files
  29. define find-subdir-files
  30. define find-subdir-subdir-files
  31. define find-subdir-assets
  32. define find-other-java-files
  33. define find-other-html-files
  34. define find-files-in-subdirs
  35. define find-parent-file
  36. define find-test-data-in-subdirs
  37. define add-dependency
  38. define reverse-list
  39. define def-host-aux-target
  40. define find-idf-prefix
  41. define intermediates-dir-for
  42. define local-intermediates-dir
  43. define generated-sources-dir-for
  44. define local-generated-sources-dir
  45. define module-built-files
  46. define module-installed-files
  47. define module-stubs-files
  48. define doc-timestamp-for
  49. define java-lib-files
  50. define java-lib-header-files
  51. define java-lib-header-files
  52. define java-lib-deps
  53. define app-lib-files
  54. define app-lib-header-files
  55. define app-lib-header-files
  56. define streq
  57. define normalize-path-list
  58. define normalize-comma-list
  59. define word-colon
  60. define collapse-pairs
  61. define uniq-pairs-by-first-component
  62. define modules-for-tag-list
  63. define module-names-for-tag-list
  64. define get-tagged-modules
  65. define append-path
  66. define echo-warning
  67. define echo-error
  68. define pretty-warning
  69. define pretty-error
  70. define _get-package-overrides
  71. define get-package-overrides
  72. define pretty
  73. define pretty
  74. define include-depfile
  75. define include-depfiles-for-objs
  76. define track-src-file-obj
  77. define _track-src-file-obj
  78. define track-src-file-gen
  79. define _track-src-file-gen
  80. define track-gen-file-obj
  81. define transform-l-to-c-or-cpp
  82. define transform-y-to-c-or-cpp
  83. define _merge-renderscript-d
  84. define transform-renderscripts-to-java-and-bc
  85. define transform-bc-to-so
  86. define transform-renderscripts-to-cpp-and-bc
  87. define transform-aidl-to-java
  88. define transform-aidl-to-cpp
  89. define define-aidl-java-rule
  90. define define-aidl-java-rule
  91. define-aidl-java-rule-src
  92. define define-aidl-cpp-rule
  93. define define-aidl-cpp-rule
  94. define-aidl-cpp-rule-src
  95. define transform-vts-to-cpp
  96. define define-vts-cpp-rule
  97. define define-vts-cpp-rule
  98. define-vts-cpp-rule-src
  99. define transform-logtags-to-java
  100. define transform-proto-to-java
  101. define transform-proto-to-cc
  102. define c-includes
  103. define transform-cpp-to-o-compiler-args
  104. define clang-tidy-cpp
  105. define transform-cpp-to-o
  106. define transform-cpp-to-o
  107. define transform-c-or-s-to-o-compiler-args
  108. define transform-c-to-o-compiler-args
  109. define clang-tidy-c
  110. define transform-c-to-o
  111. define transform-c-to-o
  112. define transform-s-to-o
  113. define transform-asm-to-o
  114. define transform-m-to-o
  115. define transform-host-cpp-to-o-compiler-args
  116. define clang-tidy-host-cpp
  117. define transform-host-cpp-to-o
  118. define transform-host-cpp-to-o
  119. define transform-host-c-or-s-to-o-common-args
  120. define transform-host-c-or-s-to-o
  121. define transform-host-c-to-o-compiler-args
  122. define clang-tidy-host-c
  123. define transform-host-c-to-o
  124. define transform-host-c-to-o
  125. define transform-host-s-to-o
  126. define transform-host-m-to-o
  127. define transform-host-mm-to-o
  128. define compile-dotdot-cpp-file
  129. define compile-dotdot-c-file
  130. define compile-dotdot-s-file
  131. define compile-dotdot-s-file-no-deps
  132. define _concat-if-arg2-not-empty
  133. define split-long-arguments
  134. define _extract-and-include-single-target-whole-static-lib
  135. define extract-and-include-whole-static-libs-first
  136. define extract-and-include-target-whole-static-libs
  137. define transform-o-to-static-lib
  138. define _extract-and-include-single-aux-whole-static-lib
  139. define extract-and-include-aux-whole-static-libs
  140. define transform-o-to-aux-static-lib
  141. define transform-o-to-aux-executable-inner
  142. define transform-o-to-aux-executable
  143. define transform-o-to-aux-static-executable-inner
  144. define transform-o-to-aux-static-executable
  145. define _extract-and-include-single-host-whole-static-lib
  146. define extract-and-include-host-whole-static-libs
  147. define create-dummy.o-if-no-objs
  148. define get-dummy.o-if-no-objs
  149. define delete-dummy.o-if-no-objs
  150. define transform-host-o-to-static-lib
  151. define transform-host-o-to-shared-lib-inner
  152. define transform-host-o-to-shared-lib
  153. define transform-host-o-to-package
  154. define transform-o-to-shared-lib-inner
  155. define transform-o-to-shared-lib
  156. define transform-to-stripped
  157. define transform-to-stripped-keep-mini-debug-info
  158. define transform-to-stripped-keep-symbols
  159. define pack-elf-relocations
  160. define transform-o-to-executable-inner
  161. define transform-o-to-executable
  162. define transform-o-to-static-executable-inner
  163. define transform-o-to-static-executable
  164. define transform-host-o-to-executable-inner
  165. define transform-host-o-to-executable
  166. define create-resource-java-files
  167. define find-generated-R.java
  168. define aapt2-compile-one-resource-file
  169. define aapt2-compile-resource-dirs
  170. define aapt2-compile-resource-zips
  171. define aapt2-compile-one-resource-file-rule
  172. define aapt2-compiled-resource-out-file
  173. define aapt2-link
  174. define emit-line
  175. define dump-words-to-file
  176. define unzip-jar-files
  177. define jar-args-sorted-files-in-directory
  178. define fetch-additional-java-source
  179. define write-java-source-list
  180. define compile-java
  181. define transform-java-to-header.jar
  182. define commit-change-for-toc
  183. define _transform-dex-to-toc
  184. define define-dex-to-toc-rule
  185. define define-dex-to-toc-rule
  186. define define-dex-to-toc-rule
  187. define define-dex-to-toc-rule
  188. define codename-or-sdk-to-sdk
  189. define desugar-classes-jar
  190. define transform-classes.jar-to-dex
  191. define transform-classes-d8.jar-to-dex
  192. define create-empty-package-at
  193. define create-empty-package
  194. define initialize-package-file
  195. define add-assets-to-package
  196. define _add-jni-shared-libs-to-package-per-abi
  197. define add-jni-shared-libs-to-package
  198. define add-dex-to-package
  199. define add-dex-to-package-arg
  200. define add-java-resources-to
  201. define add-jar-resources-to-package
  202. define sign-package
  203. define sign-package-arg
  204. define align-package
  205. define compress-package
  206. define remove-timestamps-from-package
  207. define uncompress-dexs
  208. define uncompress-shared-libs
  209. define transform-host-java-to-package
  210. define transform-host-java-to-dalvik-package
  211. define copy-one-header
  212. define copy-one-file
  213. define copy-and-uncompress-dexs
  214. define copy-many-files
  215. define copy-xml-file-checked
  216. define copy-file-to-target
  217. define copy-file-to-target-with-cp
  218. define copy-file-to-target-strip-comments
  219. define copy-file-to-new-target
  220. define copy-file-to-new-target-with-cp
  221. define transform-prebuilt-to-target
  222. define transform-prebuilt-to-target-strip-comments
  223. define copy-files-with-structure
  224. define symlink-file
  225. define _symlink-file
  226. define dexpreopt-copy-jar
  227. define dexpreopt-remove-classes.dex
  228. define hiddenapi-copy-dex-files
  229. define hiddenapi-copy-soong-jar
  230. define transform-jar-to-proguard
  231. define transform-jar-to-proguard
  232. define transform-jar-to-dex-r8
  233. define transform-generated-source
  234. define assert-max-image-size
  235. define add-radio-file
  236. define add-radio-file-internal
  237. define add-radio-file-checked
  238. define add-radio-file-checked-internal
  239. define inherit-package
  240. define inherit-package-internal
  241. define set-inherited-package-variables
  242. define keep-or-override
  243. define set-inherited-package-variables-internal
  244. define check-api
  245. define if-build-from-source
  246. define if-build-from-source
  247. define include-if-build-from-source
  248. define get-prebuilt-src-arch
  249. define record-module-type
  250. define compatibility_suite_dirs
  251. define create-suite-dependencies
  252. define _clean-path-strip-dotdot
  253. define _clean-path-strip-root-dotdots
  254. define _clean-path-expanded
  255. define clean-path
  256. define my_test
  257. define try-validate-path-is-subdir
  258. define validate-path-is-subdir
  259. define try-validate-paths-are-subdirs
  260. define validate-paths-are-subdirs
  261. define test-validate-paths-are-subdirs
  262. define jacoco-class-filter-to-file-args
  263. define jacoco-validate-file-args
  264. define append_enforce_rro_sources
  265. define generate_all_enforce_rro_packages
  266. define has-system-sdk-version
  267. define get-numeric-sdk-version

main.mk中另一个重要的部分就是定义了一系列的规则,这些规则的目标就是编译要生成的目标文件:

 

  1. # This is the default target. It must be the first declared target.
  2. .PHONY: droid
  3. DEFAULT_GOAL := droid
  4. $(DEFAULT_GOAL): droid_targets
  5. ......
  6. .PHONY: ramdisk
  7. ramdisk: $(INSTALLED_RAMDISK_TARGET)
  8. .PHONY: systemtarball
  9. systemtarball: $(INSTALLED_SYSTEMTARBALL_TARGET)
  10. .PHONY: boottarball
  11. boottarball: $(INSTALLED_BOOTTARBALL_TARGET)
  12. .PHONY: userdataimage
  13. userdataimage: $(INSTALLED_USERDATAIMAGE_TARGET)
  14. ifneq (,$(filter userdataimage, $(MAKECMDGOALS)))
  15. $(call dist-for-goals, userdataimage, $(BUILT_USERDATAIMAGE_TARGET))
  16. endif
  17. .PHONY: userdatatarball
  18. userdatatarball: $(INSTALLED_USERDATATARBALL_TARGET)
  19. .PHONY: cacheimage
  20. cacheimage: $(INSTALLED_CACHEIMAGE_TARGET)
  21. .PHONY: bptimage
  22. bptimage: $(INSTALLED_BPTIMAGE_TARGET)
  23. .PHONY: vendorimage
  24. vendorimage: $(INSTALLED_VENDORIMAGE_TARGET)
  25. .PHONY: productimage
  26. productimage: $(INSTALLED_PRODUCTIMAGE_TARGET)
  27. .PHONY: systemotherimage
  28. systemotherimage: $(INSTALLED_SYSTEMOTHERIMAGE_TARGET)
  29. .PHONY: bootimage
  30. bootimage: $(MTK_BOOTIMAGE_TARGET)

而编译的默认目标,也就是终极目标droid,逐级依赖,依赖到各个image文件,也就是各个分区imge文件:

 

  1. # Building a full system-- the default is to build droidcore
  2. droid_targets: droidcore dist_files
  3. ......
  4. # Build files and then package it into the rom formats
  5. .PHONY: droidcore
  6. droidcore: files \
  7. systemimage \
  8. $(INSTALLED_BOOTIMAGE_TARGET) \
  9. $(MTK_BOOTIMAGE_TARGET) \
  10. $(INSTALLED_RECOVERYIMAGE_TARGET) \
  11. $(INSTALLED_VBMETAIMAGE_TARGET) \
  12. $(INSTALLED_USERDATAIMAGE_TARGET) \
  13. $(INSTALLED_CACHEIMAGE_TARGET) \
  14. $(INSTALLED_BPTIMAGE_TARGET) \
  15. $(INSTALLED_VENDORIMAGE_TARGET) \
  16. $(INSTALLED_PRODUCTIMAGE_TARGET) \
  17. $(INSTALLED_SYSTEMOTHERIMAGE_TARGET) \
  18. $(INSTALLED_FILES_FILE) \
  19. $(INSTALLED_FILES_FILE_VENDOR) \
  20. $(INSTALLED_FILES_FILE_PRODUCT) \
  21. $(INSTALLED_FILES_FILE_SYSTEMOTHER) \
  22. soong_docs

根据make的编译原理,编译时,先编译终极目标的各个依赖文件,如image文件、bin文件等等,而image文件又依赖许多的库文件、APK文件、资源文件等等,也就会递归触发库文件、APK文件、资源文件等模块的编译。递归编译完依赖文件,最后完成终极目标,这样就完成了整个android源码的构建。当然,main.mk中为了完成整个android的编译,还导入了一系列的mk等等,这里就不一一介绍了,有兴趣的可以自行查看源码。

同样,根据make的编译原理,我们也可以通过make指定的目标来单独编译某个模块,这样可以加快编译速度。例如: make systemimage。

当然,为了进一步加快编译速度,我们还可以利用envsetup.sh中的mm或者mmm函数,在仅需要编译模块自身,不需要编译模块的依赖的情况下,用mm或者mmm编译更快。

我们可以看到,实际上android.mk和Makefile没有明显差别。android.mk实际上只是多了一些方便编译android源码的内置变量和内置函数。android.mk完全遵循Makefile的语法规则,并且它的内置变量和内置函数也是按照Makefile语法进行导入的。最关键的,构建android系统时也是从一个Makefile开始的。所以,我们可以认为android.mk实际上是一种扩展的Makefile,就像GNU make扩展了原始make一样。

接下来我们介绍android.mk的使用实例。

3. android.mk的使用实例

3.1 编译APK

 

  1. LOCAL_PATH:= $(call my-dir)
  2. include $(CLEAR_VARS)
  3. # 源文件,可包含java、aidl文件
  4. LOCAL_SRC_FILES := $(call all-java-files-under, src)
  5. LOCAL_SRC_FILES += src/com/sprd/gallery3d/aidl/IFloatWindowController.aidl
  6. # resource资源文件
  7. LOCAL_RESOURCE_DIR := $(LOCAL_PATH)/res
  8. # AndroidManifest.xml文件
  9. LOCAL_MANIFEST_FILE := $(LOCAL_PATH)/AndroidManifest.xml
  10. # 是否启用AAPT2
  11. LOCAL_USE_AAPT2 := true
  12. LOCAL_MODULE_TAGS := optional
  13. # 目标APK文件名称
  14. LOCAL_PACKAGE_NAME := DreamCamera2
  15. # 需要override的名称
  16. LOCAL_OVERRIDES_PACKAGES := Camera2
  17. # APK的签名
  18. LOCAL_CERTIFICATE := platform
  19. # 是否启用odex优化
  20. LOCAL_DEX_PREOPT := false
  21. # SDK版本
  22. LOCAL_SDK_VERSION := current
  23. # 混淆配置文件
  24. LOCAL_PROGUARD_FLAG_FILES := proguard.flags
  25. # 依赖的java共享库
  26. LOCAL_JAVA_LIBRARIES := android.test.runner
  27. # 依赖的java静态库
  28. LOCAL_STATIC_JAVA_LIBRARIES := zxing
  29. # 依赖的共享库
  30. LOCAL_SHARED_LIBRARIES := libjpeg
  31. # 依赖的静态库
  32. LOCAL_STATIC_LIBRARIES := libjpeg_static_ndk
  33. # 版本号
  34. LOCAL_AAPT_FLAGS := \
  35. --auto-add-overlay \
  36. --version-name "$(version_name_package)" \
  37. --version-code $(version_code_package) \
  38. # 构建APK
  39. include $(BUILD_PACKAGE)

3.2 编译java静态库

 

  1. LOCAL_PATH := $(call my-dir)
  2. include $(CLEAR_VARS)
  3. # 源文件,可包含java、aidl文件
  4. LOCAL_SRC_FILES := $(call all-subdir-java-files)
  5. # 依赖的java静态库
  6. LOCAL_STATIC_JAVA_LIBRARIES := zxing
  7. # 依赖的java动态库
  8. LOCAL_JAVA_LIBRARIES := android.test.runner
  9. # 目标java静态库的名称
  10. LOCAL_MODULE := scan
  11. # 构建java静态库
  12. include $(BUILD_STATIC_JAVA_LIBRARY)

3.3 编译java共享库

 

  1. LOCAL_PATH := $(call my-dir)
  2. include $(CLEAR_VARS)
  3. # 源文件,可包含java、aidl文件
  4. LOCAL_SRC_FILES := $(call all-subdir-java-files)
  5. # 依赖的java共享库
  6. LOCAL_JAVA_LIBRARIES := bouncycastle core-libart ext services.core
  7. LOCAL_MODULE_TAGS := optional
  8. LOCAL_JACK_ENABLED := disabled
  9. # 目标java共享库的名称
  10. LOCAL_MODULE:= security
  11. # 构建java共享库
  12. include $(BUILD_JAVA_LIBRARY)

3.4 编译C/C++静态库

 

  1. LOCAL_PATH:= $(call my-dir)
  2. include $(CLEAR_VARS)
  3. # 目标静态库的名称
  4. LOCAL_MODULE := libgif-ex
  5. # C/C++源文件
  6. LOCAL_SRC_FILES := \
  7. dgif_lib.c \
  8. egif_lib.c \
  9. gifalloc.c \
  10. gif_err.c \
  11. gif_hash.c \
  12. openbsd-reallocarray.c \
  13. quantize.c
  14. LOCAL_CFLAGS += -Wno-format -Wno-sign-compare -Wno-unused-parameter -DHAVE_CONFIG_H
  15. LOCAL_SDK_VERSION := 8
  16. LOCAL_NDK_STL_VARIANT := c++_static
  17. # 构建C/C++静态库
  18. include $(BUILD_STATIC_LIBRARY)

3.5 编译C/C++共享库

 

  1. LOCAL_PATH:= $(call my-dir)
  2. include $(CLEAR_VARS)
  3. # C/C++头文件
  4. LOCAL_C_INCLUDES := \
  5. $(LOCAL_PATH)/include \
  6. $(TOPDIR)system/core/include
  7. # C FLAG
  8. LOCAL_CFLAGS := -O3 -DNDEBUG
  9. # LDFLAG
  10. LOCAL_LDFLAGS := -llog
  11. # 依赖的共享库
  12. LOCAL_SHARED_LIBRARIES := \
  13. libutils \
  14. myutils
  15. # 依赖的静态库
  16. LOCAL_STATIC_LIBRARIES := libjpeg_static_ndk
  17. # C/C++源文件
  18. LOCAL_SRC_FILES := \
  19. main.cpp \
  20. utils.cpp
  21. # 目标共享库的名称
  22. LOCAL_MODULE := mylib
  23. LOCAL_MODULE_TAGS := optional
  24. # 构建共享库
  25. include $(BUILD_SHARED_LIBRARY)

4. 用于预置的android.mk

4.1 预置jar

方法一:

 

  1. LOCAL_PATH:= $(call my-dir)
  2. # 预置
  3. include $(CLEAR_VARS)
  4. LOCAL_PREBUILT_STATIC_JAVA_LIBRARIES := zxing:libs/core.jar \
  5. gson:libs/gson-2.8.0.jar \
  6. android-support-v7:libs/android-support-v7-recyclerview.jar
  7. include $(BUILD_MULTI_PREBUILT)
  8. # 引用
  9. include $(CLEAR_VARS)
  10. ......
  11. LOCAL_STATIC_JAVA_LIBRARIES += zxing
  12. LOCAL_STATIC_JAVA_LIBRARIES += gson
  13. LOCAL_STATIC_JAVA_LIBRARIES += android-support-v7
  14. ......
  15. include $(BUILD_PACKAGE)

方法二:

 

  1. LOCAL_PATH := $(call my-dir)
  2. # 预置
  3. include $(CLEAR_VARS)
  4. LOCAL_MODULE_CLASS := JAVA_LIBRARIES
  5. LOCAL_MODULE := commons-io
  6. LOCAL_SDK_VERSION := current
  7. LOCAL_SRC_FILES := ../../../../../../../../prebuilts/tools/common/m2/repository/commons-io/commons-io/2.4/commons-io-2.4.jar
  8. LOCAL_UNINSTALLABLE_MODULE := true
  9. include $(BUILD_PREBUILT)
  10. # 引用
  11. include $(CLEAR_VARS)
  12. ......
  13. LOCAL_STATIC_JAVA_LIBRARIES += commons-io
  14. ......
  15. include $(BUILD_PACKAGE)

4.2 预置so

方法一:

 

  1. LOCAL_PATH:= $(call my-dir)
  2. # 预置
  3. include $(CLEAR_VARS)
  4. LOCAL_MODULE := mylib2
  5. LOCAL_SRC_FILES_32 := lib/armeabi-v7a/mylib2.so
  6. LOCAL_SRC_FILES_64 := lib/arm64-v8a/mylib2.so
  7. LOCAL_MODULE_TAGS := optional
  8. LOCAL_MODULE_CLASS := SHARED_LIBRARIES
  9. LOCAL_MODULE_SUFFIX := .so
  10. LOCAL_PROPRIETARY_MODULE := true
  11. LOCAL_MULTILIB := both
  12. include $(BUILD_PREBUILT)
  13. ......
  14. # 引用
  15. include $(CLEAR_VARS)
  16. LOCAL_SHARED_LIBRARIES += mylib2
  17. include $(BUILD_PACKAGE)
  18. ......

方法二:

 

  1. LOCAL_PATH:= $(call my-dir)
  2. # 预置
  3. include $(CLEAR_VARS)
  4. ......
  5. ifeq ($(strip $(TARGET_ARCH)), arm64)
  6. LOCAL_PREBUILT_JNI_LIBS := libs/arm64-v8a/mylib3.so
  7. else ifeq ($(strip $(TARGET_ARCH)), x86_64)
  8. LOCAL_PREBUILT_JNI_LIBS := libs/x86_64/mylib3.so
  9. else ifeq ($(strip $(TARGET_ARCH)),arm)
  10. LOCAL_PREBUILT_JNI_LIBS := libs/armeabi-v7a/mylib3.so
  11. else
  12. LOCAL_PREBUILT_JNI_LIBS := libs/x86/mylib3.so
  13. endif
  14. ......
  15. include $(BUILD_PACKAGE)

4.3 预置APK

 

  1. LOCAL_PATH := $(call my-dir)
  2. include $(CLEAR_VARS)
  3. # 模块名称
  4. LOCAL_MODULE := NewGallery2
  5. # 要覆盖掉的模块名称
  6. LOCAL_OVERRIDES_PACKAGES := Gallery Gallery3D GalleryNew3D Gallery2 DreamGallery2
  7. # 模块类型为APPS
  8. LOCAL_MODULE_CLASS := APPS
  9. # 允许使用系统隐藏接口
  10. LOCAL_PRIVATE_PLATFORM_APIS := true
  11. # 签名,如无需重签名,则直接设置为PRESIGNED使用已有签名;需要重签,则设置为对应签名的值。
  12. LOCAL_CERTIFICATE := platform
  13. # 目标编译后的输出目录
  14. LOCAL_MODULE_PATH := $(TARGET_OUT)/priv-app
  15. # APK文件
  16. LOCAL_SRC_FILES := apk/NewGallery2.apk
  17. # APK预置的so
  18. ifeq ($(strip $(TARGET_ARCH)), arm64)
  19. LOCAL_PREBUILT_JNI_LIBS := libs/arm64-v8a/libjni_jpeg.so
  20. else ifeq ($(strip $(TARGET_ARCH)), x86_64)
  21. LOCAL_PREBUILT_JNI_LIBS := libs/x86_64/libjni_jpeg.so
  22. else ifeq ($(strip $(TARGET_ARCH)),arm)
  23. LOCAL_PREBUILT_JNI_LIBS := libs/armeabi-v7a/libjni_jpeg.so
  24. else
  25. LOCAL_PREBUILT_JNI_LIBS := libs/x86/libjni_jpeg.so
  26. endif
  27. include $(BUILD_PREBUILT)

5. 结语

在掌握了Makefile基础知识以及结合源码理清楚了android.mk之后,就不会觉得.mk文件那么陌生了。尤其是在理解了android.mk的内置变量和内置函数之后,我们在编写.mk文件就基本可以得心应手了。

由于make在编译android系统源码时表现出效率不够高的问题,google后来在7.0及以上版本引进了速度更快的ninja,中文意思是忍者,ninja对应的配置文件是android.bp。但google即便引进了ninja,源码中还是保留了部分android.mk。并且android.mk也可以通过kati工具转换成ninja,也就是说android.mk还可以继续使用。所以,make仍然是android开发者的必备技能。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Monodyee/article/detail/221688
推荐阅读
相关标签
  

闽ICP备14008679号