Android7.0原始碼編譯執行指南
編譯環境:Ubuntu 16.04
映象檔案:清華大學AOSP映象 Android7.0
一、原始碼下載
1 映象地址
清華大學AOSP(Android Open Source Project)
https://mirrors.tuna.tsinghua.edu.cn/help/AOSP/
2 過程摘錄
下載Repo 工具
mkdir ~/bin
PATH=~/bin:$PATH
curl https://storage.googleapis.com/git-repo-downloads/repo > ~/bin/repo
chmod a+x ~/bin/repo
使用每月更新的初始化包
由於首次同步需要下載 24GB 資料,過程中任何網路故障都可能造成同步失敗,我們強烈建議您使用初始化包進行初始化。下載 https://mirrors.tuna.tsinghua.edu.cn/aosp-monthly/aosp-latest.tar,下載完成後記得根據 checksum.txt 的內容校驗一下。由於所有程式碼都是從隱藏的 .repo
目錄中 checkout 出來的,所以我們只保留了 .repo
目錄,下載後解壓 再 repo sync
一遍即可得到完整的目錄。
使用方法如下:
wget https://mirrors.tuna.tsinghua.edu.cn/aosp-monthly/aosp-latest.tar # 下載初始化包
tar xf aosp-latest.tar
cd AOSP # 解壓得到的 AOSP 工程目錄
# 這時 ls 的話什麼也看不到,因為只有一個隱藏的 .repo 目錄
repo sync # 正常同步一遍即可得到完整目錄
# 或 repo sync -l 僅checkout程式碼
此後,每次只需執行 repo sync
即可保持同步。 我們強烈建議您保持每天同步,並儘量選擇凌晨等低峰時間
3 解決repo sync下載程式碼時Failed connect錯誤的辦法:
下載android程式碼時錯誤提示:error: Failed connect to android.googlesource.com:443
解決方法:編輯/etc/hosts檔案
vim /etc/hosts
增加下面內容,儲存
173.194.72.82 http://www.googlesource.com
173.194.72.82 android.googlesource.com
37.61.54.158 cache.pack.google.com
173.194.74.82 gerrit.googlesource.com
4 其他
- 初始化包大小21.4G,我的網速5-6M/s,下載時間基本一個小時左右。
- Android7.0,aosp目錄編譯完之後的大小是54.6G,安裝Ubuntu時要注意留夠足夠的空間,推薦100G+以上。
二、原始碼編譯
1 搭建編譯環境
- openJdk is needed !我自己平時開發用的是JDK1.8,這裡就要安裝一下openjdk,並且更改環境變數。
sudo apt-get update
sudo apt-get install openjdk-7-jdk
但是如果你是剛安裝完Ubuntu,那麼就可以跳過此步驟,Ubuntu自帶openjdk
- required package
sudo apt-get install bison g++-multilib git gperf libxml2-utils make python-networkx zlib1g-dev:i386 zip
- 因為原始碼包含C 和 C++程式碼,gcc為必須
sudo apt-get install gcc
- Setting up ccache , 使用ccache
ccache是一個編譯器ccache用於C和C++。可以讓構建更加快速。在原始碼的根目錄處,做如下操作:
$export USE_CCACHE=1
$export CCACHE_DIR=/<path_of_your_choice>/.ccache
$prebuilts/misc/linux-x86/ccache/ccache -M 50G</path_of_your_choice>
建議的cache大小為50-100G
可以使用如下操作來檢視使用的ccache大小
$watch -n1 -d prebuilts/misc/linux-x86/ccache/ccache -s
若使用Ice Cream Sandwich(4.0.x)或更老的版本,需要用prebuilts來代替prebuilts/misc
2 開始編譯 Building the System
- 1.Set up environment
$ source build/envsetup.sh
or
$ . build/envsetup.sh
- 2.Choose a Target
$ lunch
You're building on Linux
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_manta-userdebug
8. aosp_flo-userdebug
9. aosp_deb-userdebug
10. full_fugu-userdebug
11. aosp_fugu-userdebug
12. aosp_tilapia-userdebug
13. aosp_grouper-userdebug
14. aosp_mako-userdebug
15. aosp_hammerhead-userdebug
16. aosp_flounder-userdebug
17. aosp_shamu-userdebug
18. mini_emulator_x86-userdebug
19. mini_emulator_arm64-userdebug
20. mini_emulator_x86_64-userdebug
21. mini_emulator_mips-userdebug
22. m_e_arm-userdebug
Which would you like? [aosp_arm-eng]
- 3.build
使用make構建,GNU make可以通過使用-jN引數來處理並行任務。此處的N,根據所用的計算機的硬體的核數來指定,一般N指定為CPU核心的1到2倍。例如我的CPU是i5-6500(CPU4個核,每個核2個執行緒),那麼最優的構建是使用make -j8。
make -j8
3 build successfully
#### make completed successfully (01:04:27 (hh:mm:ss)) ####
- 目錄結構如下:
7 9月 24 00:36 android-info.txt
62 9月 24 00:22 build_fingerprint.txt
4.0K 9月 24 10:34 cache
66M 9月 24 12:26 cache.img
70K 9月 23 23:51 clean_steps.mk
38 9月 24 00:22 current_build_config.mk
4.0K 9月 24 10:41 data
4.0K 9月 24 09:51 dex_bootjars
4.0K 9月 24 01:33 gen
1.4K 9月 24 12:25 hardware-qemu.ini
70K 9月 24 11:00 installed-files.txt
1.1M 9月 24 00:30 module-info.json
4.0K 9月 24 11:00 obj
38 9月 23 23:51 previous_build_config.mk
1.5M 9月 24 10:40 ramdisk.img
4.0K 9月 24 10:39 recovery
4.0K 9月 24 10:39 root
4.0K 9月 24 10:39 symbols
4.0K 9月 24 10:50 system
1.8G 9月 24 11:01 system.img
550M 9月 24 10:58 userdata.img
550M 9月 24 12:41 userdata-qemu.img
~/aosp/out/target/product/generic$
4 編譯中的問題解決
- 第一次編譯,在編譯到82%的時候失敗,報錯 : Increase Java heap size
including ./tools/external/fat32lib/Android.mk ...
Starting build with ninja
ninja: Entering directory `.'
[ 0% 1/21542] Ensure Jack server is installed and started
Jack server already installed in "/home/smile/.jack-server"
Server is already running
[ 0% 2/21542] Building with Jack: out/target/common/obj/JAVA_LIBRARIES/core-all_intermediates/with-local/classes.dex
FAILED: /bin/bash out/target/common/obj/JAVA_LIBRARIES/core-all_intermediates/with-local/classes.dex.rsp
Java heap space
Try increasing heap size with java option '-Xmx<size>'
Warning: This may have produced partial or corrupted output.
ninja: build stopped: subcommand failed.
build/core/ninja.mk:146: recipe for target 'ninja_wrapper' failed
make: *** [ninja_wrapper] Error 1
\#### make failed to build some targets (08:25 (mm:ss)) ####
stackoverflow上關於此問題的解決方法([詳見這裡](http://stackoverflow.com/questions/34940793/increasing-heap-size-while-building-the-android-source-code-on-ubuntu-15-10)),在make之前進行heap size配置:
1. export JACK_SERVER_VM_ARGUMENTS="-Dfile.encoding=UTF-8 -XX:+TieredCompilation -Xmx4096m"
2. out/host/linux-x86/bin/jack-admin kill-server
3. out/host/linux-x86/bin/jack-admin start-server
三、執行
1 推送裝置(真機執行)Flash device
- To flash a device, you will need to use fastboot, which should be included in your path after a successful build. Place the device in fastboot mode either manually by holding the appropriate key combination at boot, or from the shell with
$ adb reboot bootloader
- Once the device is in fastboot mode, run
$ fastboot flashall -w
- The -w option wipes the /data partition on the device; this is useful for your first time flashing a particular device but is otherwise unnecessary.
2 模擬器執行 Flash emulator
- 1.build generic img
lunch 1即可
- 2.啟動模擬器
~/aosp$ emulator
emulator: WARNING: system partition size adjusted to match image file (1536 MB > 200 MB)
emulator: WARNING: data partition size adjusted to match image file (550 MB > 200 MB)
emulator: WARNING: Increasing RAM size to 1GB
Creating filesystem with parameters:
Size: 576716800
Block size: 4096
Blocks per group: 32768
Inodes per group: 7040
Inode size: 256
Journal blocks: 2200
Label:
Blocks: 140800
Block groups: 5
Reserved block group size: 39
Created filesystem with 11/35200 inodes and 4536/140800 blocks
resize2fs 1.42.13 (17-May-2015)
The filesystem is already 140800 (4k) blocks long. Nothing to do!
Creating filesystem with parameters:
Size: 69206016
Block size: 4096
Blocks per group: 32768
Inodes per group: 4224
Inode size: 256
Journal blocks: 1024
Label:
Blocks: 16896
Block groups: 1
Reserved block group size: 7
Created filesystem with 11/4224 inodes and 1302/16896 blocks
emulator: UpdateChecker: skipped version check
3 注意事項
1 因為上面的環境變數配置的是臨時的,所以終端關閉後,直接執行emulator是不行的,需要重新執行下/build/envsetup.sh 和lunch 選擇之前編譯的版本
2 安裝kvm,開啟硬體加速
無論是在Windows平臺還是Linux平臺,或者還是Mac OSX平臺,虛擬機器的硬體加速全靠這個。
$ sudo apt-get install qemu-kvm
直接執行,完成安裝即可。
開啟之後我們可以使用下面的指令來驗證
$ egrep -c '(vmx|svm)' /proc/cpuinfo
執行的結果不為0
表示開啟成功(我的是4
)。