How to define and use a system property in Android Instrumentation test?

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:

Class clazz = null;
clazz = Class.forName("android.os.SystemProperties");
Method method = clazz.getDeclaredMethod("get", String.class);
String prop = (String)method.invoke(null, "AP");
Log.e("so_test", "my prop is: <" + prop  + ">");

Two. use ‘getprop’ utility:

Process proc = Runtime.getRuntime().exec(new String[]{"/system/bin/getprop", "AP"});
BufferedReader reader = new BufferedReader(new InputStreamReader(proc.getInputStream()));
Log.e("so_test", "my prop is: " + reader.readLine());

Maybe using functions availble in NDK is an option too, but why bother?

Leave a Comment