本文介紹Android的通用編譯技巧。
手機廠商或者芯片平臺可能都封裝了編譯腳本命令,但是基本都沒有脫離Google的原始編譯邏輯。Google在開發者網站對相關命令有介紹。
可以參考Google的官方文檔 https://source.android.google.cn/setup/build/building
基礎編譯流程
對於AOSP源碼來講,通用的編譯流程如下:
cd /home/tanfuhai/data/code/android
source build/envsetup.sh # Add "lunch" (and other utilities and variables)
# to the shell environment.
lunch [-] # Choose the device to target.
m -j [] # Execute the configured build.
通用目標
在整個AOSP源碼內部,編譯通用格式:
make 目標
定義好了多個目標。通用的目標如下:
- dist: 將編譯目標構建之後,進行打包處理,服務器一般建議用此命令。
- superimage: 構建超級固件。
- systemimage: 構建系統固件。
- vendorimage: 構建vender固件。
- odmimage: 構建odm固件。
- productimage: 構建產品固件。
- clean: 類似於rm -rf out/
- checkbuild Build every module defined in the source tree
- droid :默認的Android目標
- nothing 不構建任何東西,只需解析並驗證構建結構
- java 編譯所有的java代碼。
- native Build all the native code in the source tree
- host Build all the host code (not to be run on a device) in the source tree
- target Build all the target code (to be run on the device) in the source tree
- (java|native)-(host|target)
- (host|target)-(java|native) Build the intersection of the two given arguments
- snod:快速編譯systemimage,不會進行依賴構建
- vnod:快速編譯vendorimage,不會進行依賴構建。
- pnod: 快速編譯productimage,不會進行依賴構建。
- psnod: 快速編譯productserviceimage,不會進行依賴構建。
- onod: 快速編譯odmimage,不會進行依賴構建。
如果make後面沒有跟目標,默認目標是droid
常用編譯目標
除了通用編譯目標之外,實際工作中,可能針對不同的模塊或者目標需要進行具體的指令變更。簡單介紹常用的一些技巧:
修改framework相關
a. 如果當前的源碼和手機內部版本一致,可以通過adb sync 分區來進行同步。
make framework-res framework services -j16 // 根據情況擇優編譯
adb sync system
adb shell am restart // 重啓用戶空間,修改的內容會生效。
b. 如果本地源碼和手機版本不一致。
make installclean -j16 // 此步驟會刪除out/target/product/XXX/下生成的相關安裝文件。比如apk、bin等
make framework services -j16
adb sync system
adb shell am restart // 重啓用戶空間,修改的內容會生效。
如果不想刪除當前產品生成的相關文件,可以通過adb push --sync 命令來同步文件到手機。
make framework services -j16
adb push --sync out/target/product/jide_pro2_lte_r/system/framework/ /system/
執行過程中可以將所有命令寫到一起,通過分號隔開。
注意:如果同步過程中提示空間不足,可以先執行 adb shell am restart ,然後在執行 adb sync + 分區。
修改了apk
make 模塊名 -j16
make Launcher3QuickStep -j16
adb sync product
adb shell pm clear com.android.launcher3
或者adb shell am force-stop com.android.launcher3
修改kernel
make bootimage -j32
adb reboot bootloader
fastboot flash boot boot_image.img
修改selinux相關
- Android O之前
make bootimage -j32
- Android O之後 (Treble架構)
Treble架構的原因,Selinux被拆分到不同的分區。編譯方式發生了變化:
make selinux_policy -j32
如果可以直接連接手機,執行:
adb sync
否則,將如下對應目錄的所有文件push到手機對應的位置即可。
- out/target/product/generic/odm/etc/selinux
- out/target/product/generic/vendor/etc/selinux
- out/target/product/generic/product/etc/selinux
- out/target/product/generic/system/etc/selinux
更改了property屬性
AOSP默認沒有一個通用目標可以方便編譯所有的*.prop文件。爲了方便編譯所有的目標, 需要給Makefile打上補丁。
diff --git a/core/Makefile b/core/Makefile
index 3f98ee400..ef3f406b2 100755
--- a/core/Makefile
+++ b/core/Makefile
@@ -679,6 +679,13 @@ endif
.PHONY: package-stats
package-stats: $(PACKAGE_STATS_FILE)
+
+BUILD_TO_PROP := \
+ $(sort $(filter $(TARGET_OUT)/% $(TARGET_OUT_DATA)/%, \
+ $(filter %.prop, $(ALL_DEFAULT_INSTALLED_MODULES))))
+.PHONY: buildprop
+buildprop: $(BUILD_TO_PROP)
+
然後執行:
make buildprop -j32
adb sync // 同步結果到手機
編譯目標分區所有的apk和jar文件
make package-stats -j32
adb sync