2012年11月19日 星期一

Extending monkeyrunner with Plugins - Jar File

PrefaceAs you can see the description on Android Developer Site, you can extend MonkeyRunner by writing java programming language and integrate to an Jar file and create more capacities for your control purpose. In following section, using 21 steps to guide you how to create a monkeyrunner extension JAR file.How to create a Jar component for MonkeyRunner?Before we start to know how to create a Jar Component for MonkeyRunner, there're something you need to do in advance. The J2SDK installation on your computer is required and you need to know how to program Java based program. Eclipse is an option, however it's very useful. Android SDK installation is required. Here I'll show you an example by using Eclipse to create a Jar component for MonkeyRunner Extension.1. Using Eclipse to create a Java Project through File->New->Java Project. Fill the project name.2. Press Next button.

10. If you need to initial some variable for monkeyrunner runtime environment, you can implement a interface named Predicate and write the initial thing in apply method.11. In this example, I create three static methods for interacting with Android device. They're tap, PressButton, and saveScreenToFile respectively. These functions are to trigger a touch event, and a button event, and to save run-time screen to a file. In order to implement these function to communicate with Android device, we need to import MonkyDevice and MonkeyImage classes. The usage is similar with what you wrote on monkeyrunner console to control Android devices. The difference is that you need to use Python Object class for given parameters. For instance, to invoke a touch event, you can use touch method in MonkeyDevice class. But you need to give a value which is belong to the type of PyObject. Hence, we declare a PyObject variable and assign two integer and one string as their values that is needed by declared PyObject variable. For given parameters, the two integer values are used to assign where to touch and the string value is used to assign which action we're going to use for touch event (E.g. DOWN, UP, or DOWN_AND_UP). Note that, you can not use int as integer's type in PyObject's initialization. To assign an integer value to PyObject class, the type of PyInteger Object is needed. To assign a string value to PyObject class, the type of PyString Object is needed.

12. The Eclipse is very useful for developing MonkeyRunner Extension Jar file, because you can see the tip when your mouse move cursor to the method. Hence, you can know what type and you should use and how many parameters you should provide for particular method.

13. Start to export your wrote files to a Jar file for MonkeyRunner after you finish the coding work.14. Move mouse to project name on Eclipse and right click the mouse.15. Click Export option.

16. Select JAR file and press Next button.

17. Select the files which are going to be exported and press Next button.

18. Press Next Button.

19. If you don't have a MANIFEST file existed already, you can select option of "Generate the manifest file" and press Browser button to select where to store the MANIFEST file. You can give a name like MANIFEST.MF and place in root folder of project. If you have a MANIFEST file already, you can select option - Use existing manifest from workspace and select your own file.20. Press Finish button.

21. Edit the MANIFEST file you used at last step and add a command - MonkeyRunnerStartupRunner: com.android.monkeyrunner.monkeyrunnerExtto MANIFEST file. The string of MonkeyRunnerStartupRunner: is followed by full package name of your class. Finally, save your modified MANIFEST file to your JAR file.

How to import your own Jar file for MonkeyRunner?Copy your Jar file to /android-sdk_folder/tools/lib before running monkeyrunner.bat. Double click monkeyrunner.bat at the folder of android-sdk/tools/ to bring up a MonkeyRunner console as follows. And import the necessary class and your own class as follows. Then you can use your own class to do something on MonkeyRunner run-time environment.

11 則留言:

Sir, I am having problems with the last part of your (well described) process, the problem is that monkeyrunner refuses to import monkeyrunnerExt saying this package is not signed. tried repeating this process building it as Android app, but i couldn't find the way to create a Jar file and signing it.Hope u can help.Best RegardsRaul Jacinto

Greeting, do you follow all the steps aforementioned and have the same class name - monkeyrunnerExt with package name com.android.monkeyrunner? To sign this class is unnecessary.It's a Java application rather than a Android app. You should create a Jar file and don't need to sign it. The monkeyrunner plugin would be run with Monkeyrunner environment on a PC or NB. Is it helpful. Any question you can post here again. Good Luck.

關於我自己

Smart Embedded/Mobile Device Verification Design for over 10 years.
Web/Database System Integrated design for over 2 years.
Master degree in computer science,
AS degree in Electronic Engineering,
PMP certified.