`
isiqi
  • 浏览: 16047137 次
  • 性别: Icon_minigender_1
  • 来自: 济南
社区版块
存档分类
最新评论

Android Building System 分析

 
阅读更多

想要了解一個系統,我常會從 makefile 或是 building system 下手, 以了解系統組成元素為何? 目錄結構為何? 對於 Android ,我也不例外。 透過了解 building system ,我們能知道如何新增、修改、刪除程式, 並保有完整性,順利編譯出結果。

設定檔

Android building system 包括幾種重要的設定檔,

  • Android .mk
  • AndroidProducts.mk
  • target_<os>-<arch>.mk, host_<os>-<arch>.mk and <os>-<arch>.mk
  • BoardConfig.mk
  • buildspec.mk

Android .mk 是 module 和 package 的設定檔,每個 module/package 的目錄下都會有一個 Android .mk。 所謂的 module 是指系統的 native code ,相對於用 Java 寫成的 Android application 稱為 package。

AndroidProducts.mk 則設定 product 配置。 product 即特定 系統版本,透過編譯不同 product ,產生不同軟體配置內容,安裝不同的 application。 Product 可視為特定專案,產生特定規格系統。

BoardConfig.mk 是為 product 主板做設定,像是 driver 選擇、 設定。*<os>-<arch>.mk 則是針對選擇的作業系統和 CPU 架構,進行相關設定。

buildspec.mk 是位於 source 根目錄下,為進行編譯者所做之額外設定。 例如,可在此選擇要產生的 product 、平台、額外的 module/package 等。

參數

build/envsetup.sh 實作一個 mm 指令,以編譯單一 module,不需編譯整個 source tree。ONE_SHOT_MAKEFILE 這個 makefile 變數/參數就是用以實作這個功能。使用 方法是在執行 make 時,將該變數指定為 module 的 Android .mk。

  • make ONE_SHOT_MAKEFILE=<path to Androiod.mk>

透過定義 CREATE_MODULE_INFO_FILE , building system 會將所有 module 資訊 列在 $(PRODUCT_OUT)/module-info.txt 檔案裡。

  • make CREATE_MODULE_INFO_FILE=true

設定 BUILD_TINY_ANDROID=true , building system 產生一個簡單的 image , 以測試硬體的可用度。此功能用於移植的早期階段,以快速 bring up 。

HOST_BUILD_TYPE 和 TARGET_BUILD_TYPE 指定 building system 產生 binary 的目的為 debug 或 release 。透過設定此二變數,能產生包含 debug information 的 binry 。

  • debug
  • release

這些參數,也可設於 buildspec.mk 裡,以避免開發過程不斷的重新指定。

Goals

一般編輯整個 Android 系統,就是使用 droid 這個 goal。 droid 會產生一個 完整的系統,包括 bootloader、kernel、系統程式、模組和應用 程式。

showcommands 和 droid 功能相同,但 droid 在編譯過程不顯示所使用的指令。 透過 showcommands 這個 goal, building system 顯示過程中每一個步驟的詳細 指令。

Makefile 的流程

  • 初始化相關變數
  • 偵測編譯環境和目標環境
  • 決定目標 product
  • 讀取 product 的設定
  • 讀取 product 所指定之目標平台架構設定
    • 選擇 toolchain
    • 指定編譯參數 (*<os>-<arch>.mk)
  • 清除輸出目錄
  • 設定/檢查版本編號
  • 讀取所有 BoardConfig.mk 檔案
  • 讀取所有 module 的設定
  • 根據設定,產生必需的 rule
  • 產生 image

以上的主要流程都是由 build/core/main.mk 所安排。

初始化和偵測

由 build/core/config.mk 所進行。 build/core/envsetup.mk 檢查 developer 的設定 (buildspec.mk) , 並檢查執行環境,以決定輸出目錄、項目。

build/core/config.mk 本身還依據參數,決定解譯時的相關參數。像是 compiler 的路徑、flags, lex 、yacc 的路徑參數等。

關於 product 的相關設定,則是由 build/core/product_config.mk 所處理, 使用 build/core/product.mk 提供之 macro 載入。根據 AndroidProduct.mk 的內容, product_config.mk 決定了

  • PRODUCT_TAGS
  • OTA_PUBLIC_KEYS
  • PRODUCT_POLICY
  • ......

Product 設定的讀取

Android product 的設定來自於 build/target/product/AndroidProduct.mk 和 vendor 子目錄下的 AndroidProduct.mk 。 building system 透過 find 指令, 找出所有可能的 AndroidProduct.mk。 AndroidProduct.mk 裡定義 PRODUCT_MAKEFILES 變數,列舉所有實際定義 product 的 makefile。 這些 makefile 各自定義獨立的 product 。product 相關參數,存成 PRODUCTS.<path of makefile>.<variable> 形式的變數。並將 makefile 路徑 存在 PRODUCTS 變數。因此,透過 PRODUCTS 能取得所有的 product 路徑/名稱, 並透過 PRODUCTS.<path of makefile>.<variable> 形式的變數取得內容。

Module 設定的讀取

Module 是指 native code 的軟體元件,而 Java application 則被稱為 package。 build/core/definitions.mk 定義 module/package 相關 macro ,讀取、檢查 module/package 定義檔;分散 source tree 各處的 Android .mk 檔案。 build/core/main.mk 使用 find 指令,在這些子目錄下找出所有 Android .mk , 並將路徑存在 subdir_makefiles 變數裡。最後,include 這些檔案。

這些 Android .mk 會 include 定義成變數 BUILD_SHARED_LIBRARY 、BUILD_PACKAGE 等, 和其目的相配的 makefile。這些 makefile 會變 Android .mk 定義之內容,存成 ALL_MODULES.<path of Android .mk>.<variable> 形式。例如, Android .mk 定義了 LOCAL_MODULE_SUFFIX ,變會存成 ALL_MODULES.<path of Android .mk>.LOCAL_MODULE_SUFFIX 。而 Android .mk 路徑, 當樣會存於 ALL_MODULES 變數裡。

Search Android .mk 的路徑,基本上會是整個 source tree 。但會依特定的 goal , 選擇性只找尋特定目錄。例如 SDK 只需特定目錄下的 Android .mk 。

Board Level 設定

和目標平台主板相關之設定,例如使用了什麼裝置、driver 等,或是是否需要編譯 bootloader 、 kernel 等,都是在 BoardConfig.mk 裡設定。同樣,每張主板 可以有不同設定,存在不同目錄下的 BoardConfig.mk ,以 find 尋找如下檔案:

  • build/target/board/$(TARGET_DEVICE)/BoardConfig.mk
  • vendor/*/$(TARGET_DEVICE)/BoardConfig.mk

TARGET_DEVICE 是 product 所定義,因此同一個 BoardConfig.mk 可被多個 product 所使用。一個 TARGET_DEVICE ,通常只有一個 BoardConfig.mk 。 BoardConfig.mk 會被直接 include 到 building system 的 name space 裡。 因此,一些 module 的 enable/disable ,可以在 BoardConfig.mk 以對映不同的 主板。

Rules

在 module 的定義檔 Android .mk 裡,可定義 module 的 tag, LOCAL_MODULE_TAGS, 以分類這些 module。 每一個 product 可以指定需要的 tag (PRODUCT_TAGS),使 building system 只編譯標示這些 tag 的 module。在 build/core/main.mk 裡,所有標示特定 tag 的 module 收集為 ALL_DEFAULT_INSTALLED_MODULES ,並 include build/core/Makefile 處理。

build/core/Makefile 為這些 module 產生 rule ,並使產生 image 的 goal depend on 這些 rule ,使這些 module 被編譯。

結論

Android 的 building system 其實不是那麼複雜。在了解之後,也不是那麼難修改。 但, GNU make 的一些語法,所 building system 使用一些不是那麼直覺的用法, 使的 building system 較難了解。但,花點心思就能克服。

分享到:
评论

相关推荐

    Gradle Recipes for Android Master the New Build System for Android pdf 0分

    Gradle Recipes for Android Master the New Build System for Android 英文pdf

    Embedded Programming with Android: Bringing Up an Android System from Scratch

    Step by step, he guides you through building a system with all the key components Android hardware developers must deliver to manufacturing. By the time you’re done, you’ll have the key programming...

    Learning.Java.by.Building.Android.Games

    Android is the fastest growing operating system (OS) with one of the largest installed bases of any mobile OS. Android uses one of the most popular programming languages, Java, as the primary language...

    Building Android Apps with HTML, CSS, and JavaScript

    This book received valuable community input through O’Reilly’s Open Feedback Publishing System (OFPS). Learn more at http://labs.oreilly.com/ofps.html. CSDN上见到过这个资源但无法下载,自己上传一个

    Building Android Apps with HTML,CSS,and.JavaScript(2nd,2012.01)

    Discover why device-agnostic mobile apps are the wave of the future, and start building apps that offer greater flexibility and a broader reach. Learn the basics for making a web page look great on...

    Learning Android

    If you're new to the Android mobile operating system, Learning Android is the perfect way to master the fundamentals. Written by an expert who's taught Android to thousands of people in large ...

    Android Platform Developer‘s Guide.doc

    Android Build System 4 In this document 4 Building the Android Platform 6 Building the Android Kernel 8 Build Variants 9 Configuring a New Product 10 In this document 10 Detailed Instructions 11 New ...

    Android.SQLite.Essentials.1783282959

    Android SQLite Essentials focuses on the core concepts behind building database-driven applications. This book covers the basic and advanced topics with equivalent simplicity and detail, in order to ...

    Introduction.to.Android.Application.Development(4th,2013.12) pdf

    Building Your First Android Application 68 Creating and Configuring a New Android Project 69 Core Files and Directories of the Android Application 73 Creating an AVD for Your Project 75 Creating a ...

    Android programming for beginners

    Book Details ISBN 139781785883262 Paperback 698 pages Book Description ...By the end of this book, you’ll be ready to start building your own custom applications in Android and Java.

    App.Inventor.2.Building.Android.Apps.B0193RHQG6

    App Inventor 2 Building Android Apps takes you step-by-step through the whole process of designing and creating your first two android apps using the free MIT App Inventor 2 software. The book is ...

    Beginning.Android.5th.Edition.1430246863

    Google’s Android operating-system has taken the industry by storm, going from its humble beginnings as a smartphone operating system to its current status as a platform for apps that run across a ...

    Mastering Android Game Development with Unity

    You will begin with the basic concepts of Android game development, a brief history of Android games, the building blocks of Android games in Unity 5, and the basic flow of games. You will configure ...

    Learning Android: Develop Mobile Apps Using Java and Eclipse(第二版)

    Alarms and System Services Broadcasting Intents Summary Chapter 14 App Widgets Using Content Providers Through Widgets Summary Chapter 15 Networking and Web Overview Quick Example Networking Basics ...

    Mastering.Android.Game.Development.1783551771

    Learn the internals of a game engine by building one A succinct, hands-on guide to enhance your game development skills with Android SDK Who This Book Is For If you are an intermediate-level Android ...

    Pro OpenGL ES For Android

    In Pro OpenGL ES for Android, you'll find out how to harness the full power of OpenGL ES, and design your own 3D applications by building a fully-functional 3D solar system model using Open GL ES!...

    Android.6.Essentials.1785884417

    Android 6 is the latest and greatest version of the Android operating system, and comes packed with cutting edge new features for you to harness for the benefit of building better applications. ...

    Pro OpenGL ES for Android

    In Pro OpenGL ES for Android, you'll find out how to harness the full power of OpenGL ES, and design your own 3D applications by building a fully-functional 3D solar system model using Open GL ES!...

    Exploring SE for Android 无水印书签修正版pdf

    Their positions range from hobbyists wanting to secure their Android powered creations to OEM engineers building handsets to engineers of emerging areas where Android is seeing growth. What You Will...

Global site tag (gtag.js) - Google Analytics