環境準備
操作系統: Ubuntu16.04 + Win10
剩余空間: 200G以上(我是新買了一條240G的Nvme固態來編譯)
硬件配置: 16G內存 i7-7700HQ
配置解釋: 一開始, 我是用win10自帶的Ubuntu16.04, 結果各種bug(編譯的時候, 應該跟內核有關, 自帶的Ubuntu的內核是高度定制的), 卒; 然后想想用虛擬機, 也是Ubuntu16.04, 又出現各種bug, 也是編譯的時候, 卒; 最后很無奈, 買了一條新固態來裝雙系統, 第一次裝的是Ubuntu18.04, 結果編譯到98%就跑不下去了...卒; 最后的最后, 才裝回16.04, 編譯成功了...這一路的艱辛, 恐怕難有人體會.
目標系統: Android9 Pie
目標架構: aosp_arm64-eng
心理準備: 推薦先看0x04 幾點注意.
0x00 下載aosp源碼
- 下載好每月更新的初始化包:https://mirrors.tuna.tsinghua.edu.cn/aosp-monthly/aosp-latest.tar, 并放到要編譯的硬盤或文件夾里.
- 在
bash
里運行如下命令:
tar xvf aosp-latest.tar
- 解壓之后, 再配好
repo
:
mkdir ~/bin
PATH=~/bin:$PATH
curl https://mirrors.tuna.tsinghua.edu.cn/git/git-repo -o ~/bin/repo #使用tuna的git-repo鏡像
chmod a+x ~/bin/repo
- 配好
repo
環境之后, 再修改repo源:vim ~/bin/repo
:
# 將這行
REPO_URL = 'https://gerrit.googlesource.com/git-repo'
# 改為這行
REPO_URL = 'https://mirrors.tuna.tsinghua.edu.cn/git/git-repo'
- 開始更新初始化包:
cd aosp # 進入剛剛解壓aosp-latest.tar后的aosp目錄(該目錄有一個.repo)
repo sync
0x01 編譯aosp源碼
- 安裝jdk環境(已裝請忽略, 可裝最新版, 后面編譯看運氣):
sudo apt-get update
sudo apt-get install openjdk-8-jdk
- 安裝依賴環境(不裝的話, 后面編譯看運氣)
sudo apt-get install libx11-dev:i386 libreadline6-dev:i386 libgl1-mesa-dev g++-multilib
sudo apt-get install -y git flex bison gperf build-essential libncurses5-dev:i386
sudo apt-get install tofrodos python-markdown libxml2-utils xsltproc zlib1g-dev:i386
sudo apt-get install dpkg-dev libsdl1.2-dev libesd0-dev
sudo apt-get install git-core gnupg flex bison gperf build-essential
sudo apt-get install zip curl zlib1g-dev gcc-multilib g++-multilib
sudo apt-get install libc6-dev-i386
sudo apt-get install lib32ncurses5-dev x11proto-core-dev libx11-dev
sudo apt-get install libgl1-mesa-dev libxml2-utils xsltproc unzip m4
sudo apt-get install lib32z-dev ccache
或者你可以直接copy一條命令:
sudo apt-get install git ccache automake lzop bison gperf build-essential zip curl zlib1g-dev g++-multilib python-networkx libxml2-utils bzip2 libbz2-dev libbz2-1.0 libghc-bzlib-dev squashfs-tools pngcrush schedtool dpkg-dev liblz4-tool make optipng maven libc6-dev linux-libc-dev gcc-multilib g++-5-multilib libssl-dev
- 配置git環境
$ git config --global user.name "github賬號"
$ git config --global user.email "github密碼"
- 進入到剛剛解壓好的
aosp
這個目錄, 輸入如下命令(一定要注意命令先后順序):
關于lunch
的對象, 參考如下表格(根據手機型號編譯, 如果是emulator, 那就無所謂了), 例如我的是Nexus 6P, 當我進入到fastboot的時候能顯示我的代號是:angler
, 所以編譯選項就是:aosp_angler-userdebug
.
設備 | 代號 | 編譯配置 |
---|---|---|
Pixel XL | marlin | aosp_marlin-userdebug |
Pixel | sailfish | aosp_sailfish-userdebug |
HiKey | hikey | hikey-userdebug |
Nexus 6P | angler | aosp_angler-userdebug |
Nexus 5X | bullhead | aosp_bullhead-userdebug |
Nexus 6 | shamu | aosp_shamu-userdebug |
Nexus Player | fugu | aosp_fugu-userdebug |
Nexus 9 | volantis (flounder) | aosp_flounder-userdebug |
Nexus 5 (GSM/LTE) | hammerhead | aosp_hammerhead-userdebug |
Nexus 7 (WLAN) | razor (flo) | aosp_flo-userdebug |
Nexus 7(移動版) | razorg (deb) | aosp_deb-userdebug |
Nexus 10 | mantaray (manta) | full_manta-userdebug |
Nexus 4 | occam (mako) | full_mako-userdebug |
Nexus 7 (WLAN) | nakasi (grouper) | full_grouper-userdebug |
Nexus 7(移動版) | nakasig (tilapia) | full_tilapia-userdebug |
Galaxy Nexus (GSM/HSPA+) | yakju (maguro) | full_maguro-userdebug |
Galaxy Nexus (Verizon) | mysid (toro) | aosp_toro-userdebug |
Galaxy Nexus(試驗版) | mysidspr (toroplus) | aosp_toroplus-userdebug |
Motorola Xoom(美國 WLAN 版) | wingray | full_wingray-userdebug |
Nexus S | soju (crespo) | full_crespo-userdebug |
Nexus S 4G | sojus (crespo4g) | full_crespo4g-userdebug |
source build/envsetup.sh
# 下面的aosp_arm-eng是根據編譯的目標機器來編譯的, 如果你想考慮其他的, 可以在lunch后tab
lunch aosp_arm64-eng
# 鑒于我的cpu有8個processor, 所有開了8*2=16條線程編譯. 查看processor方式: cat /proc/cpuinfo
make -j16
對于lunch的版本解釋, 如下:
編譯類型 | 使用情況 |
---|---|
user | 權限受限;適用于生產環境 |
userdebug | 與“user”類似,但具有 root 權限和可調試性;是進行調試時的首選編譯類型 |
eng | 具有額外調試工具的開發配置 |
這時候就可以慢慢等著Successful(更有可能是error)出現吧.
0x02 運行系統
ource build/envsetup.sh
# 下面輸入你剛剛編譯的目標系統架構 我的如下
lunch aosp_arm64-eng
emulator
等等...怎么變成Q版本了 2333
0x03 刷入手機
- 解鎖手機(對于谷歌耳機而言)
# 對于新款設備(2015 年及之后發布的設備):
fastboot flashing unlock
# 對于老款設備(2014 年及之前發布的設備):
fastboot oem unlock
- 進入fastboot(也可以在關機狀態下, 按住
關機鍵+音量下鍵
)
# 在手機正常開機狀態下,打開開發者模式,允許adb調試,輸入如下命令
adb reboot bootloader
- 開始刷機, 在aosp目錄下進入
out/target/product/generic_arm64
:
fastboot flash boot boot.img
fastboot flash recovery recovery.img
fastboot flash system system.img
fastboot flash userdata userdata.img
- 或者在aosp根目錄下:
fastboot -w flashall
0x04 幾點注意
- 不推薦最新版的Ubuntu(如果你時間很多, 喜歡調error, 那盡情裝吧)
- 千萬不要在zsh編譯, 不然肯定error. (當初就是貪zsh炫酷, 又浪費了很多時間)
- 不推薦在虛擬機/Docker編譯aosp, 編譯幾十上百G的工程, 出個error絕對很容易, 任意一個變量都可能讓你調一天都解決不了.
- 網上的教程不要看太多, 容易亂(包括我的), 最好還是跟著官方文檔走, 那個比較穩. 如果真要找, 那就找和自己編譯的環境一樣的高質blog, 不然浪費的是自己的時間.
- 如果硬盤太小, 編譯到后面肯定不夠容量, 當初我就是因為沒有把初始化包刪掉(30多G), 導致編譯到99%出現了error, 原因就是因為容量不夠.
- 編譯路徑不要含有中文
- 實在解決不了的bug, 可以在評論區一起討論, 堅持就是勝利, 相信自己堅持就能編譯成功.
0x05 關于編譯介紹
- 在aosp根目錄下,
out
目錄是編譯之后產生的, 具體介紹如下:
-
out/target/product/[generic]/system
: 其中,[]
表示的目錄是根據你lunch
選項生成的, 例如generic_arm64
表示生成的是arm
架構的64問系統. -
out/target/product/generic/system/apk
: Android系統自帶的apk文件 -
out/target/product/generic/system/bin
: 一些可執行文件(比如C編譯的執行) -
out/target/product/generic/system/lib
: 動態鏈接庫; -
out/targer/product/generic/system/lib/hw
: 硬件抽象層文件
- 模塊編譯:
當在aosp根目錄下執行了source build/envsetup.sh
之后, 生成了一系列可執行的命令, 如下:
- croot: Changes directory to the top of the tree.
- m: Makes from the top of the tree.
- mm: Builds all of the modules in the current directory.
- mmm: Builds all of the modules in the supplied directories.
- cgrep: Greps on all local C/C++ files.
- jgrep: Greps on all local Java files.
- resgrep: Greps on all local res/*.xml files.
- godir: Go to the directory containing a file.
command -help
可以查看這些命令的具體用法mmm
命令是用來編譯指定目錄的所有模塊的mm
命令是用來編譯當前目錄的所有模塊的
- 編譯
framework
# 先在framework/base目錄下執行
mmm core/res/ # 生成framework-res.apk
# 然后再返回aosp目錄,單獨編譯framework
mmm frameworks/base
編譯好模塊后,還要重新打包一下system.img
文件(重打包命令如下),這樣我們把system.img運行在模擬器上時,就可以看到我們的程序了。
make snod
0x06 aosp相關推薦
安裝sublime(破解版): https://www.yejinmo.com/sublime-text-3-3103/
AS Sublime風格主題:https://github.com/OtaK/jetbrains-monokai-sublime
aosp源碼打包下載: https://android.googlesource.com/?format=HTML
aosp源碼在線閱讀: http://androidxref.com/
安卓進階, 幾種通信方式: https://blog.csdn.net/u011240877/article/details/72863432
學習Java很棒的網站(有在線Java IDE可以用案例): https://www.geeksforgeeks.org
0x07 編譯相關推薦
https://blog.csdn.net/fuchaosz/article/details/51487585
https://blog.csdn.net/lyb2518/article/details/77072792
https://blog.csdn.net/tyyj90/article/details/53443913
https://blog.csdn.net/litefish/article/details/51864248
官方教程:
下載: https://source.android.com/setup/build/requirements
編譯: https://source.android.com/setup/build/requirements
0x08 幾個錯誤修復
1. repo sync錯誤: error: .repo/manifests/contains uncommitted changes
cd .repo/manifests
git stash
git clean -f -d
還是不行. 試用如下命令(也是在manifests目錄下)
git config core.filemode false