Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Life is too short not to use ADB commands

Life is too short not to use ADB commands

Talk given at the following conferences/events:
- N26 Barcelona | Android Meetup (February / 2020)
- Codemotion Berlin (November / 2019)
- Droidcon Lisbon (September / 2019)
- droidcon Berlin (July / 2019)
- 360AnDev (July / 2018)

ADB is a versatile command-line tool that lets you communicate with a device. The ADB command facilitates a variety of device actions, such as installing and debugging apps, and it provides access to a Unix shell that you can use to run a variety of commands on a device. It is maybe one of the most used tools by the Android developers, but it is for sure the less known.

Do you want to learn the basic ADB commands or extends your knowledge with some advanced commands? Are you struggling with some bugs? Are you tired to perform some repetitive test? This talk is for you! You can make ADB work for you with automation and assist you to kill bugs. I spent weeks diving into the ADB commands for Sony Mobile and Motorola, and I learned helpful commands that will make your life easier.

It’s time for me to share this with you!

Zhenlei Ji

July 20, 2018
Tweet

More Decks by Zhenlei Ji

Other Decks in Programming

Transcript

  1. About this talk How does it work? Brushing up on

    some basic commands Some ADB commands you may have never heard about @zhenleiji
  2. How does it work? Android Debug Bridge (adb) ADB Daemon

    (adbd) Android ADB Server (adb) Client Client Host TCP 5555 to 5585 Usb or TCP 5037 TCP 5037 @zhenleiji
  3. Android Debug Bridge version 1.0.40 Version 4797878 Installed as /Users/zji/Library/Android/sdk/platform-tools/adb

    global options: -a listen on all network interfaces, not just localhost -d use USB device (error if multiple devices connected) -e use TCP/IP device (error if multiple TCP/IP devices available) -s SERIAL use device with given serial (overrides $ANDROID_SERIAL) -t ID use device with given transport id -H name of adb server host [default=localhost] -P port of adb server [default=5037] -L SOCKET listen on given socket for adb server [default=tcp:localhost:5037] ~$ adb Usages Android Debug Bridge @zhenleiji
  4. ~$ adb tcpip 5555 Wireless debugging ~$ adb connect <device-ip>

    ~$ adb usb Usb debugging Debug connections @zhenleiji
  5. ~$ adb devices -l Devices Show device detail list List

    of devices attached ZX1G4295NB device usb:336592896X product:shamu model:Nexus_6 device:shamu transport_id:2 emulator-5554 device product:sdk_gphone_x86 model:Android_SDK_built_for_x86 device:generic_x86 transport_id:4 @zhenleiji
  6. ~$ adb devices -l Devices Show device detail list List

    of devices attached ZX1G4295NB device usb:336592896X product:shamu model:Nexus_6 device:shamu transport_id:2 emulator-5554 device product:sdk_gphone_x86 model:Android_SDK_built_for_x86 device:generic_x86 transport_id:4 ~$ adb -s [serial_number] … Send commands to a specific device ZX1G4295NB, emulator-5554 @zhenleiji
  7. Transfer ~$ adb install <apk-path> Install/Uninstall an app ~$ adb

    uninstall <package> ~$ adb install -r <apk-path> Reinstall an app @zhenleiji
  8. Transfer ~$ adb install <apk-path> Install/Uninstall an app ~$ adb

    uninstall <package> ~$ adb push <local> <remote> Copy files to/from a device ~$ adb pull <remote> <local> ~$ adb install -r <apk-path> Reinstall an app @zhenleiji
  9. ~$ adb shell pm Package Manager Call package manager usage:

    pm path [--user USER_ID] PACKAGE pm dump PACKAGE pm install [-lrtsfd] [-i PACKAGE] [--user USER_ID] [PATH] pm install-create [-lrtsfdp] [-i PACKAGE] [-S BYTES] [--install-location 0/1/2] [--force-uuid internal|UUID] pm install-write [-S BYTES] SESSION_ID SPLIT_NAME [PATH] pm install-commit SESSION_ID pm install-abandon SESSION_ID pm uninstall [-k] [--user USER_ID] PACKAGE pm set-installer PACKAGE INSTALLER pm move-package PACKAGE [internal|UUID] . . . @zhenleiji
  10. ~$ adb shell pm list packages | grep youtube Package

    Manager Show package list @zhenleiji
  11. ~$ adb shell pm list packages | grep youtube Package

    Manager Show package list ~$ adb shell pm path [package] Print the path to the APK of the given package @zhenleiji
  12. Package Manager ~$ adb shell pm clear [package] Clear data

    ~$ adb shell pm grant[package][permission] Grant a permission to an app ~$ adb shell pm revoke[package][permission] Revoke a permission to an app @zhenleiji
  13. ~$ adb shell pm list packages | grep youtube Package

    Manager ~$ adb shell pm dump [package] Print current system state associated with the given PACKAGE Actions Schemes Broadcast Receivers ContentProviders Permissions Pending Intents Activity Back Stack Memory Usage @zhenleiji
  14. ~$ adb shell am Activity Manager Call activity manager usage:

    am [subcommand] [options] usage: am start [-D] [-N] [-W] [-P <FILE>] [--start-profiler <FILE>] [--sampling INTERVAL] [-R COUNT] [-S] [--track-allocation] [--user <USER_ID> | current] <INTENT> am startservice [--user <USER_ID> | current] <INTENT> am stopservice [--user <USER_ID> | current] <INTENT> am force-stop [--user <USER_ID> | all | current] <PACKAGE> am kill [--user <USER_ID> | all | current] <PACKAGE> am kill-all am broadcast [--user <USER_ID> | all | current] <INTENT> . . . @zhenleiji
  15. ~$ adb shell am start [options] [intent] Start an Activity

    specified by intent Intent @zhenleiji
  16. ~$ adb shell am start [options] [intent] Start an Activity

    specified by intent Intent -D: Enable debugging. -W: Wait for launch to complete. --start-profiler file: Start pro fi ler and send results to file. -P file: Like --start-profiler, but pro fi ling stops when the app goes idle. -R count: Repeat the activity launch count times. Prior to each repeat, the top activity will be fi nished. -S: Force stop the target app before starting the activity. --opengl-trace: Enable tracing of OpenGL functions. --user user_id | current: Specify which user to run as; if not speci fi ed, then run as the current user. @zhenleiji
  17. ~$ adb shell am start [options] [intent] Start an Activity

    specified by intent Intent -a action Specify the intent action, such as android.intent.action.VIEW. You can declare this only once. -d data_uri Specify the intent data URI, such as content://contacts/people/1. You can declare this only once. -t mime_type Specify the intent MIME type, such as image/png. You can declare this only once. -c category Specify an intent category, such as android.intent.category.APP_CONTACTS. -n component Specify the component name with package name pre fi x to create an explicit intent, such as com.example.app/.ExampleActivity. -f flags Add fl ags to the intent, as supported by setFlags(). @zhenleiji
  18. ~$ adb shell am start [options] [intent] Start an Activity

    specified by intent Intent @zhenleiji
  19. ~$ adb shell am start [options] [intent] Start an Activity

    specified by intent ~$ adb shell am startservice [options] [intent] Start a Service specified by intent Intent @zhenleiji
  20. ~$ adb shell am start [options] [intent] Start an Activity

    specified by intent ~$ adb shell am startservice [options] [intent] Start a Service specified by intent ~$ adb shell am broadcast [options] [intent] Send a Broadcast intent Intent @zhenleiji
  21. ~$ adb shell am force-stop [package] Force stop everything associated

    with package ~$ adb shell am kill [package] Kill all process associated with package Kill @zhenleiji
  22. ~$ adb shell am force-stop [package] Force stop everything associated

    with package ~$ adb shell am kill [package] Kill all process associated with package ~$ adb shell am kill-all Kill all background processes Kill @zhenleiji
  23. ~$ adb shell wm Window Manager Call window manager usage:

    wm [subcommand] [options] wm size [reset|WxH|WdpxHdp] wm density [reset|DENSITY] wm overscan [reset|LEFT,TOP,RIGHT,BOTTOM] wm scaling [off|auto] wm screen-capture [userId] [true|false] wm size: return or override display size. width and height in pixels unless suffixed with 'dp'. wm density: override display density. wm overscan: set overscan area for display. . . .
  24. Simulate other devices ~$ adb shell wm size [WxH|WdpxHdp|reset] Return

    or override display size ~$ adb shell wm density [DENSITY|reset] Return or override density size mdpi (~160) hdpi (~240) xhdpi (~320) xxhdpi (~480) xxxhdpi (~640) @zhenleiji
  25. System Services ~$ adb shell dumpsys -l List of system

    services ~$ adb shell dumpsys Provides information about the status of all system services
  26. System Services ~$ adb shell dumpsys [service] Provides information about

    the status of a specific system service alarm, activity, battery… ~$ adb shell dumpsys -l List of system services ~$ adb shell dumpsys Provides information about the status of all system services @zhenleiji
  27. ~$ adb shell dumpsys activity top Provides information about the

    top activities Activity name Activity state Language Screen size
  28. ~$ adb shell dumpsys activity top Provides information about the

    top activities Fragment name Fragment state
  29. System Properties ~$ adb shell getprop List of system properties

    ~$ adb shell getprop [system_property] Provides information about the specific system property @zhenleiji
  30. System Properties ~$ adb shell getprop List of system properties

    ~$ adb shell getprop [system_property] Provides information about the specific system property
  31. System Properties ~$ adb shell getprop List of system properties

    ~$ adb shell getprop [system_property] Provides information about the specific system property ~$ adb shell setprop [system_property] [value] Change a system property @zhenleiji
  32. ~$ adb shell input Simulate Input Call input Usage: input

    [<source>] <command> [<arg>...] . . . The commands and default sources are: text <string> (Default: touchscreen) keyevent [--longpress] <key code number or name> ... (Default: keyboard) tap <x> <y> (Default: touchscreen) swipe <x1> <y1> <x2> <y2> [duration(ms)] (Default: touchscreen) draganddrop <x1> <y1> <x2> <y2> [duration(ms)] (Default: touchscreen) press (Default: trackball) roll <dx> <dy> (Default: trackball) @zhenleiji
  33. 4 or KEYCODE_BACK 61or KEYCODE_TAB 66 or KEYCODE_ENTER 82 or

    KEYCODE_MENU https://developer.android.com/reference/android/view/KeyEvent Simulate Input ~$ adb shell input keyevent [keycode_number or name] Keyevent @zhenleiji
  34. Simulate Input ~$ adb shell input keyevent [keycode_number or name]

    Keyevent 4 or KEYCODE_BACK 61or KEYCODE_TAB 66 or KEYCODE_ENTER 82 or KEYCODE_MENU https://developer.android.com/reference/android/view/KeyEvent @zhenleiji ~$ adb shell input text [text] Text
  35. Simulate Input Tap ~$ adb shell input swipe [X1] [Y1]

    [X2] [Y2] Swipe @zhenleiji ~$ adb shell input tap [X] [Y]