Android-failed to set system property

1. 现象

failed to set system property,无法写属性值的闪退报错

2019-10-09 18:56:20.020 10252-10252/com.fadi.gps_switch W/.fadi.gps_switch: type=1400 audit(0.0:211): avc: denied { write } for name="com.fadi.gps_switch-Ox-iQk-Dkwj06P13_L--fg==" dev="mmcblk0p46" ino=606212 scontext=u:r:system_app:s0 tcontext=u:object_r:apk_data_file:s0 tclass=dir permissive=0

2019-10-09 18:58:14.680 10252-10252/com.fadi.gps_switch W/libc: Unable to set property "persist.fadi.gps" to "true": error code: 0x18
2019-10-09 18:58:14.680 10252-10252/com.fadi.gps_switch D/AndroidRuntime: Shutting down VM
    
    
    --------- beginning of crash
2019-10-09 18:58:14.686 10252-10252/com.fadi.gps_switch E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.fadi.gps_switch, PID: 10252
    java.lang.RuntimeException: failed to set system property
        at android.os.SystemProperties.native_set(Native Method)
        at android.os.SystemProperties.set(SystemProperties.java:196)
        at com.fadi.gps_switch.MainActivity.setGpsPowerSave(MainActivity.java:73)
        at com.fadi.gps_switch.MainActivity.access$000(MainActivity.java:10)
        at com.fadi.gps_switch.MainActivity$1.onCheckedChanged(MainActivity.java:47)
        at android.widget.CompoundButton.setChecked(CompoundButton.java:180)
        at android.support.v7.widget.SwitchCompat.setChecked(SwitchCompat.java:1060)
        at android.support.v7.widget.SwitchCompat.toggle(SwitchCompat.java:1055)
        at android.widget.CompoundButton.performClick(CompoundButton.java:140)
        at android.view.View.performClickInternal(View.java:7123)
        at android.view.View.access$3500(View.java:807)
        at android.view.View$PerformClick.run(View.java:27540)
        at android.os.Handler.handleCallback(Handler.java:883)
        at android.os.Handler.dispatchMessage(Handler.java:100)
        at android.os.Looper.loop(Looper.java:214)
        at android.app.ActivityThread.main(ActivityThread.java:7486)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:980)

3.代码调用

    /**
     * adb shell getprop persist.sys.fadi.gps
     */
    private boolean isGpsPowerSaveOn(){
        boolean resulut = false;

        String info = SystemProperties.get(GPS_KEY, "false").trim().toString();
        Log.d(TAG, "info");
        resulut = "true".equals(info);

        return resulut;
    }

    /**
     * adb shell setprop persist.sys.fadi.gps
     */
    private void setGpsPowerSave(boolean value) {
        SystemProperties.set(GPS_KEY, value ? "true" : "false");
    }

3. 分析

上述需满足下述条件:

  1. SE-LINUX
  2. android:sharedUserId=“android.uid.system”
  3. 系统签名

我这边是违反了,属性key为 persist.fadi.gps,需要定义相应的Se-linux权限。

如果不想额外定义,可以修改为persist.sys前缀开头就行;参考依据来源于源码定义

system/sepolicy/private/property_contexts

persist.sys.            u:object_r:system_prop:s0

即修改为 persist.sys.fadi.gps 即可

你可能感兴趣的:(Android系统)