发现一个写得很简洁的 AOSP 的 build system 的介绍,很适合初学Android 的同学。 英文的 http://www.jayway.com/2012/10/24/a-practical-approach-to-the-aosp-build-system/

原文拷贝如下。

A practical approach to the AOSP build system

Introduction

The Android open-source project (AOSP) is quite complex and it can be hard to find a good way to get more familiar with it. I’m going to try a practical approach, by adding an Android application to the system image, while at the same time describing the relevant parts of the build process. As you might already know, the Android source is built using make, and this blog post assumes some knowledge in how to write makefiles.

If you want to follow this guide, start by setting up the development environment according to the AOSP web site:

http://source.android.com/source/index.html

After setting up the environment, finish a full build for the emulator (replacing -j5 with a value suitable for your computer):

1 2 3

source build/envsetup.sh
lunch 1
make -j5
view raw build_platform.sh hosted with ❤ by GitHub

Now, lets have a look at the steps involved.

envsetup.sh

When building, you usually start with sourcing the file build/envsetup.sh to setup your build environment. It basically adds a lot of shell commands to your bash environment, e.g:

  • hmm() – print short help menu
  • lunch() – prints lunch menu (available build targets)
  • add_lunch_combo() – adds a new entry to the lunch menu
  • mm() -  Builds all of the modules in the current directory

It also sources any vendorsetup.sh files found under vendor/*/vendorsetup.sh, vendor/*/*/vendorsetup.sh and device/*/*/vendorsetup.sh, which allows vendors to add their own products to the lunch menu using the add_lunch_combo function. An example of the a vendorsetup.sh is the Samsung Maguro product directory (device/samsung/maguro):

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17

#
# Copyright (C) 2011 The Android Open Source Project
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
add_lunch_combo full_maguro-userdebug
view raw vendorsetup.sh hosted with ❤ by GitHub

You can try calling one of the commands after sourcing envsetup.sh:

1

hmm
view raw call_hmm.sh hosted with ❤ by GitHub

lunch

Next thing is to select target using the lunch menu, which was added to your bash shell environment after sourcing envsetup.sh. After making your selection, the chosen product and variant is verified and environment variables are set, including:

  • export TARGET_PRODUCT=$product – The chosen product
  • export TARGET_BUILD_VARIANT=$variant – The chosen variant
  • export TARGET_BUILD_TYPE=release – Only release type is available. Use choosecombo if you want to select type.
  • export ANDROID_BUILD_TOP=$(gettop) – The build  root directory.
  • export ANDROID_TOOLCHAIN=... – The toolchain directory for the prebuilt cross-compiler matching the target architecture
  • export PATH=... – Among other stuff, the prebuilt toolchain is added to PATH.
  • export ANDROID_PRODUCT_OUT=... – Absolute path to the target product out directory
  • export ANDROID_HOST_OUT=... – Absolute path to the host out directory

Check out some of the variables by typing:

1

echo $ANDROID_BUILD_TOP
view raw check_env_variables.sh hosted with ❤ by GitHub

Building the platform

You previously finished a platform build by running the following commands:

1 2 3

source build/envsetup.sh
lunch 1
make -j5
view raw build_platform.sh hosted with ❤ by GitHub

Depending on the type of build you selected, the result can be varying. However, a typical build results in the following images:

  • boot.img – Native system boot image.
  • ramdisk.img – Ramdisk rootfs.
  • recovery.img – Recovery image.
  • ramdisk-recovery.img – Ramdisk rootfs for Recovery.
  • system.img – System data (/system directory)
  • userdata.img – User data (/data directory)

These images is what makes up the Android system, with one exception. The Android kernel is not a part of the AOSP, and must be built separately. We will get back to this later…

Building a module

Now, in order to dig a little deeper into the build system, navigate to the Music application in packages/apps/Music and build it using mm:

1 2

cd $ANDROID_BUILD_TOP/packages/apps/Music
mm
view raw build_module.sh hosted with ❤ by GitHub

The main makefile for this module is called Android.mk and is located in $ANDROID_BUILD_TOP/packages/apps/Music:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18

LOCAL_PATH:= $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE_TAGS := optional
LOCAL_SRC_FILES := $(call all-java-files-under, src) \
src/com/android/music/IMediaPlaybackService.aidl
LOCAL_PACKAGE_NAME := Music
LOCAL_SDK_VERSION := current
LOCAL_PROGUARD_FLAG_FILES := proguard.flags
include $(BUILD_PACKAGE)
# Use the folloing include to make our test apk.
include $(call all-makefiles-under,$(LOCAL_PATH))
view raw music_app.mk hosted with ❤ by GitHub

