app以root身法执行shell脚本

  • 写一个xxxsh的shell脚本文件,放到android系统的/system/bin目录下
#! /system/bin/sh
echo "hello"

可以在device/厂家/项目/device.mk里面加如下代码来拷贝

PRODUCT_COPY_FILES += \
         xxxshPath/xxxsh:system/bin/xxxsh
 #end Install
  • 添加一个service,在device/厂家/项目/init.项目.rc文件里添加如下代码
service xxxsh /system/bin/xxxsh
    user root
    group root
    disabled
    oneshot
  • app需要系统platform签名(Android.mk有定义LOCAL_CERTIFICATE := platform)
  • app的AndroidManifest.xml要有定义android:sharedUserId="android.uid.system"
  • app的调用代码,
import android.os.SystemProperties;
SystemProperties.set("ctl.start", "xxxsh:0");

冒号后面是传入给脚本的参数。

  • 如果调用不成功,很可能是selinux的权限问题,进入adb的shell环境,手动关掉selinux再试
setenforce 0 #临时关掉selinux
setprop ctl.start xxxsh #手动调用运行shell脚本
  • 如果是selinux的权限问题导致调用不成功,就去添加需要的权限。

猜你喜欢

转载自my.oschina.net/kyle960/blog/1790244