Android之系统属性(SystemProperties)

一、系统属性

        系统属性是系统中具有特殊含义的键值对数据,我们在开发过程中有时需要使用系统属性,例如获取系统软件版本,获取设备名名称等,有时也需要设置自定义属性。系统属性具有全局性的特点,存取方便。

二、获取和设置

2.1 架构

2.2 使用终端

//获取系统属性值
getprop my.prop.test

//设置系统属性值
setprop my.prop.test

//监听系统属性变化
watchprops my.prop.test

2.3 Java代码中

        在Android源码中的android.os下有一个隐藏类:SystemProperties,通过SystemProperties.set及SystemProperties.get可以设置和获取系统属性。        

Systemproperties部分源码如下:frameworks\base\core\java\android\os\SystemProperties.java

...
public class SystemProperties {
...
    //获取属性key的值,如果没有该属性则返回默认值def
    @SystemApi
    public static String get(@NonNull String key, @Nullable String def) {
        if (TRACK_KEY_ACCESS) onKeyAccess(key);
        return native_get(key, def);
    }
...
    //设置属性key的值为val
    @SystemApi
    public static void set(@NonNull String key, @Nullable String val) {
        if (val != null && !val.startsWith("ro.") && val.length() > PROP_VALUE_MAX) {
            throw new IllegalArgumentException("value of system property '" + key
                    + "' is longer than " + PROP_VALUE_MAX + " characters: " + val);
        }
        if (TRACK_KEY_ACCESS) onKeyAccess(key);
        native_set(key, val);
    }
....
}

但是该类的接口也不是对外开放的,需要通过反射的方式来使用。下面是我的一个 SystemProperties 工具类:

public class SystemProperties {

    private final static String TAG = "SystemProperties";

    public static String get(String key) {
        Class<?> SysProp = null;
        Method method = null;
        String value = null;
        try {
            SysProp = Class.forName("android.os.SystemProperties");
            method = SysProp.getMethod("get", String.class);
            value = (String) method.invoke(null, key);
            Log.i(TAG, "value:" + value);
        } catch (Exception e) {
            Log.e(TAG,"read SystemProperties error",e);
        }
        return value;
    }

    public static String get(String key, String defaultValue) {
        Class<?> SysProp = null;
        Method method = null;
        String value = null;
        try {
            SysProp = Class.forName("android.os.SystemProperties");
            method = SysProp.getMethod("get", String.class, String.class);
            value = (String) method.invoke(null, key, defaultValue);
        } catch (Exception e) {
            Log.e(TAG,"read SystemProperties error",e);
        }
        return value;
    }

    public static int getInt(String key, int defaultValue) {
        Class<?> SysProp = null;
        Method method = null;
        int value = 0;
        try {
            SysProp = Class.forName("android.os.SystemProperties");
            method = SysProp.getMethod("getInt", String.class, int.class);
            value = (Integer) method.invoke(null, key, defaultValue);
        } catch (Exception e) {
            Log.e(TAG,"read SystemProperties error",e);
        }
        return value;
    }

    public static long getLong(String key, long defaultValue) {
        Class<?> SysProp = null;
        Method method = null;
        long value = 0;
        try {
            SysProp = Class.forName("android.os.SystemProperties");
            method = SysProp.getMethod("getLong", String.class, long.class);
            value = (Long) method.invoke(null, key, defaultValue);
        } catch (Exception e) {
            Log.e(TAG,"read SystemProperties error",e);
        }
        return value;
    }

    public static boolean getBoolean(String key, boolean defaultValue) {
        Class<?> SysProp = null;
        Method method = null;
        boolean value = false;
        try {
            SysProp = Class.forName("android.os.SystemProperties");
            method = SysProp.getMethod("getBoolean", String.class, boolean.class);
            value = (Boolean) method.invoke(null, key, defaultValue);
        } catch (Exception e) {
            Log.e(TAG,"read SystemProperties error",e);
        }
        return value;
    }

    public static void set(String key, String value) {
        Class<?> SysProp = null;
        Method method = null;
        try {
            SysProp = Class.forName("android.os.SystemProperties");
            method = SysProp.getMethod("set", String.class, String.class);
            method.invoke(null, key, value);
        } catch (Exception e) {
            Log.e(TAG,"read SystemProperties error",e);
        }
    }

    public static void addChangeCallback(Runnable runnable) {
        Class<?> SysProp = null;
        Method method = null;
        try {
            SysProp = Class.forName("android.os.SystemProperties");
            method = SysProp.getMethod("addChangeCallback", Runnable.class);
            method.invoke(null, runnable);
        } catch (Exception e) {
            Log.e(TAG,"read SystemProperties error",e);
        }
    }
}

下面以ActivityManagerService为例,frameworks\base\services\core\java\com\android\server\am\ActivityManagerService.java
列举源码中使用prop的例子:

