【Android】android.os.SystemProperties 完整文档

【Android】android.os.SystemProperties 完整文档 android.os.SystemProperties完整文档1. 概述android.os.SystemProperties是 Android 系统用于读写系统属性的工具类位于android.os包下。特性说明包路径android.os.SystemProperties可见性hide- 隐藏 API不在公开 SDK 中底层实现通过native方法调用__system_property_get/__system_property_set数据存储存储在/system/build.prop、/vendor/build.prop、/default.prop等文件中2. 权限说明2.1 属性前缀与权限前缀读取权限写入权限说明debug.✅ 所有应用✅ shell/adb调试专用无需特殊权限log.✅ 所有应用✅ shell/adb日志相关ro.✅ 所有应用❌ Read-Only只读属性重启后保持persist.✅ 所有应用❌ 需要 root持久化属性重启后保持ctl.❌ 需要特权❌ 需要特权控制系统服务hw.✅ 所有应用❌ 需要 root硬件相关属性其他自定义❌ 需要权限❌ 需要 root应用自定义属性2.2 Android 版本限制Android 版本限制程度Android 8.0 以下可自由使用反射Android 8.0 - 8.1灰名单可用但警告Android 9.0黑名单可能被阻止3. API 方法3.1 核心方法通过反射调用// 获取属性值publicstaticStringget(Stringkey)// 获取属性值带默认值publicstaticStringget(Stringkey,StringdefaultValue)// 设置属性值需要权限publicstaticvoidset(Stringkey,Stringvalue)// 添加变更监听publicstaticvoidaddChangeCallback(Runnablecallback)3.2 反射工具类importjava.lang.reflect.Method;publicclassSystemPropertiesProxy{/** * 获取字符串属性 */publicstaticStringget(Stringkey){returnget(key,null);}/** * 获取字符串属性带默认值 */publicstaticStringget(Stringkey,StringdefaultValue){try{Class?cClass.forName(android.os.SystemProperties);Methodmethodc.getMethod(get,String.class,String.class);return(String)method.invoke(c.newInstance(),key,defaultValue);}catch(Exceptione){returndefaultValue;}}/** * 获取整数属性 */publicstaticintgetInt(Stringkey,intdefaultValue){try{Class?cClass.forName(android.os.SystemProperties);Methodmethodc.getMethod(getInt,String.class,int.class);return(Integer)method.invoke(c.newInstance(),key,defaultValue);}catch(Exceptione){returndefaultValue;}}/** * 获取长整数属性 */publicstaticlonggetLong(Stringkey,longdefaultValue){try{Class?cClass.forName(android.os.SystemProperties);Methodmethodc.getMethod(getLong,String.class,long.class);return(Long)method.invoke(c.newInstance(),key,defaultValue);}catch(Exceptione){returndefaultValue;}}/** * 获取布尔属性 */publicstaticbooleangetBoolean(Stringkey,booleandefaultValue){try{Class?cClass.forName(android.os.SystemProperties);Methodmethodc.getMethod(getBoolean,String.class,boolean.class);return(Boolean)method.invoke(c.newInstance(),key,defaultValue);}catch(Exceptione){returndefaultValue;}}/** * 设置属性需要 root 或 shell 权限 */publicstaticvoidset(Stringkey,Stringvalue){try{Class?cClass.forName(android.os.SystemProperties);Methodmethodc.getMethod(set,String.class,String.class);method.invoke(c.newInstance(),key,value);}catch(Exceptione){e.printStackTrace();}}}4. 常用系统属性4.1 系统信息类# SDK 版本ro.build.version.sdk ro.build.version.release# 设备信息ro.product.model# 设备型号ro.product.brand# 品牌ro.product.manufacturer# 制造商ro.product.device# 设备代号ro.product.name# 产品名称# 硬件信息ro.hardware.platform# 平台ro.hardware.chipname# 芯片名称ro.board.platform# 主板平台# 构建信息ro.build.date# 构建日期ro.build.date.utc# 构建时间戳ro.build.display.id# 显示的构建IDro.build.fingerprint# 构建指纹ro.build.tags# 构建标签 (release-keys/test-keys)ro.build.type# 构建类型 (user/userdebug)4.2 调试相关类# 调试开关debug.sf.nobootanimation# 禁用开机动画debug.layout# 布局调试debug.enable_tracing# 跟踪启用# 自定义调试属性应用可读写debug.com.yourapp.feature_a debug.com.yourapp.config_b4.3 网络相关类# 网络状态net.hostname# 主机名net.dns1# DNS 服务器 1net.dns2# DNS 服务器 2net.gprs.httpProxy# HTTP 代理# WiFi 相关wifi.interface# WiFi 接口名称4.4 性能相关类# SurfaceFlingerdebug.sf.disable_backpressure# 禁用背压debug.sf.showupdates# 显示更新debug.sf.layer_debug_regions# 层调试区域# HWUI渲染debug.hwui.render_dirty_regions# 渲染脏区域debug.hwui.show_dirty_regions# 显示脏区域5. ADB 命令5.1 基本操作# 获取单个属性adb shell getprop ro.build.version.sdk# 获取所有属性adb shell getprop# 筛选特定属性adb shell getprop|grepdebug# 设置属性debug 前缀无需 rootadb shell setprop debug.com.xxx.xxxvalue# 设置其他属性需要 rootadb shellsu-csetprop persist.sys.locale zh-CN# 清除属性adb shell setprop debug.com.xxx.xxx5.2 批量操作# 保存所有属性到文件adb shell getpropprops.txt# 从文件设置属性catprops.txt|whilereadline;dokey$(echo$line|cut-d[-f2|cut-d]-f1|cut-d:-f1)value$(echo$line|cut-d[-f2|cut-d]-f2)adb shell setprop$key$valuedone5.3 监听属性变化# 监听属性变化adb shellwatch-n1getprop | grep debug# 或者使用 getprop 的监听模式部分设备支持adb shell getprop-d6. 实际应用场景6.1 动态开关功能// Java 代码publicclassFeatureManager{publicstaticbooleanisFeatureEnabled(StringfeatureName){Stringkeydebug.com.yourapp.feature_featureName;returnSystemPropertiesProxy.getBoolean(key,false);}publicstaticvoidenableFeature(StringfeatureName,booleanenabled){Stringkeydebug.com.yourapp.feature_featureName;SystemPropertiesProxy.set(key,String.valueOf(enabled));}}// 使用if(FeatureManager.isFeatureEnabled(new_ui)){// 启用新 UI}6.2 开发调试工具# 开启详细日志adb shell setprop log.tag.YourAppTag VERBOSE# 禁用某功能adb shell setprop debug.com.yourapp.disable_adstrue# 设置测试环境adb shell setprop debug.com.yourapp.envstaging6.3 性能测试// 禁用硬件加速进行测试adb shell setprop debug.hwui.profilefalse7. 注意事项⚠️ 重要提醒非 SDK APISystemProperties是隐藏 API使用反射可能在未来的 Android 版本中被完全阻止权限限制debug.前缀的属性可以自由读写其他属性需要 root 权限性能影响不要在频繁调用的代码中使用属性读取涉及跨进程调用IPC安全性敏感信息不要存储在属性中可通过adb shell getprop查看不要用于存储密码、令牌等调试完成后清理# 清理调试属性adb shell setprop debug.com.yourapp.xxx8. 替代方案需求推荐方案获取设备信息Build类公开 API应用内配置SharedPreferences跨进程配置ContentProvider / AIDL调试开关BuildConfig.DEBUG 自定义配置9. 完整示例代码importandroid.content.Context;importandroid.util.Log;importjava.lang.reflect.Method;publicclassSystemPropertiesHelper{privatestaticfinalStringTAGSystemPropertiesHelper;/** * 检查属性是否可用 */publicstaticbooleanisAvailable(){try{Class.forName(android.os.SystemProperties);returntrue;}catch(ClassNotFoundExceptione){returnfalse;}}/** * 获取调试开关用于功能开关 */publicstaticbooleanisDebugFeatureEnabled(Contextcontext,StringfeatureKey){// 优先从本地 SharedPreferences 读取android.content.SharedPreferencesprefscontext.getSharedPreferences(debug_config,Context.MODE_PRIVATE);if(prefs.contains(featureKey)){returnprefs.getBoolean(featureKey,false);}// 回退到 SystemPropertiesStringpropKeydebug.com.context.getPackageName().featureKey;Stringvalueget(propKey,false);returntrue.equalsIgnoreCase(value)||1.equals(value);}/** * 获取属性通用方法 */publicstaticStringget(Stringkey){returnget(key,null);}publicstaticStringget(Stringkey,StringdefaultValue){if(!isAvailable()){Log.w(TAG,SystemProperties not available);returndefaultValue;}try{Class?cClass.forName(android.os.SystemProperties);Methodmethodc.getMethod(get,String.class,String.class);return(String)method.invoke(null,key,defaultValue);}catch(Exceptione){Log.e(TAG,Failed to get property: key,e);returndefaultValue;}}/** * 获取整数属性 */publicstaticintgetInt(Stringkey,intdefaultValue){if(!isAvailable()){returndefaultValue;}try{Class?cClass.forName(android.os.SystemProperties);Methodmethodc.getMethod(getInt,String.class,int.class);return(Integer)method.invoke(null,key,defaultValue);}catch(Exceptione){Log.e(TAG,Failed to get int property: key,e);returndefaultValue;}}/** * 获取布尔属性 */publicstaticbooleangetBoolean(Stringkey,booleandefaultValue){if(!isAvailable()){returndefaultValue;}try{Class?cClass.forName(android.os.SystemProperties);Methodmethodc.getMethod(getBoolean,String.class,boolean.class);return(Boolean)method.invoke(null,key,defaultValue);}catch(Exceptione){// 回退方案解析字符串Stringvalueget(key);if(valuenull)returndefaultValue;returntrue.equalsIgnoreCase(value)||1.equals(value);}}}10. 快速参考# 获取 SDK 版本adb shell getprop ro.build.version.sdk# 设置调试开关adb shell setprop debug.com.yourapp.feature_xxxtrue# 查看所有 debug 属性adb shell getprop|grep^debug\.# 清空调试属性adb shell setprop debug.com.yourapp.feature_xxx11. 通过 ADB 设置属性并在 Java 中读取场景动态调试开关# 通过 ADB 设置属性无需 rootadb shell setprop debug.com.xxx.my_featureenabled// 在 Java 代码中读取privateStringgetDebugProperty(Stringkey){try{Class?systemPropertiesClass.forName(android.os.SystemProperties);MethodgetsystemProperties.getMethod(get,String.class);return(String)get.invoke(null,key);}catch(Exceptione){e.printStackTrace();returnnull;}}// 使用StringvaluegetDebugProperty(debug.com.xxx.my_feature);if(enabled.equals(value)){// 功能已启用}带默认值的版本privateStringgetDebugProperty(Stringkey,StringdefaultValue){try{Class?systemPropertiesClass.forName(android.os.SystemProperties);MethodgetsystemProperties.getMethod(get,String.class,String.class);return(String)get.invoke(null,key,defaultValue);}catch(Exceptione){returndefaultValue;}}// 使用StringvaluegetDebugProperty(debug.com.xxx.my_feature,default);注意事项要点说明debug.前缀必须使用普通应用只能读取debug.开头的属性权限不需要特殊权限Android 版本Android 4.2 支持应用重启属性修改后可能需要重启应用才能生效持久化debug 属性重启设备后会丢失总结debug.前缀的属性是 Android 提供的调试机制允许开发者在不需要 root 的情况下动态调整应用行为。非常适合用于开发调试、A/B 测试、功能开关等场景。