安卓10添加代码生成可执行文件作为服务启动

首先添加文件目录system/core/zxytest/,然后在此目录下添加文件Android.bp 和 zxytest.cpp,文件内容如下,Android.bp内容

cc_binary {
          
   
    name: "zxytest",
    srcs: ["zxytest.cpp"],
    cflags: [
        "-Wall",
        "-Wextra",
        "-Werror",
    ],
    shared_libs: [
        "libbase",
        "libutils",
        "libcutils",
        "liblog",
    ],
}

然后是zxytest.cpp文件,我这是一个判断某些路径是否存在,或者文件是否存在的功能

#include <stdlib.h>
#include <android-base/logging.h>
#include <fcntl.h>
#include <android-base/properties.h>
#include <cutils/properties.h>
#include <utils/Log.h>

int main( int argc, char *argv[] )
{
          
   
    LOG(INFO) << "zxytest service start" << argc;
    LOG(INFO) << "argv = " << argv[0];

    static const char filepath[] = "/oem/media/bootanimation.zip";

    ALOGE("zxy start get file
");

    if ( access("/oem",F_OK) == 0 ) ALOGE("zxy dir /oem is existence
");
    else ALOGE("zxy dir /oem is not existence
");

    if ( access("/oem/media",F_OK) == 0 ) ALOGE("zxy dir /oem/media is existence
");
    else ALOGE("zxy dir /oem/media is not existence
");

    if ( access("/oem/oem.prop",F_OK) == 0 ) ALOGE("zxy dir /oem/oem.prop is existence
");
    else ALOGE("zxy dir /oem/oem.prop is not existence
");
    if ( access(filepath,F_OK) == 0 ) {
          
   
        ALOGE("zxy file %s is existence
",filepath);
    } else ALOGE("zxy file %s is not existence
",filepath);

    if (access(filepath, R_OK) == 0) {
          
   
        ALOGE("zxy read file %s is ok
",filepath);
     } else {
          
   
         ALOGE("zxy read file %s is failed
",filepath);
     }
    return 0;
}

添加完成源文件后,然后就是添加编译了,加入我们的目标文件

diff --git a/android/device/softwinner/ceres-c3/ceres_c3.mk b/android/device/softwinner/ceres-c3/ceres_c3.mk
index 499430b8ce..5443848a7b 100644
--- a/android/device/softwinner/ceres-c3/ceres_c3.mk
+++ b/android/device/softwinner/ceres-c3/ceres_c3.mk
@@ -174,7 +174,8 @@ PRODUCT_PROPERTY_OVERRIDES += 


 PRODUCT_PACKAGES += 
-    SoundRecorder
+    SoundRecorder 
+    zxytest

 PRODUCT_PACKAGES += 
        eext

然后修改init.rc脚本文件,启动我们的服务

diff --git a/android/device/softwinner/ceres-common/init.sun50iw10p1.rc b/android/device/softwinner/ceres-common/init.sun50iw10p1.rc
index a0e5ec7c7c..1c0db3eab8 100644
--- a/android/device/softwinner/ceres-common/init.sun50iw10p1.rc
+++ b/android/device/softwinner/ceres-common/init.sun50iw10p1.rc
@@ -82,6 +82,8 @@ on boot
     mkdir /oem/media 0777 system system
     chown -R system:system /oem/media
     chmod 0644 /oem/media/bootanimation.zip
+
+    start zxytest

 on post-fs-data
     # create file for audio dump data
@@ -128,3 +130,9 @@ service mciputils /system/bin/mciputils
        group root
        oneshot
        disabled
+
+service zxytest /system/bin/zxytest
+        class main
+        user root
+        group root
+        oneshot

接着添加android/device/softwinner/common/sepolicy/vendor/zxytest.te文件,文件内容如下

type zxytest, domain, coredomain;
type zxytest_exec, exec_type, system_file_type, file_type;
init_daemon_domain(zxytest)

allow zxytest unlabeled:dir {
          
    search };

然后修改file_contexts文件,否则无法启动

diff --git a/android/device/softwinner/common/sepolicy/vendor/file_contexts b/android/device/softwinner/common/sepolicy/vendor/file_contexts
index 08c4c988fd..51afb84bc1 100644
--- a/android/device/softwinner/common/sepolicy/vendor/file_contexts
+++ b/android/device/softwinner/common/sepolicy/vendor/file_contexts
@@ -190,4 +190,4 @@
 #systemmixservice
 /system/bin/systemmixservice  u:object_r:systemmix_exec:s0

-
+/system/bin/zxytest  u:object_r:zxytest_exec:s0

这样就算好了,一个服务就算添加成功了

经验分享 程序员 微信小程序 职场和发展