The make file sets a few module specific build variables and includes the BUILD_PACKAGE variable to tell make how to build this particular module. The BUILD_PACKAGE variable is defined in a file called config.mk located at $ANDROID_BUILD_TOP/build/core/config.mk. Lets have a look at the relevant parts of it:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29

...
# ###############################################################
# Build system internal files
# ###############################################################
BUILD_COMBOS:= $(BUILD_SYSTEM)/combo
CLEAR_VARS:= $(BUILD_SYSTEM)/clear_vars.mk
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:= $(BUILD_SYSTEM)/package.mk
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
...
view raw config.mk hosted with ❤ by GitHub

As you can see, there are several build variables defined in this file. They basically references a separate make file which handles that particular type of build. For example, the BUILD_PACKAGE variable refers to the file $ANDROID_BUILD_TOP/build/core/package.mk. Other build notable build variables are:

  • CLEAR_VARS – Includes the file $ANDROID_BUILD_TOP/build/core/clear_vars.mk. Clears all LOCAL_* variables (with the exeption of LOCAL_PATH) to ensure that any previously built modules does not affect the current module.
  • BUILD_PACKAGE - Includes the file $ANDROID_BUILD_TOP/build/core/package.mk. Defines how packages (*.apk) are built. The build is controlled by setting LOCAL_* variables as seen in the Music application example above.
  • BUILD_PREBUILT - Includes the file $ANDROID_BUILD_TOP/build/core/prebuilt.mk. Defines how pre-built modules should be handled, e.g. a pre-compiled jar file.
  • BUILD_JAVA_LIBRARY - Includes the file $ANDROID_BUILD_TOP/build/core/java_library.mk. Defines how shared java libraries should be built.
  • BUILD_STATIC_JAVA_LIBRARY - Includes the file $ANDROID_BUILD_TOP/build/core/static_java_library.mk. Defines how static java libraries should be built. Includes java_library.mk as well.
The result of the build can be found in the target output directory, e.g.
$ANDROID_BUILD_TOP/out/target/product/<product name>/APPS

A closer look at the core makefiles

How does the build system know what to build? The answer to this question is fairly complex and involves lots of build files, but a very simplistic view of the process can be seen in the image below. Most of the involved files have been abstracted away to get a feeling for how a build is triggered.

When invoking make in the build root after setting up the environment (sourcing envsetup.sh and calling lunch), make finds the default Makefile in the build root, which in turn includes the main makefile in build/core/main.mk. The main makefile includes lots of other makefiles based on what make target. Assuming the default target, three of the more important ones are highlighted in the image below: config.mk, definitions.mk and Makefile.

Very simplistic view of the build system

Short description of what the different makefiles actually do:

  • main.mk

    • Verify correct java version
    • Setup build version (user, userdebug, eng)
    • Fnd all Android.mk files in the platform build
    • Filter out what Android.mk files (modules) to actually include in build
  • config.mk
    • Setup the internal build system files as described earlier (CLEAR_VARS, etc.)
    • Find the board config file (BoardConfig.mk) based on lunch selection.
    • Sanity check the board config file
    • Setup tools, e.g. aapt, proguard, flex, etc.
  • definitions.mk
    • Define LOTS of make macros, e.g. my-dir, all-subdir-makefiles, add-prebuilt-file, etc.
    • Macros are later used in the other build files.
  • Makefile
    • Define target variables for e.g. recovery image, system image, ramdisk image, ota package, etc.

As described earlier, each module defines its own Android.mk file, specifying the type of module by calling the correct build file for that module, e.g. $(BUILD_PACKAGE) for building an APK.

Summary

This was a high level overview of the Android build system based on the Jellybean source code. To make the blog post relatively short, it barely touches many important parts such as board and product definitions, module makefiles and target output. In the following post, I will show how to add an APK to the system image.

