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

Viewed 64071

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.

7 Answers

Based on provided answer, Slightly modified version of SetProperty

    public void setSystemProperty(String Key, String value){
    InputStreamReader in = null;
    BufferedReader reader = null;
    try {
        Process proc = Runtime.getRuntime().exec("/system/bin/setprop "+Key+" "+value);
        in = new InputStreamReader(proc.getInputStream());
        reader = new BufferedReader(in);

        String line = null;
        Log.d("Saurabh Shell" ,"<OUTPUT>");
        while ( (line = reader.readLine()) != null)
            Log.d("Shell" , line);
        Log.d("Saurabh Shell", "</OUTPUT>");
        int exitVal = proc.waitFor();
        Log.d("Saurabh Shell","Process exitValue: " + exitVal);

    } catch (IOException e) {
       e.printStackTrace();
    } catch (InterruptedException e) {
        e.printStackTrace();
    } finally {
        closeQuietly(in);
        closeQuietly(reader);
    }
}

close Input and reader

    public  void closeQuietly(Closeable closeable) {
    if (closeable == null) return;
    try {
        closeable.close();
    } catch (IOException ignored) {
    }
}

You need a root to set system property

adb shell
su
setprop AP 123
Related