Question

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. I am attempting to install applications to the android devices programmatically, using the desktop Java application. I am doing this by running adb commands (connect, install, etc.) through the Runtime.exec() methods, like so:

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.

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. 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.

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.

Was it helpful?

Solution

adb connect is an async process. 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.

There's a library, "ddmlib", that lets you interact with adb.

I have some Groovy code on Github that connects via ddmlib to do a screen capture. It implements AndroidDebugBridge.IDeviceChangeListener to be notified when the connection is complete.

If Groovy isn't your thing, it shouldn't be too hard to convert it to Java.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top