在不同平台上从Java代码运行adb shell命令 [英] Run adb shell commands from Java code on different platforms

查看:188
本文介绍了在不同平台上从Java代码运行adb shell命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am using Mac to run Java program which contains some commands to execute on a remote Android device. When I execute my program on a Windows machine it gives proper output but when I execute the same program on a Mac machine it doesn't. Here I am sharing the code snippet and the output I get.

代码:

 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上提供预期的输出。

When I run the same shell command from the command prompt it gives me the expected output on Mac as well.

推荐答案

最好使用 ProcessBuilder 。但是,如果您坚持使用 Runtime.getRuntime()。exec()-使用 .exec(String [] cmdarray)而不是当前的 .exec(String命令)

Better use ProcessBuilder instead. But if you insist on using Runtime.getRuntime().exec() - use .exec(String[] cmdarray) instead of your current .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);

这篇关于在不同平台上从Java代码运行adb shell命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