AOSP build 系统简介相关推荐

  1. 了解一下,Android 10 Build系统

    源起 因工作原因不得不重新抄起Android源代码开始看.这次就直接上Android 10.0了.当把代码导入Source Insight后,感慨万千.我一度觉得对TA的熟悉简直超过对自己的身体... ...

  2. 第2章:Android的编译环境--build系统

    2.0 build简介 Android的build系统基于GNU Make 和shell 构建的一套编译环境.这套系统定义了大量的变量和函数,无论编写一个产品的配置文件还是一个模块的Android.m ...

  3. 监控系统简介(二):使用 App Metrics 在 ASP.NET Web API 中记录指标

    回顾 在<监控系统简介:使用 Prometheus 与 Grafana>一文中,我们了解了什么是监控系统,Prometheus 这一监控工具及它提供的数据类型.PromQL 以及 Graf ...

  4. 学习AOSP安卓系统源代码,需要什么样的电脑?不同配置的电脑,其编译时间有多大差距?

    文章目录 1. 卷首语 2.中低配置(6000元价位) 2.1 系统配置 2.2 编译配置 2.3 编译时间 (约200分钟) build completed successfully (03:23: ...

  5. OpenWRT系统简介

    1.OpenWRT系统简介¶ 由于工作需要接触到OpenWRT系统,发现目前这方面的资料不是很完善,要么是单纯理论的说明性的资料,要么是具体问题解决过程记录的资料,对于初学者都不具被借鉴意义.我认为好 ...

  6. 【译】Android系统简介—— Activity

    续上一篇,继续介绍Android系统.上一篇: [译]Android系统简介 本文主要介绍构建Android应用的一些主要概念: Activity Activity是应用程序中一个单独的有UI的页面( ...

  7. 理解 Android Build 系统

    Android Build 系统是用来编译 Android 系统,Android SDK 以及相关文档的一套框架.众所周知,Android 是一个开源的操作系统.Android 的源码中包含了许许多多 ...

  8. PowerPC中断系统简介(一)

    1. PowerPC中断系统简介 PowerPC处理器的中断系统由两部分组成,一是内核的中断及异常的处理:二是中断控制器.以P2020处理器为例,包括E500内核中断及异常处理系统和OpenPIC中断 ...

  9. Google Apps – Framework, Phonesky, GmsCore w/ AOSP Build.

    Useful links: Android.mk File syntax specification AOSPforNexus5 ---------– 1.) The purpose of this ...

  10. QuickWAP 2005企业WAP网站系统简介

    QuickWAP 2005企业WAP网站系统简介 中国被爱可以在线于2006年4月1日发布了QuickWAP 2005,软件中包含了一套"企业WAP网站系统",该系统完全基于Qui ...

最新文章

  1. 深入解析Vue组件间通信
  2. 解决struts.xml文件提示问题
  3. OS - MMAP初探
  4. java kafka 多线程消费
  5. 火狐浏览器设置_[教程] 在谷歌浏览器和火狐浏览器里配置DoH加密DNS流量提高安全性...
  6. java 整数相等,为什么Java不会看到整数是相等的?
  7. java clone()用法_java clone方法使用详解(转)
  8. 速达5000维护服务器,财务软件速达5000为什么连不上服务器
  9. 计算机无法安装网卡驱动,如何用驱动人生解决win10网卡驱动问题
  10. net stop sharedaccess命令 2009-1-15 11:10
  11. CSDN 博客添加无水印图片的方法
  12. 微型计算机原理姚向华答案,微型计算机原理试题答案-微型计算机原理姚向华.doc...
  13. 雅典娜暴利烹饪系列(下)
  14. 编写函数求区间[200,3000]中所有的回文数, 回文数是正读和反读都是一样的数
  15. LeetCode/LintCode 题解丨一周爆刷字符串:独特的摩尔斯编码
  16. 小程序遇到回车换行怎么处理
  17. 麦咭萌app送智伴机器人_模拟麦咭机器人软件 麦咭机器人官网
  18. 什么是SPAM行为?
  19. linux pppoe 拨号日志,PPPoe宽带拨号
  20. C语言实验设备预约管理系统

热门文章

  1. linux磁盘写保护怎么修改_linux
  2. EditPlus实现json格式化
  3. mysql workbench 安全模式_MySQL Workbench的安全模式解决
  4. 微信小程序 自定义组件之《转盘》
  5. MongoDB安装Python操作MongoDB
  6. 动力节点_JDBC学习笔记详解【源代码分析】
  7. php无版权图库api,12个无版权限制的大图特供网站_交互设计教程
  8. 从亏损19亿到盈利6亿,恺英网络做对了什么?
  9. 【数学】通俗解释布丰投针实验过程、蒙特卡洛方法及python仿真代码
  10. 统一身份认证(CAS)中文文档 请多指教