如何在Android Instrumentation测试中定义和使用系统属性?

如何在Android Instrumentation测试中定义和使用系统属性?,android,adb,Android,Adb,我试图使用一些参数进行仪器测试。我注意到我可以使用system.getProperty()函数读取系统属性。所以我使用setprop命令来设置系统属性。例如:adb shell setprop AP 123。 在我的测试代码中,我尝试通过以下方式读取此AP属性: tmp = System.getProperty("AP"); Log.d("MyTest","AP Value = " + tmp); 然后,我使用logcat查看这个调试消息,但是我得到这个属性的空值。对可能出现的问题有什么看

我试图使用一些参数进行仪器测试。我注意到我可以使用
system.getProperty()
函数读取系统属性。所以我使用setprop命令来设置系统属性。例如:
adb shell setprop AP 123
。 在我的测试代码中,我尝试通过以下方式读取此AP属性:


tmp = System.getProperty("AP"); 
Log.d("MyTest","AP Value = " + tmp);
然后,我使用logcat查看这个调试消息,但是我得到这个属性的空值。对可能出现的问题有什么看法吗?
请注意,我仍然可以使用
adb shell getprop AP
命令读取系统属性。

在启动根VM(Zygote)时,系统属性只读取一次,这反过来又会生成与应用程序类似的其他Dalvik VM。这意味着您不能动态设置系统属性


尝试使用
adb shell stop
(等待它停止)和
adb shell start
(等待它重新启动)重新启动合子,然后重试。或者只需重新启动设备或模拟器。

因为Android中有两种类型的属性

  • 系统级-我们可以使用命令
    adb shell getprop/setprop
    获取/设置
  • 在当前进程级别-我们可以使用常规java
    System.getProperty()/setProperty()
    获取/设置

  • 当您设置系统级属性并尝试将其值作为当前进程级时,您将在日志中获取空值。

    要获取由“setprop”设置的属性,有两个选项:
    一个。使用android.os.SystemProperties,这是一个隐藏API。像这样使用它:

    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  + ">");
    

    也许使用NDK中可用的功能也是一种选择,但为什么要麻烦呢?

    根据accuya的回答,这里有一个稍微理智一点的版本:

    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) {
        }
    }
    

    导入android.os.SystemProperties


    字符串s=SystemProterties.get(“ro.xxx.xxx”,“未设置属性时的默认值”)

    根据提供的答案,稍微修改了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);
        }
    }
    

    您需要一个根来设置系统属性

    adb shell
    su
    setprop AP 123
    

    值得一提的是,您应该能够在emulator启动时设置属性,例如
    emulator-avd 2.3-prop AP=123
    。在执行测试时,始终尽最大努力保持环境完整。重新启动Zygote无法使android系统属性对system.getProperty()可用。我并不是在给出如何运行插装测试的建议,而是在回答他的问题,即如何在emulator上更改系统属性。我所描述的正是这样。假设您希望在测试之间更改这些属性,并且假设您无法在运行时应用新的系统属性(请参见Matthias的回答),那么最好将系统属性的设置/获取抽象为一个帮助器类,您可以在测试时提供一个模拟实现。这个线程更好:只为像我这样需要Set-method
    method-method=clazz.getDeclaredMethod(“Set”,String.class,String.class)的人完成这个任务是否需要添加特殊权限才能执行此操作@miroslavign@htellez不,您没有得到RuntimeException:
    java.lang.RuntimeException:未能在android.os.SystemProperties.native中设置系统属性。在android.os.SystemProperties.set(SystemProperties.java:130)中设置(本机方法)
    您在棉花糖中尝试过吗?
    android.os.SystemProperties
    不可导入。
        public  void closeQuietly(Closeable closeable) {
        if (closeable == null) return;
        try {
            closeable.close();
        } catch (IOException ignored) {
        }
    }
    
    adb shell
    su
    setprop AP 123