SoFunction
Updated on 2025-03-04

Android SDK command line tool Monkey parameters and usage analysis

What is Monkey?

Monkey is a command line tool provided by the Android SDK, which can easily and conveniently send pseudo-random user event streams and conduct pressure (stability) testing on Android APPs. It is mainly to test whether the app is unresponsive and crashed.

Monkey usage:

1. Enter the adb shell environment

In the windows environment, enter the DOS interface. In the interface, enter the adb shell to enter the adb shell environment

(Note: The 5037 port used by the adb shell service. If this port is occupied by other processes, the adb shell will not be able to start the adb shell normally. You need to close the process occupying the port and restart the adb shell)

2. Check the package name

adb shell pm list packages (-f: package names for all applications, -3: package names for non-system installations)

It is necessary to control the execution time of monkey, and you can calculate the number of times that need to be executed by setting the execution time/event sending delay.

For example: Want monkey to run for 24 hours and send an event every 100 milliseconds. 24*60*60*1000/100=864000 (times)

adb shell monkey -p *** -v -v -v --ignore-crashes --ignore-timeoutss --throttle 100 864000 >D:\

Said: This monkey will run for 24 hours

Monkey command parameter description:

Monkey's command parameters are divided into:

Basic parameters: -v, -s, -p, --throttle, etc.

Debug options: --ignore-crashes, etc.

Event type: --pct-touch, etc.

(I) Basic parameters

1. The simplest monkey command (monkey 100)

Indicates: On the device, 100 pseudo-random events are sent for the entire system.

2. -v parameters

Used to specify the feedback information level (the information level is the level of the log) is divided into 3 levels in total, and the default is -v (corresponding to: level0)

-v: Only provide a small amount of information such as startup prompts, test completion prompts and final results.

-v -v: More detailed logs, including each time information sent to the activity.

-v -v -v: The most detailed log, including the selected/unselected activity information in the test.

Note: When the monkey test is completed, there must be a: monkey finished logo in the end.

3. -p parameter

When we test, we test for specific apps, so when using monkey test, we need to provide the specific app package name to monkey, and the parameter -p is needed. Follow the app package name after -p.

During testing, you can not specify the package name. At this time, monkey will randomly start the app on the test device for operation; if you only need to test one app, just use one -p; if there are multiple apps to test at the same time, you can directly use -p package name 1 -p package name 2 to specify the specific app to be tested.

4. -s parameter

Seed value (seed), because monkey is sending a pseudo-random event stream, but if the two seed values ​​are the same, the sequence of events generated by the two monkey tests is the same. (So ​​generally, the seed value should be recorded during testing to prevent unresponsiveness and crash, which is not easy to verify.)

Note: -s needs to follow the package name before the number of times

5. --throttle parameter

Set the delay time (milliseconds) for performing an operation - refers to the interval between two events. If this parameter is not specified, the event will be generated and sent as quickly as possible.

(II) Debug options

1、--ignore-crashes

Used to specify whether Monkey stops running when the application crashes. If you use this parameter, even if the application crashes, monkey will still send the event until the event count is complete.

2、---ignore-timeouts

Used to specify whether Monkey stops running when an ANR (Application No Responding) error occurs in the application. If you use this parameter, Monkey will still send an event until the event count is completed even if an ANR error occurs in the application.

3、--ignore-security-exceptionss

Used to specify whether Monkey stops running when a license error occurs in the application (such as certificate license, network license, etc.). If you use this parameter, Monkey will send events even if a license error occurs in the application until the event count is complete.

4、--kill-process-after-error

Used to specify whether the application will stop running when an error occurs. If this parameter is specified, the application stops running and remains in its current state when an error occurs (note: the application is only stationary when an error occurs, and the system does not end the process of the application).

5、--monitor-native-crashes

Local code used to specify whether to monitor and report an application crash

6、--hprof

After setting this option, a report will be generated immediately before and after the monkey event sequence, with a size greater than 5MB, stored in /data/misc

(III) Event Type

Monkey has different types when sending pseudo-random events. The default random allocation ratio can also be specified. If not set, it will be --pct-anyevent to 100%, which is a pure random event; if other parameters are configured, but not 100%, the remaining percentage is also --pct-anyevent event.

The specific event types are:

1: Touch event --pct-touch

Adjust the percentage of touch touch events. Touch events refer to a single click/lift event in a single position on the screen.

2: Sliding screen event --pct-motion (gesture event)

(The gesture event consists of a press event somewhere on the screen, a series of pseudo-random movements, and a lift event) that is, a sliding operation, but it is straight and cannot turn)

3: Trackball --pct-trackball

(Trackball events include one or more random movements, sometimes accompanied by clicks. Trackballs are no longer available on smartphones, just like the direction keys of the handle)

4: Rotate (--pct-rotation)

Rotate the screen

5: Navigation--pct-nav

(Navigation events include up, down, left and right, such as input to the direction input device) The up, down, left and right keys of old phones are not available on the smart phone)

6: Main navigation --pct-majornav

Adjust the percentage of main navigation events (such as the actions of the graphical interface caused by the middle key, cancel, confirm, or menu)

7: System keys --pct-syskeys

Adjust system key events, such as: home/back/startcall/endcall and volume control keys, etc.

8: App switch--pct-appswitch

Adjust the percentage of the startup activity, and in a random interval, execute a startActivity() method call as a method that maximizes the coverage of all activities of the installation package.

9: Keyboard flip (--pct-flip)

10: Random --pct-anyevent

Adjust the percentage of other types of events, such as the percentage of key presses or other less commonly used events

The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.