//! A thin rust wrapper for Android system properties. //! //! This crate is similar to the `android-properties` crate with the exception that //! the necessary Android libc symbols are loaded dynamically instead of linked //! statically. In practice this means that the same binary will work with old and //! new versions of Android, even though the API for reading system properties changed //! around Android L. //! //! ## Example //! //! ```rust //! use android_system_properties::AndroidSystemProperties; //! //! let properties = AndroidSystemProperties::new(); //! //! if let Some(value) = properties.get("persist.sys.timezone") { //! println!("{}", value); //! } //! ``` //! //! ## Listing and setting properties //! //! For the sake of simplicity this crate currently only contains what's needed by wgpu. //! The implementations for listing and setting properties can be added back if anyone needs //! them (let me know by filing an issue). //! //! ## License //! //! Licensed under either of //! //! * Apache License, Version 2.0 ([LICENSE-APACHE] or <http://www.apache.org/licenses/LICENSE-2.0>) //! * MIT license ([LICENSE-MIT] or <http://opensource.org/licenses/MIT>) //! //! at your option. //! //! [LICENSE-APACHE]: https://github.com/nical/android_system_properties/blob/804681c5c1c93d4fab29c1a2f47b7d808dc70fd3/LICENSE-APACHE //! [LICENSE-MIT]: https://github.com/nical/android_system_properties/blob/804681c5c1c93d4fab29c1a2f47b7d808dc70fd3/LICENSE-MIT
use std::{
ffi::{CStr, CString},
os::raw::{c_char, c_int, c_void},
};
// Fallback for old versions of Android. if properties.read_callback_fn.is_none() || properties.find_fn.is_none() {
properties.get_fn = load_fn(libc_so, b"__system_property_get\0")
.map(|raw| mem::transmute::<*const c_void, SystemPropertyGetFn>(raw));
}
}
properties
}
/// Retrieve a system property. /// /// Returns None if the operation fails. /// /// # Example /// /// ``` /// # use android_system_properties::AndroidSystemProperties; /// let properties = AndroidSystemProperties::new(); /// /// if let Some(value) = properties.get("persist.sys.timezone") { /// println!("{}", value); /// } /// ``` pubfn get(&self, name: &str) -> Option<String> { let cname = CString::new(name).ok()?; self.get_from_cstr(&cname)
}
/// Retrieve a system property using a [`CStr`] key. /// /// Returns None if the operation fails. /// /// # Example /// /// ``` /// # use android_system_properties::AndroidSystemProperties; /// # use std::ffi::CStr; /// let properties = AndroidSystemProperties::new(); /// /// let key = unsafe { CStr::from_bytes_with_nul_unchecked(b"persist.sys.timezone\0") }; /// if let Some(value) = properties.get_from_cstr(key) { /// println!("{}", value); /// } /// ``` pubfn get_from_cstr(&self, cname: &std::ffi::CStr) -> Option<String> { // If available, use the recommended approach to accessing properties (Android L and onward). iflet (Some(find_fn), Some(read_callback_fn)) = (self.find_fn, self.read_callback_fn) { let info = unsafe { (find_fn)(cname.as_ptr()) };
// Fall back to the older approach. iflet Some(get_fn) = self.get_fn { // The constant is PROP_VALUE_MAX in Android's libc/include/sys/system_properties.h const PROPERTY_VALUE_MAX: usize = 92; letmut buffer: Vec<u8> = Vec::with_capacity(PROPERTY_VALUE_MAX); let raw = buffer.as_mut_ptr() as *mut c_char;
let len = unsafe { (get_fn)(cname.as_ptr(), raw) };
if len > 0 {
assert!(len as usize <= buffer.capacity()); unsafe { buffer.set_len(len as usize); }
String::from_utf8(buffer).ok()
} else {
None
}
} else {
None
}
}
}
impl Drop for AndroidSystemProperties { fn drop(&mutself) { if !self.libc_so.is_null() { unsafe {
libc::dlclose(self.libc_so);
}
}
}
}
Messung V0.5 in Prozent
¤ Dauer der Verarbeitung: 0.12 Sekunden
(vorverarbeitet am 2026-06-27)
¤
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.