在不同平台上从Java代码运行adb shell命令

时间:2017-01-20 13:31:26

标签: java android macos adb

我正在使用Mac来运行Java程序,其中包含一些要在远程Android设备上执行的命令。当我在Windows机器上执行我的程序时,它提供了正确的输出,但是当我在Mac机器上执行相同的程序时,它并没有。在这里,我将分享代码片段和我得到的输出。

代码:

 private static final String DUMPSYSCOMMAND = "adb -s ? shell \"dumpsys package com.PACKAGENAME.service | grep versionName\"";

  String versionString = runADBCommand(DUMPSYSCOMMAND.replace("?",
      deviceIP));
  System.out.println("Version String Result " + versionString);

  String versionName = null;
  if (versionString != null && !versionString.isEmpty()) {
      versionString = versionString.replace("\\s+", "");
      versionName = versionString.replace(".", "-")
          .substring(versionString.indexOf("=") + 1)
          .replaceAll("\\s+", "");
      System.out.println("Version String " + versionName);

  }

public String runADBCommand(String adbCommand) throws IOException {
        System.out.println("Running given command= " + adbCommand + "$$$");
        StringBuffer returnValue = new StringBuffer();
        String line;
        InputStream inStream = null;
        try {
            System.out.println("adbCommand = " + adbCommand);
            Process process = Runtime.getRuntime().exec(adbCommand);

            // process.waitFor();/
            inStream = process.getInputStream();
            BufferedReader brCleanUp = new BufferedReader(
                    new InputStreamReader(inStream));
            while ((line = brCleanUp.readLine()) != null) {
                if (!line.equals("")) {
                    System.out.println("After exec");
                    System.out.println("Line=" + line);

                }

                // returnValue = returnValue + line + "\n";
                returnValue.append(line).append("\n");
            }

            brCleanUp.close();
            try {


                process.waitFor();

            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("Error: " + e.getMessage());
        }
        System.out.println(returnValue.toString() + "@@");
        return returnValue.toString();
    }

输出:

@@Running given command= adb -s DEVICEIP shell "dumpsys package PACKAGENAME | grep versionName"$$$
adbCommand = adb -s DEVICEIP shell "dumpsys package PACKAGENAME | grep versionName"
After exec
Line=/system/bin/sh: dumpsys package PACKAGENAME | grep versionName: not found
/system/bin/sh: dumpsys package PACKAGENAME | grep versionName: not found
@@Version String Result /system/bin/sh: dumpsys package PACKAGENAME | grep versionName: not found
Version String /system/bin/sh:dumpsyspackagecom-PACKAGENAME|grepversionName:notfound

当我从命令提示符运行相同的shell命令时,它也会在Mac上为我提供预期的输出。

1 个答案:

答案 0 :(得分:1)

更好地使用ProcessBuilder。但如果您坚持使用Runtime.getRuntime().exec() - 请使用.exec(String[] cmdarray)代替您当前的.exec(String command)

private static final String DUMPSYSCOMMAND = "dumpsys package com.PACKAGENAME.service | grep versionName";

String versionString = runADBCommand({"adb", "-s", deviceIP, "shell", DUMPSYSCOMMAND});

...

public String runADBCommand(String[] adbCommand) throws IOException {

...

// do not forget to remove / modify this println - it expect a string
//        System.out.println("adbCommand = " + adbCommand);