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

前端 未结 7 1018
时光说笑
时光说笑 2020-12-02 23:25

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

相关标签:
7条回答
  • 2020-12-02 23:52

    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) and adb shell start (wait until it has restarted), then try again. Or simply reboot the device or emulator.

    0 讨论(0)
  • 2020-12-02 23:56

    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?

    0 讨论(0)
  • 2020-12-02 23:59

    here's a slightly saner version based on accuya's answer:

    public static String readSystemProperty(String name) {
        InputStreamReader in = null;
        BufferedReader reader = null;
        try {
            Process proc = Runtime.getRuntime().exec(new String[]{"/system/bin/getprop", name});
            in = new InputStreamReader(proc.getInputStream());
            reader = new BufferedReader(in);
            return reader.readLine();
        } catch (IOException e) {
            return null;
        } finally {
            closeQuietly(in);
            closeQuietly(reader);
        }
    }
    
    public static void closeQuietly(Closeable closeable) {
        if (closeable == null) return;
        try {
            closeable.close();
        } catch (IOException ignored) {
        }
    }
    
    0 讨论(0)
  • 2020-12-03 00:02

    import android.os.SystemProperties

    String s = SystemProterties.get("ro.xxx.xxx","default value if property not set");

    0 讨论(0)
  • 2020-12-03 00:03

    Because there are two types of property in Android.

    1. System level - we can get/set with command adb shell getprop/setprop.
    2. In current process level - we can get/set with regular java 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.

    0 讨论(0)
  • 2020-12-03 00:11

    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) {
        }
    }
    
    0 讨论(0)
提交回复
热议问题