环境
aosp: android-14.0.0_r1
真机:Pixel5
目标
测试:将一个shell脚本,打入到rom中,最终刷机,并能在手机中使用。
步骤
- 添加 PRODUCT_PACKAGES
# 路径:device/google/redfin/device-redfin.mk
PRODUCT_PACKAGES += mysh
- 创建mysh文件(无后缀)
路径:device/google/redfin/prebuilt/mysh
#!/system/bin/sh
current_time=$(date +"%Y-%m-%d %H:%M:%S")
echo "gn--->time=: $current_time" >> /data/local/tmp/gn.log
- 新增Android.mk构建模板文件
路径:device/google/redfin/prebuilt/Android.mk
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := mysh
LOCAL_SRC_FILES := mysh
LOCAL_MODULE_PATH := $(TARGET_OUT)/bin
LOCAL_MODULE_TAGS := optional
LOCAL_MODULE_CLASS := EXECUTABLES
include $(BUILD_PREBUILT)
- 设置权限
路径:system/sepolicy/prebuilts/api/34.0/private/file_contexts
和system/sepolicy/private/file_contexts
这两个路径都需要新增下面内容
/system/bin/mysh u:object_r:system_file:s0
- 设置init.rc
路径:system/core/rootdir/init.rc
on property:sys.boot_completed=1
start myserver
service myserver /system/bin/mysh
class main
seclabel u:r:system_file:s0
user root
disabled
oneshot
- 禁用artifact_path_requirements检测
路径:build/envsetup.sh
# disable artifact check
export DISABLE_ARTIFACT_PATH_REQUIREMENTS="true"
如果不禁用的话,make编译的时候,会遇到如下报错:
FAILED:
build/make/core/artifact_path_requirements.mk:30: warning: device/google/redfin
/aosp_redfin.mk produces files inside build/make/target/product/generic_system.m
ks artifact path requirement.
Offending entries:
system/bin/mysh
In file included from build/make/core/main.mk:1373:
build/make/core/artifact_path_requirements.mk:30: error: Build failed.
19:26:07 ckati failed with: exit status 1
#### failed to build some targets (01:39 (mm:ss)) ####
编译打包
最后编译打包,最好先清理掉所有的out,否则容易影响成包。
编译:
make clean
source build/envsetup.sh
lunch <我这里选择的是pixel5对应的userdebug版本>
make
编译成功后,可以在out中找到mysh,路径:out/target/product/redfin/system/bin/mysh
刷机:
adb reboot fastboot
fastboot devices
fastboot flashall -w
当然不要忘记设置ANDROID_PRODUCT_OUT
结果验证
最终在手机中,可以在/system/bin中找到mysh运行脚本。
shell进去,运行mysh,可以在/data/local/tmp中生成gn.log文件,里面是打印当前时间。则说明验证通过