《AOSP 源碼下載》完成后,就可以開編了。
整編
整編匾七,顧名思義就是編譯整個 Android 源碼,最終 out 目錄會生成幾個重要的鏡像文件江兢,其中有 system.img昨忆、userdata.img、ramdisk.img 等杉允,這些是可以刷機的邑贴。
初始化編譯環(huán)境
項目目錄席里,打開終端,輸入命令:
source build/envsetup.sh
選擇編譯目標
命令:
lunch
輸出:
You're building on Linux
Lunch 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-userdebug
Which would you like? [aosp_arm-eng] 26
編譯目標格式說明
編譯目標的格式:BUILD-BUILDTYPE拢驾,比如上面的 poplar-eng 的 BUILD 是 poplar奖磁,BUILDTYPE 是 eng.
什么是 BUILD
BUILD 指的是特定功能的組合的特定名稱,即表示編譯出的鏡像可以運行在什么環(huán)境繁疤。其中 aosp(Android Open Source Project)代表 Android 開源項目咖为;arm 表示系統(tǒng)是運行在 arm 架構的處理器上,arm64 則是指 64 位 arm 架構處理器稠腊,x86 則表示 x86 架構的處理器躁染,更多設備代碼和編譯目標參考官方文檔:https://source.android.google.cn/source/running.html#selecting-device-build。
什么是 BUILDTYPE
BUILDTYPE 則指的是編譯類型架忌,通常有三種:
- user:用來正式發(fā)布到市場的版本吞彤,權限受限,如沒有 root 權限叹放,不能 dedug 等饰恕。
- userdebug:在user版本的基礎上開放了 root 權限和 debug 權限。
- eng:代表 engineer井仰,開發(fā)工程師的版本埋嵌,擁有最大的權限(root等),具有額外調試工具的開發(fā)配置糕档。
如果沒有谷歌手機設備莉恼,可以選擇 arm 或者 x86,我選擇了 aosp_x86-eng速那,編譯完后運行模擬器看看俐银,因此這里選擇序號 26。
開始編譯
通過 make 指令進行代碼編譯:
make -j8
其中 -jN 參數(shù)表示處理并行任務端仰,通常使用的任務數(shù) N 介于編譯時所用計算機上硬件線程數(shù)的 1-2 倍之間捶惜。
查看計算機上的核心數(shù):
cat /proc/cpuinfo | grep processor
processor : 0
processor : 1
processor : 2
processor : 3
可看到創(chuàng)建的虛擬機 CPU 核心共有 4 個,那么要實現(xiàn)最快的編譯速度荔烧,可以使用介于 make -j4 到 make -j8 之間的命令吱七。
不出意外,當看到:#### build completed successfully (03:55:24 (hh:mm:ss)) ####
就整編成功了鹤竭。
運行模擬器
整編完踊餐,網上很多都是說直接運行命令:emulator
,但是我報以下錯誤了:
emulator: WARNING: Couldn't find crash service executable /media/ubuntu/disk/Project/AOSP9/prebuilts/android-emulator/linux-x86_64/emulator64-crash-service
emulator: WARNING: system partition size adjusted to match image file (1080 MB > 800 MB)
queryCoreProfileSupport: swap interval not found
failed to create drawable
failed to create drawable
failed to create drawable
failed to create drawable
failed to create drawable
failed to create drawable
failed to create drawable
failed to create drawable
getGLES2ExtensionString: Could not make GLES 2.x context current!
第一個警告不管,第二個警告可以 emulator 加參數(shù) -partition-size 2048
解決,也可以不管故爵,最后一個折騰了很久刁俭,可以加參數(shù)-gpu off
窜管,完整運行模擬器的命令:emulator -gpu off -partition-size 2048
散劫,這時應該又會報:
emulator: WARNING: Couldn't find crash service executable /media/ubuntu/disk/Project/AOSP9/prebuilts/android-emulator/linux-x86_64/emulator64-crash-service
emulator: 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.
只能重啟一下才能解決。
如果編譯完成后啟動模擬器時卡在黑屏幕帆,可以嘗試編譯其他的获搏!
emulator 還有很多參數(shù),可以用 emulator -help
查看失乾,參數(shù)如下:
-sysdir <dir> 為模擬器在<dir>目錄中搜索系統(tǒng)硬盤鏡像
-system <file> 為模擬器從<file>文件中讀取初始化系統(tǒng)鏡像
-datadir <dir> 設置用戶數(shù)據寫入的目錄
-kernel <file> 為模擬器設置使用指定的模擬器內核
-ramdisk <file> 設置內存RAM 鏡像文件(默認為<system>/ramdisk.img)
-image <file> 廢棄常熙,使用-system <file> 替代
-init-data <file> 設置初始化數(shù)據鏡像(默認為<system>/userdata.img)
-initdata <file> 和"-init-data <file>"使用方法一致
-data <file> 設置數(shù)據鏡像(默認為<datadir>/userdata-qemu.img)
-partition-size <size> system/data 分區(qū)容量大小(MB)
-cache <file> 設置模擬器緩存分區(qū)鏡像(默認為零時文件)
-no-cache 禁用緩存分區(qū)
-nocache 與"-no-cache"使用方法相同
-sdcard <file> 指定模擬器SDCard 鏡像文件(默認為<system>/sdcard.img)
-wipe-data 清除并重置用戶數(shù)據鏡像(從initdata 拷貝)
-avd <name> 指定模擬器使用Android 虛擬設備
-skindir <dir> 設置模擬器皮膚在<dir>目錄中搜索皮膚(默認為<system>/skins 目錄)
-skin <name> 選擇使用給定的皮膚
-no-skin 不適用任何模擬器皮膚
-noskin 使用方法與"-no-skin"相同
-memory <size> 物理RAM 內存大小(MB)
-netspeed <speed> 設置最大網絡下載、上傳速度
-netdelay <delay> 網絡時延模擬
-netfast 禁用網絡形態(tài)
-tarce <name> 代碼配置可用
-show-kernel 顯示內核信息
-shell 在當前終端中使用根Shell 命令
-no-jni Dalvik 運行時禁用JNI 檢測
-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 鍵盤翻轉圖
-radio 重定向無線模式接口到個性化設備
-port <port> 設置控制臺使用的TCP 端口
-ports <consoleport>,<adbport> 設置控制臺使用的TCP 端口和ADB 調試橋使用的TCP 端口
-onion <image> 在屏幕上層使用覆蓋PNG 圖片
-onion-alpha <%age> 指定上層皮膚半透明度
-onion-rotation 0|1|2|3 指定上層皮膚旋轉
-scale <scale> 調節(jié)模擬器窗口尺寸(三種:1.0-3.0碱茁、dpi症概、auto)
-dpi-device <dpi> 設置設備的resolution (dpi 單位) (默認165)
-http-proxy <proxy> 通過一個HTTP 或HTTPS 代理來創(chuàng)建TCP 連接
-timezone <timezone> 使用給定的時區(qū),而不是主機默認的
-dns-server <server> 在模擬系統(tǒng)上使用給定的DNS 服務
-cpu-delay <cpudelay> 調節(jié)CUP 模擬
-no-boot-anim 禁用動畫來快速啟動
-no-window 禁用圖形化窗口顯示
-version 顯示模擬器版本號
-report-console <socket> 向遠程socket 報告控制臺端口
-gps <device> 重定向GPS 導航到個性化設備
-keyset <name> 指定按鍵設置文件名
-shell-serial <device> 根shell 的個性化設備
-old-system 支持舊版本(pre 1.4)系統(tǒng)鏡像
-tcpdump <file> 把網絡數(shù)據包捕獲到文件中
-bootchart <timeout> bootcharting 可用
-qemu args.... 向qemu 傳遞參數(shù)
-qemu -h 顯示qemu 幫助
-verbose 和"-debug-init"相同
-debug <tags> 可用早芭、禁用調試信息
-debug-<tag> 使指定的調試信息可用
-debug-no-<tag> 禁用指定的調試信息
-help 打印出該幫助文檔
-help-<option> 打印出指定option 的幫助文檔
-help-disk-images 關于硬盤鏡像幫助
-help-keys 支持按鈕捆綁(手機快捷鍵)
-help-debug-tags 顯示出-debug <tag>命令中的tag 可選值
-help-char-devices 個性化設備說明
-help-environment 環(huán)境變量
-help-keyset-file 指定按鍵綁定設置文件
-help-virtula-device 虛擬設備管理
補充
模擬器運行需要四個文件,分別是:
- Linux Kernel
- system.img
- userdata.img
- ramdisk.img
上面我 lunch 命令時選擇的是 aosp_x86-eng诅蝶,因此 linux 默認使用的 AOSP/prebuilds/qemu-kernel/x86/kernel-qemu 下的 kernel-qemu退个,而其他文件則是使用的 AOSP/out/target/product/generic_x86 目錄下的 system.img、userdata.img调炬、ramdisk.img语盈。
單編
單編就是編譯某個模塊,比如 Setting缰泡,會在 out 目錄對應的產品有 Settings.apk刀荒,例如:out/target/product/generic_x86/system/priv-app/Settings/Settings.apk,這是可以直接安裝的棘钞。
1缠借、source build/envsetup.sh
2、lunch
3宜猜、選擇單編版本序號
4泼返、編譯
4.1、mmm
mmm packages/apps/Settings/
編譯指定目錄下的模塊姨拥,但不編譯它所依賴的其它模塊绅喉。
4.2、mm
先進入目錄:
cd packages/apps/Settings/
再編譯
mm
編譯當前目錄下的模塊叫乌,它和 mmm 一樣柴罐,不編譯依賴模塊。
4.3憨奸、mma
編譯當前目錄下的模塊及其依賴項革屠。
4.4、mmma [module_path]
編譯指定路徑下所有模塊,且包含依賴
4.5屠阻、make
make: 不帶任何參數(shù)則是編譯整個系統(tǒng)
make MediaProvider
單個模塊編譯红省,會把該模塊及其依賴的其他模塊一起編譯,會搜索整個源代碼來定位 MediaProvider 模塊所使用的 Android.mk 文件国觉,還要判斷該模塊依賴的其他模塊是否有修改吧恃。
make snod
如果我們修改代碼,想看效果麻诀,怎么辦痕寓?
1、可以在編譯完成后蝇闭,借助 adb install -r apk路徑
直接將生成的 apk 文件安裝到設備上呻率。
2、可以編譯好完成后呻引,再使用make snod
礼仗,重新生成 system.img,再運行模擬器也可逻悠。