final void finishBooting() {
  ...
  //设置开机完成标志属性sys.boot_completed
  SystemProperties.set("sys.boot_completed", "1");
}
...
private static void maybePruneOldTraces(File tracesDir) {
  ...
  //获取tombstoned.max_anr_count属性值
  final int max = SystemProperties.getInt("tombstoned.max_anr_count", 64);
}

 2.4 C++代码中

下面以MPEG4Writer.cpp为例,

frameworks\av\media\libstagefright\MPEG4Writer.cpp
列举源码中使用prop的例子:

#include <cutils/properties.h>
    ...
void MPEG4Writer::addDeviceMeta() {
    ...
    if (property_get("ro.build.version.release", val, NULL)
    ...
        if (property_get("ro.product.model", val, NULL)
    ...
}

在C++代码中使用prop需要:

  1. include <cutils/properties.h>
  2. Android.mk或Android.bp或Makefile中需要链接libcutils库frameworks\av\media\libstagefright\Android.bp
shared_libs: [
        ...
        "libcutils",
        ...

properties.h源码在:system/core/libcutils/include/cutils/properties.h

int property_get(const char* key, char* value, const char* default_value);
int property_set(const char *key, const char *value);
int property_list(void (*propfn)(const char *key, const char *value, void *cookie), void *cookie);

三、特殊属性

3.1 ro只读属性

ro即read only这类属性通常是系统默认属性,在系统编译或初始化时设置的。

$ getprop ro.vendor.build.version.release
10
$ setprop ro.vendor.build.version.release 9
setprop: failed to set property 'ro.vendor.build.version.release' to '9'

3.2 persist持久属性

设置persist开头的属性,断电后仍能保存,值写入data/property/persistent_properties。

$ getprop persist.hello.test  //属性为空
$ setprop persist.hello.test abc //设置属性persist.hello.test值为abc
$ getprop persist.hello.test abc //属性get正常
abc
$reboot //重启设备
$ getprop persist.hello.test //属性为abc
abc

3.3 ctl 控制属性

setprop ctl.start xxx //启动某服务
setprop ctl.stop xxx  //关闭某服务
setprop ctl.restart xxx  //重启某服务

3.4 sys.powerctl属性

sys.powerctl属性可控制设备重启关机

setprop sys.powerctl shutdown //设备关机
setprop sys.powerctl reboot //设备重启

3.5 普通属性

设置其他格式开头的属性,断电后不能保存

$ getprop hello.test  //属性为空
$ setprop hello.test 123//设置属性persist.hello.test值为abc
$ getprop hello.test 123//属性get正常
123
$reboot //重启设备
$ getprop hello.test //属性为空

3.7 添加系统默认属性

系统开机时会加载 *.prop属性配置文件中的属性,因此开机后就有了默认属性。init进程中property_load_boot_defaults(load_debug_prop)函数分析。

位置:system\core\init\property_service.cpp

void property_load_boot_defaults(bool load_debug_prop) {
    std::map<std::string, std::string> properties;
    //加载属性build.prop、default.prop至properties
    if (!load_properties_from_file("/system/etc/prop.default", nullptr, &properties)) {
        // Try recovery path
        if (!load_properties_from_file("/prop.default", nullptr, &properties)) {
            // Try legacy path
            load_properties_from_file("/default.prop", nullptr, &properties);
        }
    }
    load_properties_from_file("/system/build.prop", nullptr, &properties);
    load_properties_from_file("/vendor/default.prop", nullptr, &properties);
    load_properties_from_file("/vendor/build.prop", nullptr, &properties);
    if (SelinuxGetVendorAndroidVersion() >= __ANDROID_API_Q__) {
        load_properties_from_file("/odm/etc/build.prop", nullptr, &properties);
    } else {
        load_properties_from_file("/odm/default.prop", nullptr, &properties);
        load_properties_from_file("/odm/build.prop", nullptr, &properties);
    }
    load_properties_from_file("/product/build.prop", nullptr, &properties);
    load_properties_from_file("/product_services/build.prop", nullptr, &properties);
    load_properties_from_file("/factory/factory.prop", "ro.*", &properties);
    ...
    //将properties中属性通过PropertySet存入属性属性
    for (const auto& [name, value] : properties) {
        std::string error;
        if (PropertySet(name, value, &error) != PROP_SUCCESS) {
            LOG(ERROR) << "Could not set '" << name << "' to '" << value
                       << "' while loading .prop files" << error;
        }
    }
    property_initialize_ro_product_props();//初始化ro_product前缀的只读属性
    property_derive_build_props();//初始化编译相关属性
    update_sys_usb_config();//设置persist.sys.usb.config属性,用于控制USB调试和文件传输功能
}

从上面代码可以看出从多个属性文件.prop中将系统属性加载至properties变量,再通过PropertySet()将properties添加到系统中,并初始化只读、编译、usb相关属性值。在PropertySet()中是通过Socket与属性服务进行通信,将props存储共享内存。

下面举一个自己在device/google/marlin/system.prop中添加系统属性的例子:

# 添加自己的系统默认属性
persist.hello.world=hello

注意:这里添加的属性前缀必须是在system/sepolicy/private/property_contexts中被定义过的,否则无效。make android后在out/target/product/xxx/system/build.prop 或out/target/product/xxx/vendor/build.prop可找到添加的属性persist.hello.world,则说明基本添加成功。

四、添加定制的*.prop

涉及的代码路径汇总如下:

device/qcom/qssi/hello.prop
device/qcom/qssi/qssi.mk
device/qcom/sepolicy/generic/private/property_contexts
system/core/rootdir/init.rc
system/core/init/property_service.cpp

为了方便统一管理定制化属性,有时会将定制化属性都写在定制的.prop文件中,下面以添加hello.prop为例说明添加过程。

4.1 device下添加hello.prop

device/qcom/qssi/hello.prop

#
# system.prop for qssi
#
ro.hello.year=2022    //添加ro属性
persist.hello.month=07    //添加persist属性
hello.day=25    //添加hello属性

ro.product.model=HelloWorld    //定制系统已有ro.product.model属性

4.2 配置预置路径

修改device下的device.mk来指定hello.prop的预置路径device/qcom/qssi/qssi.mk

#将hello.prop预置到system/hello.prop
PRODUCT_COPY_FILES += \
    device/qcom/qssi/hello.prop:system/hello.prop

4.3 SELinux权限配置

hello.开头的属性是新添加的配置,需要在配置对应的SELinux规则,否则无效,配置方法如下:device/qcom/sepolicy/generic/private/property_contexts

hello.                             u:object_r:system_prop:s0

4.4 配置hello.prop权限

此步骤可省略,若未配置读写权限,默认system/prop为644这里配置与system/build.prop相同的600权限 system/core/rootdir/init.rc

on fs
   chmod 0600 /system/hello.prop

4.5 加载hello.prop

仅仅将hello.prop预置到system/hello.prop还不够,系统启动时需要load hello.prop才能使其生效system/core/init/property_service.cpp

load_properties_from_file("/system/build.prop", nullptr, &properties);
   load_properties_from_file("/vendor/default.prop", nullptr, &properties);
   load_properties_from_file("/vendor/build.prop", nullptr, &properties);
   if (SelinuxGetVendorAndroidVersion() >= __ANDROID_API_Q__) {
       load_properties_from_file("/odm/etc/build.prop", nullptr, &properties);
   } else {
       load_properties_from_file("/odm/default.prop", nullptr, &properties);
       load_properties_from_file("/odm/build.prop", nullptr, &properties);
   }
   load_properties_from_file("/product/build.prop", nullptr, &properties);
   load_properties_from_file("/product_services/build.prop", nullptr, &properties);
   load_properties_from_file("/factory/factory.prop", "ro.*", &properties);
   //load 预置的hello.prop ,最后load保证其配置属性优先级更高
   load_properties_from_file("/system/hello.prop", nullptr, &properties);

4.6 验证

Android全编译后正常情况可找到生成的out/target/product/qssi/system/hello.prop

检查其内容应与device/qcom/qssi/hello.prop内容保持一致。

在C语言中,要读取Android系统的System Properties(系统属性),你可以使用JNI(Java Native Interface)技术,它是Java和原生代码交互的一种方式。以下是一个简单的步骤: 1. 首先,你需要包含必要的头文件: ```c #include <jni.h> #include <android/log.h> // 日志打印 #include <sys/system_properties.h> // 系统属性访问库 ``` 2. 定义一个函数,这个函数将作为JNI的入口点,接收JNIEnv指针和一个String类型的参数(property name): ```c static jstring ReadSystemProperty(JNIEnv *env, jobject /* This */, const char* propName) { // ... } ``` 3. 使用`readlink()`或者`PROPERTY_GET()`宏从`system_properties`库获取属性值: ```c char* value = property_get(propName, "", ""); // 获取指定属性名的值,默认为空字符串 ``` 4. 转换成Java String并返回: ```c jstring javaValue = env->NewStringUTF(value); // 将C字符串转为Java字符串 // 如果需要,可以使用Logcat打印日志 const char* tag = "MyActivity"; __android_log_print(ANDROID_LOG_INFO, tag, "System Property: %s", value); return javaValue; ``` 5. 最后,在JNI注册函数中声明并注册这个函数,以便在Java中调用: ```c JNIEXPORT jstring JNICALL Java_com_your_package_NameOfYourClass_readSystemProperty(JNIEnv *env, jobject obj, jstring propNameStr) { // 使用env的转换函数将Java字符串转为C字符串 const char* propName = (*env)->GetStringUTFChars(env, propNameStr, NULL); // 实现ReadSystemProperty函数并处理错误 jstring result = ReadSystemProperty(env, obj, propName); // ...释放资源并返回结果 } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

A-sL1

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值