Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I make a Java app using the Monkeyrunner API?

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?

like image 226
Matt R. Johnson Avatar asked Jul 13 '11 21:07

Matt R. Johnson


People also ask

How do I use MonkeyRunner?

Running monkeyrunner You do both by invoking the monkeyrunner command which is found in the tools/ subdirectory of your SDK directory. If you provide a filename as an argument, the monkeyrunner command runs the file's contents as a Python program; otherwise, it starts an interactive session.

What is monkey test in Android?

The Monkey is a program that runs on your emulator or device and generates pseudo-random streams of user events such as clicks, touches, or gestures, as well as a number of system-level events. You can use the Monkey to stress-test applications that you are developing, in a random yet repeatable manner.


2 Answers

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

like image 144
sokie Avatar answered Sep 30 '22 13:09

sokie


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.

like image 23
Harkish Avatar answered Sep 30 '22 12:09

Harkish