我正在为需要进入超级用户模式的root设备编写应用程序。我知道如何以一种适用于实际root设备的方式执行此操作,但是当我在AVD(也是root用户)中测试它时,“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