As 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.
4. Tap "Add External JARs..." button.
5. Add Jar files - Pick chimpchat.jar, guavalib.jar, jython.jar, and monkeyrunner.jar in the folder - /android_sdk_folder/tools/lib/.
6. Press Finish button.
7. Fill the package name - com.android.monkeyrunner.
8. Fill the class name.
9. Press Finish button.
10. If you need to initial some variable for monkeyrunner runtime environment, you can implement a interface named Predicate
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.monkeyrunnerExt to 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.