由于作者本人初次接触Android,所以本文所述内容只是操作方法,没有深究原理。各位可自行深入学习研究。
需要在Android开机启动之后,自动执行一个脚本,假设这个脚本是test.sh。脚本中只执行1条打印:
#!/bin/bash
echo "auto start test.sh!"
根据需求描述,可以推导有两件事情要做:
service test /system/app/test.sh
class main
user root
group root
on property:sys.boot_completed=1
start test
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
$(shell cp -af $(LOCAL_PATH)/test.sh $(TARGET_OUT)/app)
$(shell cp -af $(LOCAL_PATH)/test.rc $(TARGET_OUT)/etc/init)
static const struct fs_path_config android_files[] = {
//省略其他不相关权限配置
{ 00755, AID_ROOT, AID_SHELL, 0, "system/app/*"},//给/system/app下的文件赋予755权限
//省略其他不相关权限配置
};
按照第3节步骤操作完成后,通过adb shell指令,查看日志,如下:
test:/ $ cd system/app
test:/system/app $ ls -l
-rwxr-xr-x 1 root shell 26 2022-02-09 20:13 test.sh
test:/system/app $ logcat |grep test.sh
01-01 08:00:16.657 0 0 E init : Could not start service 'test' as part of class 'main': File /system/app/test.sh(labeled "u:object_r:system_file:s0") has incorrect label or no domain transition from u:r:init:s0 to another SELinux domain defined. Have you configured your service correctly? https://source.android.com/security/selinux/device-policy#label_new_services_and_address_denials
02-10 11:10:22.710 0 0 I init : Command 'start test' action=sys.boot_completed=1 (/system/etc/init/test.rc:7) took 0ms and failed: Could not start service: File /system/app/test.sh(labeled "u:object_r:system_file:s0") has incorrect label or no domain transition from u:r:init:s0 to another SELinux domain defined. Have you configured your service correctly? https://source.android.com/security/selinux/device-policy#label_new_services_and_address_denials
可以看到确实自动执行了test.rc文件,只不过在执行/system/app/test.sh时出错了,原因是SELinux的问题。这部分我还没有深入研究,待后期完善。只要解决了SELinux的权限问题,就可以正常启动test.sh了。