Skip to content

Instantly share code, notes, and snippets.

@lgawin
Created February 12, 2025 07:10
Show Gist options
  • Save lgawin/56dd37fee2ca018e27b9e033a11e900e to your computer and use it in GitHub Desktop.
Save lgawin/56dd37fee2ca018e27b9e033a11e900e to your computer and use it in GitHub Desktop.
Util function (in Kotlin) to read android property (same as `adb shell getprop xxx`)
@SuppressLint("PrivateApi")
fun getSystemProperty(propertyName: String) : String? = runCatching {
val clazz = Class.forName("android.os.SystemProperties")
val method = clazz.getMethod("get", String::class.java, String::class.java)
val value = method.invoke(clazz, propertyName, null) as String?
value
}.onFailure { Log.e("getSystemProperty", "failed to get: $propertyName", it) }
.getOrNull()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment