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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3750109/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 11:39:08  来源:igfitidea点击:

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

androidadb

提问by Michalis

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 :

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


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 APcommand.

然后我使用 logcat 查看此调试消息,但我得到此属性的空值。关于可能出什么问题的任何想法?请注意,我仍然可以使用adb shell getprop AP命令读取系统属性。

回答by accuya

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:

要获取由 '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  + ">");

Two. use 'getprop' utility:

二。使用“getprop”实用程序:

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?

也许使用 NDK 中可用的函数也是一种选择,但何必呢?

回答by Matthias

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.

当根虚拟机 (Zygote) 启动时,系统属性会被读取一次,这反过来会产生其他 Dalvik 虚拟机,例如您的应用程序。这意味着您无法即时设置系统属性。

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.

尝试使用adb shell stop(等到它停止)和adb shell start(等到它重新启动)重新启动 Zygote ,然后再试一次。或者简单地重新启动设备或模拟器。

回答by Nimesh

Because there are two types of property in Android.

因为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().
  1. 系统级别 - 我们可以使用 command 获取/设置adb shell getprop/setprop
  2. 在当前进程级别 - 我们可以使用常规 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.

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

回答by Felipe Lima

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

这是基于 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) {
    }
}

回答by SAURABH_12

Based on provided answer, Slightly modified version of SetProperty

基于提供的答案,稍微修改过的 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) {
    }
}

回答by Amol K

import android.os.SystemProperties

导入 android.os.SystemProperties

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

String s = SystemProterties.get("ro.xxx.xxx","未设置属性时的默认值");