Home » How to stress test your Android app with Monkey

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

13 comments

  1. Hey Daniel,
    thanks for the article. If you want to try stress testing on multiple devices you can also use TestObject (testobject.com).
    (Disclaimer I’m founder of TestObject; but stress-testing is for free)
    Regards,
    Hannes

  2. Lalit Sharma says:

    Hi Daniel,
    Using Monkey to stress test android app is very much useful and easy to and help in reducing little efforts for testers .

    Thanks,
    Lalit Sharma

  3. Amit says:

    Hey daniel can you please mail me the steps from very starting as I am in learning phase and very much confused about the steps.

    • dnlkntt says:

      Hi Amit,
      if you have an app that you want to test you just need to execute the following command to use monkey :

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

      All you need to do is to get the package name of the starting activity and replace it in the mentioned command.

      Greets,
      Daniel

  4. Angel says:

    Hi Daniel~
    Do you have any way to solve the crash problem? I want to analyze lots of malwares automatically, but get crash after doing just one or two apk. I have tried in uninstall apk after finishing analyze, but it doesn’t work.
    Thanks for help

    • Daniel says:

      Hi Angel,

      can you please provide some more information about your setup. The information that the app is crashing is not enough. Have you checked the error logs of the crashing device and app? This is a good starting point to get the problem solved.

      Greets,
      Daniel

  5. Sophia says:

    Awesome article, I have been looking for various tools and never thought that its in built. I just launched my app in android after a lot of manual testing and wrote about it here – http://www.fromdev.com/2015/03/mobile-app-testing.html

    I wish I had seen your page before. I will be surely trying the monkey for my app and see if any crashes come to notices. So far my app has 40+ installs and no crashes. However I do not want to wait for production crashes. Its probably better to have monkey find it.

    Thanks for this tutorial, it helps.

Leave a Reply