Codemotion Berlin 2019 - Life is too short not to use ADB commands

Ff2bfd42d37c150f01bfd5eab3e4c1fe?s=47 Zhenlei Ji
November 12, 2019

Codemotion Berlin 2019 - Life is too short not to use ADB commands

Talk given at Codemotion Berlin 2019 (https://events.codemotion.com/conferences/berlin/2019/).

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!

Ff2bfd42d37c150f01bfd5eab3e4c1fe?s=128

Zhenlei Ji

November 12, 2019
Tweet

Transcript

  1. ADB "Life is too short not to use ADB commands"

    Zhenlei Ji @zhenleiji 12-13 November, 2019
  2. About this talk How does it work? Brushing up some

    basics commands Some ADB commands you may have never heard about @zhenleiji
  3. 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
  4. Brushing up some basic commands… @zhenleiji

  5. ~$ adb Android Debug Bridge Usages @zhenleiji

  6. 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
  7. ~$ adb tcpip 5555 Debug connections Wireless debugging @zhenleiji

  8. ~$ adb tcpip 5555 Wireless debugging ~$ adb connect <device-ip>

    Debug connections @zhenleiji
  9. ~$ adb tcpip 5555 Wireless debugging ~$ adb connect <device-ip>

    ~$ adb usb Usb debugging Debug connections @zhenleiji
  10. @zhenleiji

  11. ~$ adb devices -l Devices Show device detail list @zhenleiji

  12. ~$ 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
  13. ~$ 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
  14. Transfer ~$ adb install <apk-path> Install/Uninstall an app ~$ adb

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

    uninstall <package> ~$ adb install -r <apk-path> Reinstall an app @zhenleiji
  16. 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
  17. Some ADB commands you may have never heard about… @zhenleiji

  18. ~$ adb shell pm Package Manager Call package manager @zhenleiji

  19. ~$ 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
  20. ~$ adb shell pm list packages Package Manager Show package

    list @zhenleiji
  21. ~$ adb shell pm list packages | grep youtube Package

    Manager Show package list @zhenleiji
  22. ~$ 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
  23. APK Analyzer Search for youtube package Locate youtube.apk path Pull

    youtube.apk to the machine
  24. Package Manager @zhenleiji

  25. Package Manager ~$ adb shell pm clear [package] Clear data

    @zhenleiji
  26. Package Manager ~$ adb shell pm clear [package] Clear data

    @zhenleiji
  27. 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
  28. ~$ 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
  29. Upload video @zhenleiji

  30. Activity Launcher @zhenleiji

  31. Deeplink

  32. Broadcast Receiver

  33. Content Provider

  34. Permissions

  35. Pending Intents

  36. Activity Back Stack

  37. Memory Usage

  38. ~$ adb shell am Activity Manager Call activity manager @zhenleiji

  39. ~$ 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
  40. ~$ adb shell am start [options] [intent] Start an Activity

    specified by intent Intent @zhenleiji
  41. ~$ 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 profiler and send results to file. -P file: Like --start-profiler, but profiling stops when the app goes idle. -R count: Repeat the activity launch count times. Prior to each repeat, the top activity will be finished. -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 specified, then run as the current user. @zhenleiji
  42. ~$ 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 prefix to create an explicit intent, such as com.example.app/.ExampleActivity. -f flags Add flags to the intent, as supported by setFlags(). @zhenleiji
  43. ~$ adb shell am start [options] [intent] Start an Activity

    specified by intent Intent @zhenleiji
  44. ~$ 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
  45. ~$ 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
  46. ~$ adb shell am force-stop [package] Force stop everything associated

    with package Kill @zhenleiji
  47. ~$ 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
  48. ~$ 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
  49. ~$ adb shell wm Window Manager Call window manager @zhenleiji

  50. ~$ 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. . . .
  51. Simulate other devices ~$ adb shell wm size [WxH|WdpxHdp|reset] Return

    or override display size @zhenleiji
  52. 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
  53. Default size and density

  54. Simulate a tablet

  55. Narrow screen

  56. Small screen

  57. System Services ~$ adb shell dumpsys Provides information about the

    status of all system services @zhenleiji
  58. System Services ~$ adb shell dumpsys -l List of system

    services ~$ adb shell dumpsys Provides information about the status of all system services
  59. 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
  60. Alarm Manager

  61. ~$ adb shell dumpsys activity Provides information about the Activity

    Manager
  62. ~$ adb shell dumpsys activity top Provides information about the

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

    top activities Fragment name Fragment state
  64. None
  65. uiautomatorviewer

  66. System Properties ~$ adb shell getprop List of system properties

    @zhenleiji
  67. System Properties ~$ adb shell getprop List of system properties

  68. System Properties ~$ adb shell getprop List of system properties

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

    ~$ adb shell getprop [system_property] Provides information about the specific system property
  70. None
  71. 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
  72. Enable overdraw @zhenleiji

  73. https://android.googlesource.com/platform/frameworks/base/+/master/libs/hwui/Properties.h Enable profile

  74. Disable animation

  75. Spotlight on steroids

  76. Alfred + adb https://github.com/zhenleiji/AlfredAndroidEspresso

  77. ~$ adb shell input Simulate Input Call input @zhenleiji

  78. ~$ 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
  79. 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
  80. 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
  81. Simulate Input

  82. Alfred + adb https://github.com/zhenleiji/AlfredAdbType

  83. Simulate Input Tap @zhenleiji ~$ adb shell input tap [X]

    [Y]
  84. Simulate Input Tap ~$ adb shell input swipe [X1] [Y1]

    [X2] [Y2] Swipe @zhenleiji ~$ adb shell input tap [X] [Y]
  85. Simulate Input

  86. Just to wrap up… @zhenleiji

  87. Versatile command-line tool Provides access to a Unix shell Powerful

    throughout development @zhenleiji
  88. Some tips… @zhenleiji

  89. Create alias @zhenleiji

  90. Create a folder for your scripts Environment Variable @zhenleiji

  91. Create scripts https://github.com/zhenleiji/AndroidScripts

  92. "Life is too short not to use ADB commands" @zhenleiji

  93. Zhenlei Ji A n d r o i d S

    o f t w a r e E n g i n e e r zhenlei.ji@gmail.com @zhenleiji THANK YOU!
  94. References https://developer.android.com/studio/command-line/adb http://developer.famoco.com/Android_development/ADB_commands/ http://adbshell.com/ https://jiafei427.wordpress.com/2016/11/21/adb-in-android/ https://gist.github.com/Pulimet/5013acf2cd5b28e55036c82c91bd56d8 https://nelenkov.blogspot.com/2012/06/unpacking-android-backups.html https://medium.com/@filipe.batista/ezadb-easy-android-debug-bridge-8cef841a7124 https://developer.android.com/studio/debug/bug-report https://source.android.com/setup/contribute/read-bug-reports

    https://proandroiddev.com/adb-commands-part-iii-997f97c03546 https://www.alfredapp.com/ @zhenleiji