環(huán)境準備
官網(wǎng) 描述得已經(jīng)相當清楚了 ,這里稍微總結(jié)一下:
創(chuàng)建區(qū)分大小寫的磁盤映像
mac系統(tǒng)默認是不區(qū)分大小寫的,所以我們需要創(chuàng)建一個區(qū)分大小寫的文件系統(tǒng)
hdiutil create -type SPARSE -fs 'Case-sensitive Journaled HFS+' -size 60g ~/android.dmg
這將創(chuàng)建一個.dmg.sparseimage文件,該文件在裝載后可用作具有 Android 開發(fā)所需格式的驅(qū)動盤。
按官網(wǎng)所說完成編譯至少需要 25GB 空間,相信我,其實至少需要60G。當然,空間大小后面還可以通過以下命令修改
hdiutil resize -size <new-size-you-want>g ~/android.dmg.sparseimage
為了方便,我們還可以往環(huán)境變量配置文件(~/.bash_profile--bash,~/.zshrc--zsh)添加輔助函數(shù)
-
裝載函數(shù)
# mount the android file image mountAndroid() { hdiutil attach ~/android.dmg.sparseimage -mountpoint /Volumes/android; -
卸載函數(shù)
# unmount the android file image umountAndroid() { hdiutil detach /Volumes/android; }
安裝所需的軟件
-
JDK
各種 Android 版本使用的 Java 版本不一樣,請參閱相關要求
我這里是編譯Android8.1.0 ,所以使用java1.8
-
Xcode 命令行工具
xcode-select --install -
MacPorts
從macports.org 下載安裝,請確保
/opt/local/bin在路徑中顯示在/usr/bin前面。否則,請將以下內(nèi)容添加到環(huán)境變量配置文件(~/.bash_profile--bash,~/.zshrc--zsh)中:export PATH=/opt/local/bin:$PATH通過 MacPorts 獲取 Make、Git 、 GPG、BISON 軟件包
POSIXLY_CORRECT=1 sudo port install gmake libsdl git gnupg bison
設置文件描述符數(shù)量上限
在 Mac OS 中,可同時打開的文件描述符的默認數(shù)量上限太低,在高度并行的編譯流程中,可能會超出此上限。要提高此上限,請將下列行添加到環(huán)境變量配置文件(~/.bash_profile--bash,~/.zshrc--zsh)中:
# set the number of open files to be 1024
ulimit -S -n 1024
下載源代碼
Android 源代碼樹位于由 Google 托管的 Git 代碼庫中。為了在 Android 環(huán)境中更輕松地使用 Git,Google開發(fā)了Repo。
安裝 Repo
-
確保主目錄下有一個
bin/目錄,并且該目錄包含在路徑中:mkdir ~/bin PATH=~/bin:$PATH -
下載 Repo 工具,并確保它可執(zhí)行
curl https://storage.googleapis.com/git-repo-downloads/repo > ~/bin/repo chmod a+x ~/bin/repo
repo 初始化
進入我們一開始創(chuàng)建的文件系統(tǒng),創(chuàng)建一個空目錄
? ~ mountAndroid
/dev/disk3 GUID_partition_scheme
/dev/disk3s1 EFI
/dev/disk3s2 Apple_HFS /Volumes/android
? ~ cd /Volumes/android
? ~ mkdir aosp
? ~ cd aosp
指定需要checkout對應的源代碼標記和編譯版本
repo init -u https://android.googlesource.com/platform/manifest -b android-8.1.0_r50
初始化成功后,目錄中應包含一個 .repo 目錄。
下載
這時候就可以開始漫長的下載過程了
repo sync
同步操作順利的話將需要 1 個小時或更長時間完成,
下載驅(qū)動
從官網(wǎng)下載對應機型驅(qū)動即可,下載完成后解壓,依次執(zhí)行里面的sh文件,如:
$ ./extract-huawei-angler.sh
The license for this software will now be displayed.
You must agree to this license before using this software.
Press Enter to view the licensels
執(zhí)行完畢,驅(qū)動文件會釋放到vendor目錄。
編譯
清理
make clobber
設置環(huán)境
source build/envsetup.sh
選擇目標
? ~ lunch
You're building on Darwin
Lunch menu... pick a combo:
1. aosp_arm-eng
2. aosp_arm64-eng
3. aosp_mips-eng
4. aosp_mips64-eng
5. aosp_x86-eng
6. aosp_x86_64-eng
7. aosp_deb-userdebug
8. aosp_flo-userdebug
9. full_fugu-userdebug
10. aosp_fugu-userdebug
11. mini_emulator_arm64-userdebug
12. m_e_arm-userdebug
13. mini_emulator_mips-userdebug
14. mini_emulator_x86-userdebug
15. mini_emulator_x86_64-userdebug
16. aosp_flounder-userdebug
17. aosp_angler-userdebug
18. aosp_bullhead-userdebug
19. aosp_hammerhead-userdebug
20. aosp_hammerhead_fp-userdebug
21. aosp_shamu-userdebug
22. aosp_bullhead-userdebug
23. aosp_angler-userdebug
因為我要編譯nexus6p,這里選擇23,其他設備可以參考選擇設備編譯系統(tǒng)
編譯代碼
make -j8
-jN 表示編譯并行任務數(shù),這個示電腦情況而定,一般取cpu數(shù)的1~2倍就可以
編譯遇到問題
找不到對應的macOS.sdk
-
報錯日志
[44/44] bootstrap out/soong/.minibootstrap/build.ninja.in [4/4] out/soong/.bootstrap/bin/minibp out/soong/.bootstrap/build.ninja [860/861] glob vendor///Android.bp [54/54] out/soong/.bootstrap/bin/soong_build out/soong/build.ninja FAILED: out/soong/build.ninja out/soong/.bootstrap/bin/soong_build -t -b out/soong -d out/soong/build.ninja.d -o out/soong/build.ninja Android.bp internal error: Could not find a supported mac sdk: ["10.10" "10.11" "10.12"] ninja: build stopped: subcommand failed. 17:53:06 soong failed with: exit status 1 -
解決方法
修改/build/soong/cc/config/x86_darwin_host.go文件,添加10.14支持,如下
darwinSupportedSdkVersions = []string{ "10.10", "10.11", "10.12", "10.14", // 添加mac sdk 10.14 }
遇到bison 錯誤
-
報錯日志
FAILED: out/soong/.intermediates/external/selinux/checkpolicy/checkpolicy/darwin_x86_64/gen/yacc/external/selinux/checkpolicy/policy_parse.c out/soong/.intermediates/external/selinux/checkpolicy/checkpolicy/darwin_x86_64/gen/yacc/external/selinux/checkpolicy/policy_parse.h BISON_PKGDATADIR=external/bison/data prebuilts/misc/darwin-x86/bison/bison -d --defines=out/soong/.intermediates/external/selinux/checkpolicy/checkpolicy/darwin_x86_64/gen/yacc/external/selinux/checkpolicy/policy_parse.h -o out/soong/.intermediates/external/selinux/checkpolicy/checkpolicy/darwin_x86_64/gen/yacc/external/selinux/checkpolicy/policy_parse.c external/selinux/checkpolicy/policy_parse.y [ 0% 309/87784] //external/libcxx:libc++_static header-abi-dumper src/random.cpp [arm] ninja: build stopped: subcommand failed. 18:05:05 ninja failed with: exit status 1 -
解決辦法
cd /Volumes/AOSP/external/bison git cherry-pick c0c852bd6fe462b148475476d9124fd740eba160 mm # 用新生成的bison替換掉原bison文件 cp /Volumes/AOSP/out/host/darwin-x86/bin/bison /Volumes/AOSP/prebuilts/misc/darwin-x86/bison/ # 重新編譯 make -j8
刷機
經(jīng)過漫長的等待和反復折騰后,終于到了最后一步---刷機。
# 手機連接電腦情況下
adb reboot bootloader
# 進入源碼編譯輸出的目錄
fastboot flashing unlock
fastboot flashall -w
Done
參考鏈接
https://source.android.com/setup/build/requirements
http://www.itdecent.cn/p/1c3d47b2031f