本文介绍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