简体   繁体   English

通过无线连接以编程方式使用adb安装.apk

[英]Programmatically installing .apk's using adb over a wireless connection

I am working on a project which performs tasks from a server to a client, the server being a desktop Java application and the client being an android application. 我正在执行一个项目,该项目执行从服务器到客户端的任务,该服务器是桌面Java应用程序,而客户端是android应用程序。 I am attempting to install applications to the android devices programmatically, using the desktop Java application. 我正在尝试使用桌面Java应用程序以编程方式将应用程序安装到android设备。 I am doing this by running adb commands (connect, install, etc.) through the Runtime.exec() methods, like so: 我这样做是通过Runtime.exec()方法运行adb命令(连接,安装等)来完成的,如下所示:

Runtime shell = Runtime.getRuntime();
Process p = shell.exec("cmd /c adb connect *ip address*:*designated port*");
p.waitFor();

I run the required shell commands on the android device at the start up of the client application, so that the device can be connected via adb over a wireless connection. 我在客户端应用程序启动时在android设备上运行所需的shell命令,以便可以通过无线连接通过adb连接该设备。

The problem I am having is that after a connection, the first install usually fails. 我遇到的问题是连接后,首次安装通常会失败。 I read the output of the process to see what it was giving for failing, and it gave me this: 我阅读了该过程的输出,以查看失败给出的结果,它给了我:

rm failed for /data/local/tmp/MyApp.apk, No such file or directory

This always happens to the first application install right after the connection (all subsequent applications install correctly). 这总是发生在连接后立即安装第一个应用程序(所有后续应用程序都正确安装)。 If I run the same exact install command from command prompt, it installs correctly. 如果我从命令提示符处运行相同的完全安装命令,它将正确安装。

What is strange is that I found when I was debugging after the connection and before the install, it would install correctly. 奇怪的是,在连接后和安装前进行调试时,我发现它可以正确安装。 I thought that it might be a timing problem, so i put a Thread.sleep(1000) in and It installed correctly after the sleep about half of the time. 我认为这可能是计时问题,因此我将Thread.sleep(1000)放入其中,并在睡眠后大约一半时间正确安装。 I thought this may be because waitFor() was not waiting long enough for the connection to finish, but I also read the output of the connection process (to determine its outcome) and its entire output is complete before the install takes place. 我认为这可能是因为waitFor()等待的时间不够长,无法完成连接,但是我还读取了连接过程的输出(以确定其结果),并且整个输出在安装之前已完成。

Does anyone have any suggestions to fix this problem? 有人对解决这个问题有任何建议吗? I feel that arbitrarily making the thread sleep for an amount of time shouldn't be necessary. 我觉得没有必要让线程任意休眠一段时间。

adb connect is an async process. adb connect是一个异步过程。 You may have some luck by waiting longer, but to do this correctly, you'll probably need to write some code instead of just using the command-line adb. 等待更长的时间可能会有些运气,但是要正确执行此操作,您可能需要编写一些代码,而不仅仅是使用命令行adb。

There's a library, "ddmlib", that lets you interact with adb. 有一个库“ ddmlib”,可让您与adb进行交互。

I have some Groovy code on Github that connects via ddmlib to do a screen capture. 在Github上有一些Groovy代码这些代码通过ddmlib连接进行屏幕捕获。 It implements AndroidDebugBridge.IDeviceChangeListener to be notified when the connection is complete. 它实现了AndroidDebugBridge.IDeviceChangeListener,以在连接完成时得到通知。

If Groovy isn't your thing, it shouldn't be too hard to convert it to Java. 如果Groovy不是您的事,那么将其转换为Java并不难。

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

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