如何使用Monkeyrunner API制作Java应用程序?

Android SDK有一个用于发送命令给Monkeyrunner的API。 它似乎是一个Python API。 无论如何,我可以在Java应用程序中使用它吗?

Solutions Collecting From Web of "如何使用Monkeyrunner API制作Java应用程序?"

那么我一直在试图做到这一点,这是我发现(感谢谷歌和互联网上的成员的一些帮助)

这是一个使用monkeyrunner打印设备名称的小程序

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(); } 

}

对于上面的代码工作,我需要包括以下jarr monkeyrunner,ddmlib,jython,guavalib,sdklib。

这里是@ Harkish的答案的更新,它与我认为是当前版本的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(); } } 

库依赖关系是:

 chimpchat.jar, common.jar, ddmlib.jar, guava-13.0.1.jar, sdklib.jar 

它们都可以在ADT包的sdk/tools/lib子目录中find。

我跳了进来提供另一个更新的答案。这是一个谷歌开发build议以及。我认为这是一个更坚实的实施,它使用更多的故障安全的方法。

 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(); } } 

你可以看到所有的设备属性:

 for (String prop: device.getPropertyList()) { System.out.println(prop + ": " + device.getProperty(prop)); } 

有关API的信息,您可以在这里查看文档: monkey runner API类

只是为了补充来自ValarDohaeris的伟大答案,这里是目前的maven格式的依赖关系:

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