native 程序配置init rc启动
一.编写一个 native 程序
一个简单的 native 程序一般要添加两个文件:cpp文件(也可以是.c文件),make文件(以前是 android.mk,现在是 android.bp)
下面是 main.cpp
#include<stdio.h> int main(){ int i=0; printf("Hello world\n"); return 0; }
下面是 android.bp
cc_binary { name: "helloworld", srcs: [ "main.cpp", ], shared_libs: [ "libgui", ] }
接下来就是编译native程序了,一种是将native程序放到android 源码环境里面进行编译,需要在make 里面增加如下代码:
PRODUCT_PACKAGES += \ helloworld \
这样就可以在源码环境中将native程序编译出来了。
另外一种就是使用 llvm 进行编译,需要预先下载好NDK,然后借助NDK来编译代码,编译命令如下:
\ndk\25.1.8937393\toolchains\llvm\prebuilt\windows-x86_64\bin\aarch64-linux-android26-clang main.cpp -o helloworld
这种方式就不需要写bp文件,添加模块了,但是这种方式涉及到依赖时就比较麻烦了,如果只是验证某些demo,推荐使用这种,方便快捷
二. 配置init rc 启动
1.编写rc启动脚本:
service helloworld /system/bin/helloworld class main disabled oneshot on property:sys.boot_completed=1 start helloworld
2.配置selinux权限:
在file_contexts 文件中添加:
/system/bin/helloworld u:object_r:helloworld_exec:s0
自建一个 helloworld.te 文件
type helloworld, domain, coredomain; type helloworld_exec, file_type, system_file_type, exec_type; init_daemon_domain(helloworld)
基本上这个程序就能开机自动启动了,可能还会有一些selinux denied需要添加,这个看代码做了哪些操作来决定