Android Automation Magic

Posted on Sat 07 September 2019 in android

Use automation scripts to cater to daily routine tasks on Android.

Launch Application

There are 2 ways to go. [1]

  • Without specifying an activity:

    monkey -p 1

    Note that the pseudorandom operation usually turns on auto-rotation, from my experience.

  • With a certain activity:

    am start [-S] (-S: force new activity)

    For the activity name, either use

    cmd package resolve-activity --brief to obtain the "default" activity, or

    dumpsys window windows | grep Focus to obtain the activity on current screen.

    btw, kill by am force-stop


Use the input applet to

  • tap: input tap <x> <y>

  • swipe: input swipe <x1> <y1> <x2> <y2> [duration]

    • canonically, from (x1, y1) to (x2, y2), with an optional swipe duration.
    • can be used to simulate a long press by setting (x1, y1) == (x2, y2) & duration = press time.
  • input text: input text "the text to go" (be careful about the active input method)

  • back/home/recent/etc. keys: input keyevent <keycode> (check keycodes here: 26=power, KEYCODE_APP_SWITCH=recent, 3=home, 4=back)

To show (x, y) coordiante, just turn on pointer location in phone's developer options, or by adb shell settings put system pointer_location 1 (0 to disable).


All the above commands can be issued with adb shell wrapper.

Screenshot by capturing bytes output of adb exec-out screencap -p

Stay awake by adb shell svc power stayon usb

Get lock state by "mAwake" and "Lockscreen" of adb shell dumpsys window

Use sleep to pause between operations.

Integrate scripts with automation apps like Easer, or Auto.js

See also shizuku