简体   繁体   English

在Java中以编程方式执行adb install命令

[英]Programmatically executing adb install command in java

I am trying to install an android app into the device from a Java application. 我正在尝试从Java应用程序将android应用安装到设备中。

Using the following command - Runtime.getRuntime().exec("adb install /apps/testapp.apk"); 使用以下命令-Runtime.getRuntime()。exec(“ adb install /apps/testapp.apk”);

I face the following error - Cannot run program "adb": error=2, No such file or directory 我遇到以下错误-无法运行程序“ adb”:error = 2,没有这样的文件或目录

Should I have to use ProcessBuilder to start the command execution? 我是否必须使用ProcessBuilder来开始命令执行?

I think problem may be 我认为问题可能是

  1. adb environment variable is not set. 未设置adb环境变量。 you can try with full path 你可以尝试全路径
  2. path of the apk should be relative to java project CLASSPATH or the full path apk的路径应相对于Java项目CLASSPATH或完整路径

Use absolute file paths, use -r option to reinstall app if already installed: 使用绝对文件路径,使用-r选项重新安装应用程序(如果已安装):
Runtime.getRuntime().exec("adb install -r _HERE_AbsoluteFilePath_ ");
If you will be waiting on an execution: 如果您要等待执行:

String[] commands = new String[3];
commands[0] = "adb";
commands[1] = "install";
commands[2] = "-r";//reinstall if already installed
commands[3] = ___HERE_AbsoluteFilePath___;
Process p1 = Runtime.getRuntime().exec(commands, null);
p1.waitFor();

PS: if unable to run adb in console window or terminal - use absolute file path for adb or include path to environment variable. PS:如果无法在控制台窗口或终端中运行adb,请使用adb的绝对文件路径或包含环境变量的路径。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM