【发布时间】:2014-11-29 15:49:42
【问题描述】:
我正在为需要进入超级用户模式的根设备编写应用程序。我知道如何以适用于实际 root 设备的方式执行此操作,但是当我在 AVD 中测试它时,“su”命令的行为与它在真实设备上的行为不同。它不允许非 root 用户“su”到 root ... AVD 只允许 root 到 su 到非 root 用户。
有谁知道如何让以下代码片段在 Android 模拟器中工作?
// ... etc. ...
Process process = null;
try {
// This works on a rooted device and fails within
// the rooted AVD ...
process = new ProcessBuilder("/system/xbin/su").start();
}
catch (Throwable t) {
t.printStackTrace();
return;
}
DataOutputStream writer = new DataOutputStream(process.getOutputStream());
InputStreamReader stream = new InputStreamReader(process.getInputStream());
BufferedReader reader = new BufferedReader(stream);
try {
writer.writeBytes("exec /system/bin/mount -o remount,rw /system\n");
// Just in case the "exec" fails, above ...
writer.writeBytes("exit 1\n");
writer.close();
}
catch (Throwable t) {
t.printStackTrace();
return;
}
finally {
try {
reader.close();
}
catch (Throwable t) {
// do nothing
}
try {
stream.close();
}
catch (Throwable t) {
// do nothing
}
}
// ... etc. ...
这会失败,因为应用程序没有以 root 身份运行,但在 AVD 中,/system/xbin/su 命令仅在由 root 用户调用时才有效。下面的 adb shell 对话框说明了这一点(我正在连接到正在运行的 AVD)...
% adb shell
root@generic:/ # ### we are now running as root
root@generic:/ # /system/xbin/su u0_a70
root@generic:/ $ ### this worked because "su" is invoked as root
root@generic:/ $ ### we are now running as user u0_a70
root@generic:/ $ /system/xbin/su root
su: uid 10070 not allowed to su
1|root@generic:/ $ ### this didn't work as a non-root user
有谁知道如何让“su”命令在 root AVD 中工作,就像“su”在 root 设备中工作一样?
提前致谢。
PS:这是针对 Android 4.4.x 的
【问题讨论】: