《AOSP 源碼下載》完成后,就可以開編了。
整編
整編,顧名思義就是編譯整個 Android 源碼,最終 out 目錄會生成幾個重要的鏡像文件,其中有 system.img、userdata.img、ramdisk.img 等,這些是可以刷機的。
初始化編譯環境
項目目錄,打開終端,輸入命令:
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 指的是特定功能的組合的特定名稱,即表示編譯出的鏡像可以運行在什么環境。其中 aosp(Android Open Source Project)代表 Android 開源項目;arm 表示系統是運行在 arm 架構的處理器上,arm64 則是指 64 位 arm 架構處理器,x86 則表示 x86 架構的處理器,更多設備代碼和編譯目標參考官方文檔。
什么是 BUILDTYPE
BUILDTYPE 則指的是編譯類型,通常有三種:
- user:用來正式發布到市場的版本,權限受限,如沒有 root 權限,不能 dedug 等。
- userdebug:在user版本的基礎上開放了 root 權限和 debug 權限。
- eng:代表 engineer,開發工程師的版本,擁有最大的權限(root等),具有額外調試工具的開發配置。
如果沒有谷歌手機設備,可以選擇 arm 或者 x86,我選擇了 aosp_x86-eng,編譯完后運行模擬器看看,因此這里選擇序號 26。
開始編譯
通過 make 指令進行代碼編譯:
make -j8
其中 -jN 參數表示處理並行任務,通常使用的任務數 N 介於編譯時所用計算機上硬件線程數的 1-2 倍之間。
查看計算機上的核心數:
cat /proc/cpuinfo | grep processor
processor : 0
processor : 1
processor : 2
processor : 3
可看到創建的虛擬機 CPU 核心共有 4 個,那么要實現最快的編譯速度,可以使用介於 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 加參數 -partition-size 2048
解決,也可以不管,最后一個折騰了很久,可以加參數-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 還有很多參數,可以用 emulator -help
查看,參數如下:
-sysdir <dir> 為模擬器在<dir>目錄中搜索系統硬盤鏡像
-system <file> 為模擬器從<file>文件中讀取初始化系統鏡像
-datadir <dir> 設置用戶數據寫入的目錄
-kernel <file> 為模擬器設置使用指定的模擬器內核
-ramdisk <file> 設置內存RAM 鏡像文件(默認為<system>/ramdisk.img)
-image <file> 廢棄,使用-system <file> 替代
-init-data <file> 設置初始化數據鏡像(默認為<system>/userdata.img)
-initdata <file> 和"-init-data <file>"使用方法一致
-data <file> 設置數據鏡像(默認為<datadir>/userdata-qemu.img)
-partition-size <size> system/data 分區容量大小(MB)
-cache <file> 設置模擬器緩存分區鏡像(默認為零時文件)
-no-cache 禁用緩存分區
-nocache 與"-no-cache"使用方法相同
-sdcard <file> 指定模擬器SDCard 鏡像文件(默認為<system>/sdcard.img)
-wipe-data 清除並重置用戶數據鏡像(從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 禁用網絡形態
-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> 調節模擬器窗口尺寸(三種:1.0-3.0、dpi、auto)
-dpi-device <dpi> 設置設備的resolution (dpi 單位) (默認165)
-http-proxy <proxy> 通過一個HTTP 或HTTPS 代理來創建TCP 連接
-timezone <timezone> 使用給定的時區,而不是主機默認的
-dns-server <server> 在模擬系統上使用給定的DNS 服務
-cpu-delay <cpudelay> 調節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)系統鏡像
-tcpdump <file> 把網絡數據包捕獲到文件中
-bootchart <timeout> bootcharting 可用
-qemu args.... 向qemu 傳遞參數
-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 環境變量
-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: 不帶任何參數則是編譯整個系統
make MediaProvider
單個模塊編譯,會把該模塊及其依賴的其他模塊一起編譯,會搜索整個源代碼來定位 MediaProvider 模塊所使用的 Android.mk 文件,還要判斷該模塊依賴的其他模塊是否有修改。
make snod
如果我們修改代碼,想看效果,怎么辦?
1、可以在編譯完成后,借助 adb install -r apk路徑
直接將生成的 apk 文件安裝到設備上,如果不是 APK,直接 push 對應的文件。
2、可以編譯好完成后,再使用make snod
,重新生成 system.img,再運行模擬器也可。
公眾號
我的公眾號:吳小龍同學,歡迎交流~