1. 程式人生 > >Android 源碼編譯之旅

Android 源碼編譯之旅

fin checkout 寫敏感 undefine -m nvi gen 保存 需要

目錄

  • 前言
    • 背景
    • 安裝軟件
  • 正文
    • Mac 分區
    • 移動硬盤分區
    • Repo 下載源碼
    • 編譯
    • 源碼導入 Android Studio 查看
  • 碰到的問題
    • Could not find a supported mac sdk
    • Undefined symbols for architecture i386
  • 結語
  • 致謝

前言

背景

工作需要,想著剛好有個趁手的兵器(Mac),雖然做好充足的功課,但是這場編譯之旅還是十分曲折

本文主要記錄本人使用macOs( 10.14) 在本地 checkout 出 AOSP ,並進行編譯,導入 Android Studio 的完整經歷。

安裝軟件

  1. XCode : 編譯源碼需要
  2. MacOSX-SDK (10.13) : 編譯需要。本機是10.14 版本,有問題,詳細見碰到的問題二,
  3. JDK : 一般都有,沒有要裝下
  4. MacPorts : 下載工具,用來下載編譯源碼需要的軟件

正文

Mac 分區

為何分區:

  1. Linux 文件名大小寫敏感,
  2. mac 默認文件系統大小寫不敏感,編譯系統會報錯

如何分區:

  1. 磁盤工具(Disk Utility)-> 分區 -> 二次確認分區 -> 點擊加號,選擇大小和格式 -> 點擊應用開始分區
  2. 新的分區在 /Volimes 目錄下
  3. 因為 android-7.1.1_r6 22.12G, 編譯完成占用 67.4G, 因此選擇 80G
  4. 判斷當前shell : echo $0 -> -bash

移動硬盤分區

  1. 購入移動硬盤,抹掉數據,改成 ExFat 格式
  2. 使用命令行進行 250G 分區
hdiutil create -type SPARSE -fs 'Case-sensitive Journaled HFS+' -size 250g ~/android.dmg
  1. 雙擊新加 android.img.sparseimage ,將其掛載

Repo 下載源碼

  1. repo: 方便 checkout Android 源碼這種眾多 git 項目匯聚的大項目
  2. 命令
//下載repo
$ mkdir ~/bin
$ PATH=~/bin:$PATH
$ curl https://storage.googleapis.com/git-repo-downloads/repo > ~/bin/repo
$ chmod a+x ~/bin/repo

//初始化 repo,使用清華鏡像
$ repo init -u https://aosp.tuna.tsinghua.edu.cn/platform/manifest -b android-9.0.0_r3

// 下載源碼,開啟4個並發
$ repo sync -j4
  1. 也可替換成國內鏡像,比如清華、中科
  2. 下載時間長,使用國內鏡像還是蠻快的,應該是4,5個小時吧
  3. repo 下載報超時。repo 是對 git 的封裝,給 git 設置代理
  4. 可以修改 mac 節能設置,防止休眠。系統設置 -> 節能 -> 勾選“防止進入睡眠”

編譯

  1. 設置文件描述符限制
$ ulimit -S -n 1024
  1. 編譯個系統瀏覽器 apk
// 清除緩存
make clobber
// 初始化環境
source build/envsetup.sh 
// 打包瀏覽器
mmm packages/apps/Browser2/

// or 自動生成依賴
mmma packages/apps/Browser2/
  1. 編譯源碼
//根目錄下執行,環境設置命令 
source build/envsetup.sh 
// 選擇編譯的源碼類型
lunch
// 可以選擇1. aosp_arm-eng 或2. aosp_arm64-eng
1
//查看本機CPU 核數, machdep.cpu.thread_count
sysctl machdep.cpu
//編譯,使用核數的兩倍, 這個耗時也就,差不多6個小時
make -j16
//啟動虛擬機
emulator

源碼導入 Android Studio 查看

  1. 編譯 idegen
source build/envsetup.sh // 將執行文件設置為臨時變量
mmm development/tools/idegen/  //生成idegen.jar文件

// 結果:
[100% 5/5] Install: out/host/darwin-x86/framework/idegen.jar

#### build completed successfully (07:31 (mm:ss)) ####
  1. 執行腳本 idegen.sh
development/tools/idegen/idegen.sh

//結果
Read excludes: 83ms
Traversed tree: 1886971ms
  1. 編譯完成,在AOSP 根目錄下可以看到生成的 android.ipr 和 android.iml 文件
