Skip to content

Android: launch_app fails for valid apps because monkey -c LAUNCHER returns a non-zero exit code #360

@TonyDowney

Description

@TonyDowney

Summary

On an Android emulator, mobile_launch_app fails for valid, installed apps with "Failed launching app with package name … please make sure it exists". The package exists and is launchable — the problem is that launchApp runs adb shell monkey -c LAUNCHER and treats monkey's non-zero exit code as a launch failure, but monkey returns non-zero here even when the package is fine.

Environment

mobile-mcp 0.0.59
Device Pixel 7 API 33 emulator, Android 13
adb 1.0.41
Host macOS 26.5.1 (arm64), Node v25.9.0

Reproduction

mobile_list_apps({device})            -> includes com.android.settings, com.android.chrome
mobile_launch_app({device, packageName: "com.android.settings"})
  -> "Failed launching app with package name \"com.android.settings\", please make sure it exists"
mobile_launch_app({device, packageName: "com.android.chrome"})
  -> same failure

Both are installed and launchable.

Root cause

lib/android.js launchApp():

try {
    this.silentAdb("shell", "monkey", "-p", packageName, "-c", "android.intent.category.LAUNCHER", "1");
} catch (error) {
    throw new ActionableError(`Failed launching app with package name "${packageName}", please make sure it exists`);
}

silentAdb uses execFileSync, which throws on any non-zero exit code. On this emulator image, monkey -c LAUNCHER exits non-zero even for valid packages:

$ adb shell monkey -p com.android.settings -c android.intent.category.LAUNCHER 1 ; echo "exit=$?"
...
** SYS_KEYS has no physical keys but with factor 2.0%.
exit=251

Meanwhile the package clearly resolves a LAUNCHER activity and am start launches it fine:

$ adb shell cmd package resolve-activity --brief com.android.settings
com.android.settings/.Settings

$ adb shell am start -n com.android.settings/.Settings ; echo "exit=$?"
Starting: Intent { cmp=com.android.settings/.Settings }
exit=0

So monkey's exit code is an unreliable success signal, and launchApp is gated on it.

Suggested fix

Any of:

  • Don't treat monkey's non-zero exit as fatal (it returns non-zero for benign reasons); check whether the app actually came to the foreground instead.
  • Resolve the launcher activity via adb shell cmd package resolve-activity --brief <pkg> and launch with adb shell am start -n <pkg>/<activity> (exit code is reliable).
  • Fall back to am start when monkey returns non-zero.

mobile_open_url already uses the more robust am start -a android.intent.action.VIEW and works reliably, which supports moving launchApp to am start.

Happy to test a patch — repro is consistent here.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions