🔧 问题描述

在通过 adb shell 执行 shell 脚本时,遇到了权限拒绝的问题。即使分配了 root 权限,在 shell 外部执行脚本仍然会失败。

💡 解决方案

方法一:使用 -t 参数

通过搜索发现需要为 adb shell 分配伪终端,使用 -t 参数:

1
adb shell -t "/sdcard/<your_shell>.sh"

方法二:在构建脚本中使用

1
2
3
exec {
commandLine("adb", "shell", "-t", "/sdcard/<your_shell>.sh")
}
1
ProcessBuilder("powershell", "-c","\"adb shell -t \"sh /sdcard/1.sh\"\"")

⚠️ 常见错误

错误信息

1
Remote PTY will not be allocated because stdin is not a terminal.

✅ 最终解决方案

使用 -tt 参数
-t 后面再加一个 t,即使用 -tt 参数来强制分配伪终端。

1
adb shell -tt "/sdcard/<your_shell>.sh"