《AOSP 源碼下載》完成后,就可以開編了。
整編
整編,顧名思義就是編譯整個(gè) Android 源碼,最終 out 目錄會(huì)生成幾個(gè)重要的鏡像文件,其中有 system.img、userdata.img、ramdisk.img 等,這些是可以刷機(jī)的。
初始化編譯環(huán)境
項(xiàng)目目錄,打開終端,輸入命令:
source build/envsetup.sh
選擇編譯目標(biāo)
命令:
lunch
輸出:
You're building on LinuxLunch menu... pick a combo: 1. aosp_arm-eng 2. aosp_arm64-eng 3. aosp_blueline-userdebug 4. aosp_car_arm-userdebug 5. aosp_car_arm64-userdebug 6. aosp_car_x86-userdebug 7. aosp_car_x86_64-userdebug 8. aosp_cf_x86_64_auto-userdebug 9. aosp_cf_x86_64_phone-userdebug 10. aosp_cf_x86_64_tablet-userdebug 11. aosp_cf_x86_64_tablet_3g-userdebug 12. aosp_cf_x86_64_tv-userdebug 13. aosp_cf_x86_64_wear-userdebug 14. aosp_cf_x86_auto-userdebug 15. aosp_cf_x86_phone-userdebug 16. aosp_cf_x86_tablet-userdebug 17. aosp_cf_x86_tablet_3g-userdebug 18. aosp_cf_x86_tv-userdebug 19. aosp_cf_x86_wear-userdebug 20. aosp_crosshatch-userdebug 21. aosp_marlin-userdebug 22. aosp_sailfish-userdebug 23. aosp_taimen-userdebug 24. aosp_walleye-userdebug 25. aosp_walleye_test-userdebug 26. aosp_x86-eng 27. aosp_x86_64-eng 28. beagle_x15-userdebug 29. hikey-userdebug 30. hikey64_only-userdebug 31. hikey960-userdebug 32. hikey960_tv-userdebug 33. hikey_tv-userdebug 34. m_e_arm-userdebug 35. mini_emulator_arm64-userdebug 36. mini_emulator_x86-userdebug 37. mini_emulator_x86_64-userdebug 38. poplar-eng 39. poplar-user 40. poplar-userdebug 41. uml-userdebugWhich would you like? [aosp_arm-eng] 26
編譯目標(biāo)格式說明
編譯目標(biāo)的格式:BUILD-BUILDTYPE,比如上面的 poplar-eng 的 BUILD 是 poplar,BUILDTYPE 是 eng.
什么是 BUILD
BUILD 指的是特定功能的組合的特定名稱,即表示編譯出的鏡像可以運(yùn)行在什么環(huán)境。其中 aosp(Android Open Source Project)代表 Android 開源項(xiàng)目;arm 表示系統(tǒng)是運(yùn)行在 arm 架構(gòu)的處理器上,arm64 則是指 64 位 arm 架構(gòu)處理器,x86 則表示 x86 架構(gòu)的處理器,更多設(shè)備代碼和編譯目標(biāo)參考官方文檔。
什么是 BUILDTYPE
BUILDTYPE 則指的是編譯類型,通常有三種:
user:用來正式發(fā)布到市場(chǎng)的版本,權(quán)限受限,如沒有 root 權(quán)限,不能 dedug 等。
userdebug:在user版本的基礎(chǔ)上開放了 root 權(quán)限和 debug 權(quán)限。
eng:代表 engineer,開發(fā)工程師的版本,擁有最大的權(quán)限(root等),具有額外調(diào)試工具的開發(fā)配置。
如果沒有谷歌手機(jī)設(shè)備,可以選擇 arm 或者 x86,我選擇了 aosp_x86-eng,編譯完后運(yùn)行模擬器看看,因此這里選擇序號(hào) 26。
開始編譯
通過 make 指令進(jìn)行代碼編譯:
make -j8
其中 -jN 參數(shù)表示處理并行任務(wù),通常使用的任務(wù)數(shù) N 介于編譯時(shí)所用計(jì)算機(jī)上硬件線程數(shù)的 1-2 倍之間。
查看計(jì)算機(jī)上的核心數(shù):
cat /proc/cpuinfo | grep processorprocessor : 0processor : 1processor : 2processor : 3
可看到創(chuàng)建的虛擬機(jī) CPU 核心共有 4 個(gè),那么要實(shí)現(xiàn)最快的編譯速度,可以使用介于 make -j4 到 make -j8 之間的命令。
不出意外,當(dāng)看到:#### build completed successfully (03:55:24 (hh:mm:ss)) ####
就整編成功了。
運(yùn)行模擬器
整編完,網(wǎng)上很多都是說直接運(yùn)行命令:emulator
,但是我報(bào)以下錯(cuò)誤了:
emulator: WARNING: Couldn't find crash service executable /media/ubuntu/disk/Project/AOSP9/prebuilts/android-emulator/linux-x86_64/emulator64-crash-serviceemulator: WARNING: system partition size adjusted to match image file (1080 MB > 800 MB)queryCoreProfileSupport: swap interval not foundfailed to create drawablefailed to create drawablefailed to create drawablefailed to create drawablefailed to create drawablefailed to create drawablefailed to create drawablefailed to create drawablegetGLES2ExtensionString: Could not make GLES 2.x context current!
第一個(gè)警告不管,第二個(gè)警告可以 emulator 加參數(shù) -partition-size 2048
解決,也可以不管,最后一個(gè)折騰了很久,可以加參數(shù)-gpu off
,完整運(yùn)行模擬器的命令:emulator -gpu off -partition-size 2048
,這時(shí)應(yīng)該又會(huì)報(bào):
emulator: WARNING: Couldn't find crash service executable /media/ubuntu/disk/Project/AOSP9/prebuilts/android-emulator/linux-x86_64/emulator64-crash-serviceemulator: WARNING: system partition size adjusted to match image file (1338 MB > 800 MB)emulator: ERROR: Running multiple emulators with the same AVD is an experimental feature.Please use -read-only flag to enable this feature.
只能重啟一下才能解決。
如果編譯完成后啟動(dòng)模擬器時(shí)卡在黑屏,可以嘗試編譯其他的!
emulator 還有很多參數(shù),可以用 emulator -help
查看,參數(shù)如下:
-sysdir <dir> 為模擬器在<dir>目錄中搜索系統(tǒng)硬盤鏡像-system <file> 為模擬器從<file>文件中讀取初始化系統(tǒng)鏡像-datadir <dir> 設(shè)置用戶數(shù)據(jù)寫入的目錄-kernel <file> 為模擬器設(shè)置使用指定的模擬器內(nèi)核-ramdisk <file> 設(shè)置內(nèi)存RAM 鏡像文件(默認(rèn)為<system>/ramdisk.img)-image <file> 廢棄,使用-system <file> 替代-init-data <file> 設(shè)置初始化數(shù)據(jù)鏡像(默認(rèn)為<system>/userdata.img)-initdata <file> 和"-init-data <file>"使用方法一致-data <file> 設(shè)置數(shù)據(jù)鏡像(默認(rèn)為<datadir>/userdata-qemu.img)-partition-size <size> system/data 分區(qū)容量大小(MB)-cache <file> 設(shè)置模擬器緩存分區(qū)鏡像(默認(rèn)為零時(shí)文件)-no-cache 禁用緩存分區(qū)-nocache 與"-no-cache"使用方法相同-sdcard <file> 指定模擬器SDCard 鏡像文件(默認(rèn)為<system>/sdcard.img)-wipe-data 清除并重置用戶數(shù)據(jù)鏡像(從initdata 拷貝)-avd <name> 指定模擬器使用Android 虛擬設(shè)備-skindir <dir> 設(shè)置模擬器皮膚在<dir>目錄中搜索皮膚(默認(rèn)為<system>/skins 目錄)-skin <name> 選擇使用給定的皮膚-no-skin 不適用任何模擬器皮膚-noskin 使用方法與"-no-skin"相同-memory <size> 物理RAM 內(nèi)存大小(MB)-netspeed <speed> 設(shè)置最大網(wǎng)絡(luò)下載、上傳速度-netdelay <delay> 網(wǎng)絡(luò)時(shí)延模擬-netfast 禁用網(wǎng)絡(luò)形態(tài)-tarce <name> 代碼配置可用-show-kernel 顯示內(nèi)核信息-shell 在當(dāng)前終端中使用根Shell 命令-no-jni Dalvik 運(yùn)行時(shí)禁用JNI 檢測(cè)-nojni 使用方法與"-no-jni"相同-logcat <tag> 輸出給定tag 的Logcat 信息-no-audio 禁用音頻支持-noaudio 與"-no-audio"用法相同-audio <backend> 使用指定的音頻backend-audio-in <backend> 使用指定的輸入音頻backend-audoi-out <backend> 使用指定的輸出音頻backend-raw-keys 禁用Unicode 鍵盤翻轉(zhuǎn)圖-radio 重定向無線模式接口到個(gè)性化設(shè)備-port <port> 設(shè)置控制臺(tái)使用的TCP 端口-ports <consoleport>,<adbport> 設(shè)置控制臺(tái)使用的TCP 端口和ADB 調(diào)試橋使用的TCP 端口-onion <image> 在屏幕上層使用覆蓋PNG 圖片-onion-alpha <%age> 指定上層皮膚半透明度-onion-rotation 0|1|2|3 指定上層皮膚旋轉(zhuǎn)-scale <scale> 調(diào)節(jié)模擬器窗口尺寸(三種:1.0-3.0、dpi、auto)-dpi-device <dpi> 設(shè)置設(shè)備的resolution (dpi 單位) (默認(rèn)165)-http-proxy <proxy> 通過一個(gè)HTTP 或HTTPS 代理來創(chuàng)建TCP 連接-timezone <timezone> 使用給定的時(shí)區(qū),而不是主機(jī)默認(rèn)的-dns-server <server> 在模擬系統(tǒng)上使用給定的DNS 服務(wù)-cpu-delay <cpudelay> 調(diào)節(jié)CUP 模擬-no-boot-anim 禁用動(dòng)畫來快速啟動(dòng)-no-window 禁用圖形化窗口顯示-version 顯示模擬器版本號(hào)-report-console <socket> 向遠(yuǎn)程socket 報(bào)告控制臺(tái)端口-gps <device> 重定向GPS 導(dǎo)航到個(gè)性化設(shè)備-keyset <name> 指定按鍵設(shè)置文件名-shell-serial <device> 根shell 的個(gè)性化設(shè)備-old-system 支持舊版本(pre 1.4)系統(tǒng)鏡像-tcpdump <file> 把網(wǎng)絡(luò)數(shù)據(jù)包捕獲到文件中-bootchart <timeout> bootcharting 可用-qemu args.... 向qemu 傳遞參數(shù)-qemu -h 顯示qemu 幫助-verbose 和"-debug-init"相同-debug <tags> 可用、禁用調(diào)試信息-debug-<tag> 使指定的調(diào)試信息可用-debug-no-<tag> 禁用指定的調(diào)試信息-help 打印出該幫助文檔-help-<option> 打印出指定option 的幫助文檔-help-disk-images 關(guān)于硬盤鏡像幫助-help-keys 支持按鈕捆綁(手機(jī)快捷鍵)-help-debug-tags 顯示出-debug <tag>命令中的tag 可選值-help-char-devices 個(gè)性化設(shè)備說明-help-environment 環(huán)境變量-help-keyset-file 指定按鍵綁定設(shè)置文件-help-virtula-device 虛擬設(shè)備管理
補(bǔ)充
模擬器運(yùn)行需要四個(gè)文件,分別是:
Linux Kernel
system.img
userdata.img
ramdisk.img
上面我 lunch 命令時(shí)選擇的是 aosp_x86-eng,因此 linux 默認(rèn)使用的 AOSP/prebuilds/qemu-kernel/x86/kernel-qemu 下的 kernel-qemu,而其他文件則是使用的 AOSP/out/target/product/generic_x86 目錄下的 system.img、userdata.img、ramdisk.img。
單編
單編就是編譯某個(gè)模塊,比如 Setting,會(huì)在 out 目錄對(duì)應(yīng)的產(chǎn)品有 Settings.apk,例如:out/target/product/generic_x86/system/priv-app/Settings/Settings.apk,這是可以直接安裝的。
1、source build/envsetup.sh
2、lunch
3、選擇單編版本序號(hào)
4、編譯
4.1、mmm
mmm packages/apps/Settings/
編譯指定目錄下的模塊,但不編譯它所依賴的其它模塊。
4.2、mm
先進(jìn)入目錄:
cd packages/apps/Settings/
再編譯
mm
編譯當(dāng)前目錄下的模塊,它和 mmm 一樣,不編譯依賴模塊。
4.3、mma
編譯當(dāng)前目錄下的模塊及其依賴項(xiàng)。
4.4、mmma [module_path]
編譯指定路徑下所有模塊,且包含依賴
4.5、make
make: 不帶任何參數(shù)則是編譯整個(gè)系統(tǒng)
make MediaProvider
單個(gè)模塊編譯,會(huì)把該模塊及其依賴的其他模塊一起編譯,會(huì)搜索整個(gè)源代碼來定位 MediaProvider 模塊所使用的 Android.mk 文件,還要判斷該模塊依賴的其他模塊是否有修改。
make snod
如果我們修改代碼,想看效果,怎么辦?
1、可以在編譯完成后,借助 adb install -r apk路徑
直接將生成的 apk 文件安裝到設(shè)備上。
2、可以編譯好完成后,再使用make snod
,重新生成 system.img,再運(yùn)行模擬器也可。