- android.ipr : 保存工程相關的設置
- android.iml : 用來描述 modules 
  1. 導入。Android Studio -> File -> Open 選擇 android.ipr 文件
  2. 精簡導入
// 項目 全部項目導入,一兩個小時還完成不了,因此精簡下
// 修改 android.iml 的excludeFolder, 只保留 frameworks 和 package 包
<excludeFolder url="file://$MODULE_DIR$/prebuilt" />
      <excludeFolder url="file://$MODULE_DIR$/.repo"/>
      <excludeFolder url="file://$MODULE_DIR$/frameworks/base/docs"/>
      <excludeFolder url="file://$MODULE_DIR$/art"/>
      <excludeFolder url="file://$MODULE_DIR$/bionic"/>
      <excludeFolder url="file://$MODULE_DIR$/bootable"/>
      <excludeFolder url="file://$MODULE_DIR$/build"/>
      <excludeFolder url="file://$MODULE_DIR$/compatibility"/>
      <excludeFolder url="file://$MODULE_DIR$/cts"/>
      <excludeFolder url="file://$MODULE_DIR$/dalvik"/>
      <excludeFolder url="file://$MODULE_DIR$/developers"/>
      <excludeFolder url="file://$MODULE_DIR$/development"/>
      <excludeFolder url="file://$MODULE_DIR$/device"/>
      <excludeFolder url="file://$MODULE_DIR$/external"/>
      <excludeFolder url="file://$MODULE_DIR$/hardware"/>
      <excludeFolder url="file://$MODULE_DIR$/kernel"/>
      <excludeFolder url="file://$MODULE_DIR$/libcore"/>
      <excludeFolder url="file://$MODULE_DIR$/libnativehelper"/>
      <excludeFolder url="file://$MODULE_DIR$/ndk"/>
      <excludeFolder url="file://$MODULE_DIR$/out"/>
      <excludeFolder url="file://$MODULE_DIR$/pdk"/>
      <excludeFolder url="file://$MODULE_DIR$/platform_testing"/>
      <excludeFolder url="file://$MODULE_DIR$/prebuilts"/>
      <excludeFolder url="file://$MODULE_DIR$/projectFilesBackup"/>
      <excludeFolder url="file://$MODULE_DIR$/sdk"/>
      <excludeFolder url="file://$MODULE_DIR$/system"/>
      <excludeFolder url="file://$MODULE_DIR$/test"/>
      <excludeFolder url="file://$MODULE_DIR$/toolchain"/>
      <excludeFolder url="file://$MODULE_DIR$/tools"/>

碰到的問題

Could not find a supported mac sdk

問題:
本機 mac 10.14.3, 執行編譯報 "Could not find a supported mac sdk: [“10.10” “10.11” “10.12” “10.13”] "

網上的解決之道:
x86_darwin_host.go 文件新增 "10.14"系統,讓其支持

找到
build/soong/cc/config/x86_darwin_host.go
修改
darwinSupportedSdkVersions = []string{
        "10.10",
        "10.11",
        "10.12",
        "10.13",
        "10.14",

後遺癥:
這麽改完後,不報這個問題了,但是問題轉移了,成為下面那個問題了。

Undefined symbols for architecture i386

本機 mac 10.14, 其實是不支持 i386 架構,強行修改不行,搜索千百度,終於在 Google 論壇的 Android Building 組裏面看到一些有用信息

I have solved this issue.You can download MacOSX10.13.sdk on https://github.com/phracker/MacOSX-SDKs/releases and copy it to /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs

簡述之:

  1. 去 https://github.com/phracker/MacOSX-SDKs/releases 下載 MacOsx 10.13 sdk
  2. 將該sdk 拷貝到 /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs, 我這裏的 Xcode.app 是 Xcode-beta
  3. x86_darwin_host.go 文件新增 "10.14"系統去掉,讓他使用 10.13 編譯

結語

整個工程耗時耗力,也算是自己的一場磨難。

致謝

  1. macOS(Sierra 10.12)上Android源碼(AOSP)的下載、編譯與導入到Android Studio (最詳細)
  2. Android源碼:2、下載詳解(二)(支持移動硬盤、mac)—親測成功 (移動硬盤分區)
  3. 自己動手編譯Android源碼(超詳細) (模塊編譯)
  4. 搭建編譯環境 (android 官方文檔)
  5. Getting the following error while building aosp using mmm command. (解決困擾我許久的 i386 問題)

Android 源碼編譯之旅