startInstrumentation asks to run as user -2 but is calling from user 0; this requires android.permission.INTERACT_ACROSS_USERS_FULL

由于手头上一直没有android level 17及以上版本的手机,有一个shell命令启动脚本的BUG,发生在SDK level 17及以上

API>=17中加入了INTERACT_ACROSS_USERS_FULL,目的在于允许不同用户的应用之间可以产生交互,了安全,因此在交互时会校验userSerialNumber,,发现用户标识不匹配,导致权限校验失败,就会产生startInstrumentation asks to run as user -2 but is calling from user 0; this requires android.permission.INTERACT_ACROSS_USERS_FULL的报错,导致脚本无法调用

群里尝试,发现在17及以上版本,命令中需要加入--user 0参数

public static final String[] EXEC_DEVICE_COMMAND = {"/system/bin/am",
"instrument", "--user", "0", "-w", "-e", "class",
testcase,
"packagename/InstrumentationTestRunnername" };

Runtime runtime = Runtime.getRuntime();
Process proc = runtime.exec(cmd);

adb shell am instrument --user 0 -w packagename/InstrumentationTestRunnername

可以在调用时使用Build.VERSION.SDK_INT<17来对当前版本做判断选择合适的命令行启动方式

-------记录学习之用

原文地址:https://www.cnblogs.com/CharlesGrant/p/4145895.html