The Android SDK has an API for sending commands to the phone called Monkeyrunner. It appears to be a Python API. Is there anyway I can use it in a Java application?
4 Answers
Well I have been trying to do this, here is what I found (Thanks to google and some help from members on the internet)
Here is a little Java program that uses monkeyrunner to print the name of the device
import com.android.monkeyrunner.MonkeyDevice;
import com.android.monkeyrunner.adb.AdbBackend;
public class Monk {
public static void main(String[] args) {
// TODO code application logic here
Monk monk=new Monk();
monk.demo();
}
public void demo()
{
AdbBackend ab = new AdbBackend();
MonkeyDevice device = ab.waitForConnection();
//Print Device Name
System.out.println(device.getProperty("build.model"));
device.dispose();
}
}
For the above code too work, I needed to include the following jars monkeyrunner, ddmlib, jython, guavalib, sdklib.

- 2,262
- 3
- 22
- 31
-
1I'm getting an error with the device.getProperty. required: org.python.core.PyObject[],java.lang.String[] found: java.lang.String – Matt R. Johnson Aug 01 '11 at 20:15
-
1you must have used device.getProperty(PyObject[] pos,String strings) instead of device.getProperty(String string) is my best guess. Please post ur code so that I can have a look at it – Harkish Aug 02 '11 at 14:46
-
I am sorry, unable to understand why, the code above works for me – Harkish Aug 06 '11 at 00:53
-
@Harkish am also getting the same error. Can u please post jar version which u are using – Karan Nov 07 '14 at 14:03
-
@Harkish I know this question is old, but do you know which jar version you used? If I asked a similar question could you answer for version 26? – dustytrash Aug 02 '19 at 16:30
Here is an update to @Harkish's answer which works with what I assume to be the current version of MonkeyRunner:
import com.android.chimpchat.adb.AdbBackend;
import com.android.chimpchat.core.IChimpDevice;
public class MonkeyTest {
public static void main(String[] args) {
// sdk/platform-tools has to be in PATH env variable in order to find adb
IChimpDevice device = new AdbBackend().waitForConnection();
// Print Device Name
System.out.println(device.getProperty("build.model"));
// Take a snapshot and save to out.png
device.takeSnapshot().writeToFile("out.png", null);
device.dispose();
}
}
The library dependencies are:
chimpchat.jar, common.jar, ddmlib.jar, guava-13.0.1.jar, sdklib.jar
They can all be found in the sdk/tools/lib
subdirectory of the ADT bundle.

- 6,064
- 5
- 31
- 43
I'm jumping in to provide yet another updated answer.This is what a google dev advised as well.I think it is a more solid implementation and it uses more fail-safe methods.
import java.util.Map;
import java.util.TreeMap;
import com.android.chimpchat.ChimpChat;
import com.android.chimpchat.core.IChimpDevice;
public class MonkeyRunnerTest {
private static final String ADB = "/path-to-your-sdk/sdk/platform-tools/adb";
private static final long TIMEOUT = 5000;
/**
* @param args
*/
public static void main(String[] args) {
Map<String, String> options = new TreeMap<String, String>();
options.put("backend", "adb");
//this is so you don't need to add adb or platform-tools to your system path
options.put("adbLocation", ADB);
ChimpChat chimpchat = ChimpChat.getInstance(options);
//Using this method is advised as to avoid hangs,as this would wait indefinitely
//Actually waitForConnection() doesn't wait indefinitely but for Integer.MAX_VALUE milliseconds, which still makes up for 596 hours
IChimpDevice device = chimpchat.waitForConnection(TIMEOUT, ".*");
chimpchat.shutdown();
}
}
You can see all the devices properties with:
for (String prop: device.getPropertyList()) {
System.out.println(prop + ": " + device.getProperty(prop));
}
For information on the APIs you can look at the docs here: monkey runner api classes

- 1,966
- 22
- 37
-
Worked fairly well and quick. On Mac I had challenges to add the jars since I didn't want to copy them elsewhere. I simply created an alias to ~/Library/Android folder and used add external jars to my eclipse project. Trying to figure a better way to use through Maven now. Note - to fetch value of each property MonkeyRunner has to send one event per property to the device, in my case the emulator. But effective – Gautam Sep 20 '18 at 08:29
-
-
combining device.takeSnapshot().writeToFile("out.png", null); with this code throws an device unresponsive exception. I am guessing its a version mismatch of somekind since I am trying it out with bluestack emulator – Gautam Sep 20 '18 at 08:38
-
@Gautam Will you please tell me from where did you get the jar files and also if can name all the jar files too? – suv Jan 18 '19 at 07:07
Just to complement the great answer from ValarDohaeris, here it is the current dependencies in maven format:
<properties>
<com.android.tools.version>24.3.1</com.android.tools.version>
</properties>
<dependencies>
<dependency>
<groupId>net.sf.sociaal</groupId>
<artifactId>chimpchat</artifactId>
<version>22.6.3</version>
</dependency>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>18.0</version>
</dependency>
<dependency>
<groupId>com.android.tools</groupId>
<artifactId>sdklib</artifactId>
<version>${com.android.tools.version}</version>
</dependency>
<dependency>
<groupId>com.android.tools</groupId>
<artifactId>common</artifactId>
<version>${com.android.tools.version}</version>
</dependency>
<dependency>
<groupId>com.android.tools</groupId>
<artifactId>sdk-common</artifactId>
<version>${com.android.tools.version}</version>
</dependency>
<dependency>
<groupId>com.android.tools.ddms</groupId>
<artifactId>ddmlib</artifactId>
<version>${com.android.tools.version}</version>
</dependency>
</dependencies>

- 12,390
- 27
- 101
- 182
-
Currently, the most up-to-date lib appears to be https://mvnrepository.com/artifact/net.sf.jali/jali-adm/1.0.7 – Ajax Apr 18 '17 at 12:09