Execute ADB command from Java program

前端 未结 4 1798
死守一世寂寞
死守一世寂寞 2020-12-09 06:38

The program I\'m working on uses ADB (Android Debug Bridge) to send files to my phone:

for (String s : files)
    String cmd = \"adb -s 0123456789ABCDEF push         


        
相关标签:
4条回答
  • 2020-12-09 06:54
     public static void adbpush() {
            System.out.println("adb push....");
            String[] aCommand = new String[] { adbPath, "push", inputFile(String),OutputDirectory };
            try {
                // Process process = new ProcessBuilder(aCommand).start();
                Process process = Runtime.getRuntime().exec(aCommand);
                process.waitFor(3, TimeUnit.SECONDS);
                System.out.println("file pushed");
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    
    0 讨论(0)
  • It will be better to give full path for ADB execution: like this $ANDROID_HOME/platform-tools/adb devices

    This is the full code you can use:

    String cmd = "$ANDROID_HOME/platform-tools/adb devices";
    ProcessBuilder processBuilder = new ProcessBuilder();
    if (Config.osName.contains("Windows"))
        processBuilder.command("cmd.exe", "/c", cmd);
    else
        processBuilder.command("bash", "-c", cmd);
    
    Process process = processBuilder.start();
    
    0 讨论(0)
  • 2020-12-09 07:17

    I finally got it working:

    ProcessBuilder pb = new ProcessBuilder("adb", "-s", "0123456789ABCDEF", "push", inputfile, outputfile);
    Process pc = pb.start();
    pc.waitFor();
    System.out.println("Done");
    

    I don't know what problems ProcessBuilder has with spaces in a string, but finally, it's working...

    0 讨论(0)
  • 2020-12-09 07:18

    I've solved in this way:

    public class Utils {
        private static final String[] WIN_RUNTIME = { "cmd.exe", "/C" };
        private static final String[] OS_LINUX_RUNTIME = { "/bin/bash", "-l", "-c" };
    
        private Utils() {
        }
    
        private static <T> T[] concat(T[] first, T[] second) {
            T[] result = Arrays.copyOf(first, first.length + second.length);
            System.arraycopy(second, 0, result, first.length, second.length);
            return result;
        }
    
        public static List<String> runProcess(boolean isWin, String... command) {
            System.out.print("command to run: ");
            for (String s : command) {
                System.out.print(s);
            }
            System.out.print("\n");
            String[] allCommand = null;
            try {
                if (isWin) {
                    allCommand = concat(WIN_RUNTIME, command);
                } else {
                    allCommand = concat(OS_LINUX_RUNTIME, command);
                }
                ProcessBuilder pb = new ProcessBuilder(allCommand);
                pb.redirectErrorStream(true);
                Process p = pb.start();
                p.waitFor();
                BufferedReader in = new BufferedReader(new InputStreamReader(p.getInputStream()));
                String _temp = null;
                List<String> line = new ArrayList<String>();
                while ((_temp = in.readLine()) != null) {
                    System.out.println("temp line: " + _temp);
                    line.add(_temp);
                }
                System.out.println("result after command: " + line);
                return line;
    
            } catch (Exception e) {
                e.printStackTrace();
                return null;
            }
        }
    }
    

    If you don't need env variables in your .bash_profile cut "-l" parameter.

    I have a Mac but it should work on Linux also.

    0 讨论(0)
提交回复
热议问题