[在Android上使用Runtime.getRuntime运行Shell命令

问题描述 投票:1回答:2

我正在使用设备管理应用程序(已由制造商签名)。我正在使用以下用于Android 9的adb shell命令安装其他应用程序:-

cat /sdcard/Download/myfolder/newapp.apk | pm install -S 1528293

而且我只是这样传递:-

String command = "cat /sdcard/Download/myfolder/newapp.apk | pm install -S 1528293"
Runtime.getRuntime().exec(command);

但是我收到错误“猫未知选项S”。

当我从adb shell运行它时,同一命令可以很好地工作。不知道我在做什么错,可以寻求帮助。

编辑1:-我尝试运行如下命令:-

String[] commandInstall = {
                "/system/bin/sh",
                "-c",
                "cat /sdcard/Download/myfolder/newapp.apk | pm install -S 1528293"
        };
Process process = Runtime.getRuntime().exec(commandInstall);

但是现在我得到了错误:-

ava.lang.SecurityException: Reverse mode only supported from shell
    at com.android.server.pm.PackageInstallerSession.doWriteInternal(PackageInstallerSession.java:679)
    at com.android.server.pm.PackageInstallerSession.write(PackageInstallerSession.java:612)
    at android.content.pm.PackageInstaller$Session.write(PackageInstaller.java:852)
    at com.android.server.pm.PackageManagerShellCommand.doWriteSplit(PackageManagerShellCommand.java:2447)
    at com.android.server.pm.PackageManagerShellCommand.runInstall(PackageManagerShellCommand.java:915)
    at com.android.server.pm.PackageManagerShellCommand.onCommand(PackageManagerShellCommand.java:158)
    at android.os.ShellCommand.exec(ShellCommand.java:103)
    at com.android.server.pm.PackageManagerService.onShellCommand(PackageManagerService.java:21330)
    at android.os.Binder.shellCommand(Binder.java:634)
    at android.os.Binder.onTransact(Binder.java:532)
    at android.content.pm.IPackageManager$Stub.onTransact(IPackageManager.java:2821)
    at com.android.server.pm.PackageManagerService.onTransact(PackageManagerService.java:3856)
    at android.os.Binder.execTransact(Binder.java:731)

编辑2:-在android 9之前,我只能执行以下安装应用的步骤:-

Runtime.getRuntime().exec("pm install -r app.apk");
java android shell adb
2个回答
0
投票

来自PackageInstallerSession.java:

switch (Binder.getCallingUid()) {
                    case android.os.Process.SHELL_UID:
                    case android.os.Process.ROOT_UID:
                    case android.os.Process.SYSTEM_UID:
                        break;
                    default:
                        throw new SecurityException(
                                "Reverse mode only supported from shell or system");
                }

如果您的应用程序位于系统/应用程序下,则可能必须将其放入系统/专用应用程序中。希望异常的起源可以以某种方式帮助您。


0
投票

查看PackageInstallerSession的源代码,我发现它已更改为:-

switch (Binder.getCallingUid()) {
    case android.os.Process.SHELL_UID:
    case android.os.Process.ROOT_UID:
        break;
    default:
        throw new SecurityException("Reverse mode only supported from shell");
    }

Source因此,即使它是系统应用程序,安装的shell命令也可能不起作用。从提交消息看来,这样做是为了让PackageInstaller进行此工作。

但是似乎在某些时候再次更改为,但可能未包含在android 9中:-

    switch (Binder.getCallingUid()) {
                case android.os.Process.SHELL_UID:
                case android.os.Process.ROOT_UID:
                case android.os.Process.SYSTEM_UID:
                    break;
                default:
                    throw new SecurityException(
                            "Reverse mode only supported from shell or system");
            }

Source

因此,如果该应用程序是系统应用程序,则最好的方法是使用PackageInstaller。

© www.soinside.com 2019 - 2024. All rights reserved.