I am trying to use some arguments for an Instrumentation test. I noticed that I can read system properties with System.getProperty()
function. So I use setprop command to set a system property. For example: adb shell setprop AP 123
.
Inside my Test code I try to read this AP property with :
tmp = System.getProperty("AP");
Log.d("MyTest","AP Value = " + tmp);
Then I use logcat to view this debug message but I get a null value for this property. Any ideas on what could be wrong?
Note that I can still read the system property with adb shell getprop AP
command.
Based on provided answer, Slightly modified version of SetProperty
close Input and reader
System properties are read once when the root VM (Zygote) is started, which in turn spawns other Dalvik VMs like that of your application. That means you cannot set system properties on the fly.
Try restarting Zygote using
adb shell stop
(wait until it has stopped) andadb shell start
(wait until it has restarted), then try again. Or simply reboot the device or emulator.Because there are two types of property in Android.
adb shell getprop/setprop
.System.getProperty()/setProperty()
.As you are setting a system level property and trying to get its value as current process level, you are getting null value in log.
import android.os.SystemProperties
String s = SystemProterties.get("ro.xxx.xxx","default value if property not set");
here's a slightly saner version based on accuya's answer:
To get the property set by 'setprop', there are two options:
One. use android.os.SystemProperties, this is a hide API. use it like this:
Two. use 'getprop' utility:
Maybe using functions availble in NDK is an option too, but why bother?