How to stress test your Android app with Monkey

Today I want to show you a small nice tool, to stress test your Android app. The tool is called monkey and is part of the Android SDK. The tool is not new, but I didn’t had the chance/ time to work with the tool.

Monkey is a program that runs on your device or emulator. While running it is generating pseudo-random user events such as touch, click, rotate, swipe, mute the phone, shutdown wifi and many more, to stress test your app and to see how your app is handling all those inputs.
To execute the monkey stress test you need the package name of your apk file you want to test. If you have access to the app code, you find the package name in the AndroidManifest.xml. If you have just the compiled apk file, you can use the tool Android Asset Packaging Tool (aapt) to get the package name from the file. aapt is located in the build-tools folder of the installed Android SDK version on your machine. For example, if you have the latest version of Android (4.4) installed, the path of aapt is:

Users/dknott/Documents/android/sdk/build-tools/android-4.4

If you start aapt with ./aapt you get an overview of possible parameters and options to use.aapt_help

To get the package name of the apk file, you need to execute the following command:

./aapt d badging /Users/dknott/Downloads/ExampleTestProject_v5.1/NotePad/bin/NotePad.apk | grep 'pack'


The output is:

package: name='com.example.android.notepad' versionCode='' versionName=''

With the package name, navigate to the platform-tools directory where adb is located and execute the command:

./adb shell monkey -p com.example.android.notepad -v 2000

The 2000 is the number of random commands that monkey will perform on the app.
See the following youtube video, where monkey is testing the Notepad app provided by the Robotium project.

At the end of the video you can see, that the Notepad app is crashing. The following stacktrace shows the error. It seems that the Notepad app has a problem in the onResume Method.

E/AndroidRuntime﹕ FATAL EXCEPTION: main
 java.lang.RuntimeException: Unable to resume activity {com.example.android.notepad/com.example.android.notepad.NoteEditor}: android.database.CursorIndexOutOfBoundsException: Index 0 requested, with a size of 0
 at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2919)
 at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2948)
 at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1358)
 at android.os.Handler.dispatchMessage(Handler.java:99)
 at android.os.Looper.loop(Looper.java:176)
 at android.app.ActivityThread.main(ActivityThread.java:5419)
 at java.lang.reflect.Method.invokeNative(Native Method)
 at java.lang.reflect.Method.invoke(Method.java:525)
 at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1046)
 at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:862)
 at dalvik.system.NativeStart.main(Native Method)
 Caused by: android.database.CursorIndexOutOfBoundsException: Index 0 requested, with a size of 0
 at android.database.AbstractCursor.checkPosition(AbstractCursor.java:424)
 at android.database.AbstractWindowedCursor.checkPosition(AbstractWindowedCursor.java:136)
 at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:50)
 at android.database.CursorWrapper.getString(CursorWrapper.java:114)
 at com.example.android.notepad.NoteEditor.onResume(NoteEditor.java:180)
 at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1209)
 at android.app.Activity.performResume(Activity.java:5450)
 at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2909)
 at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2948)
 at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1358)
 at android.os.Handler.dispatchMessage(Handler.java:99)
 at android.os.Looper.loop(Looper.java:176)
 at android.app.ActivityThread.main(ActivityThread.java:5419)
 at java.lang.reflect.Method.invokeNative(Native Method)
 at java.lang.reflect.Method.invoke(Method.java:525)
 at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1046)
 at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:862)
 at dalvik.system.NativeStart.main(Native Method)

If you are testing an Android app you should try monkey, it is really easy to use and the results are often really interesting :).

Have fun!

By Daniel Knott