Advertisement
v1ral_ITS

ADB CMDS

Apr 25th, 2019
192
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 9.71 KB | None | 0 0
  1. == Adb Server
  2. adb kill-server
  3. adb start-server
  4.  
  5. == Adb Reboot
  6. adb reboot
  7. adb reboot recovery
  8. adb reboot-bootloader
  9.  
  10. == Shell
  11. adb shell    // Open or run commands in a terminal on the host Android device.
  12.  
  13. == Devices
  14. adb usb
  15. adb devices   //show devices attached
  16. adb connect ip_address_of_device
  17.  
  18. == Get device android version
  19. adb shell getprop ro.build.version.release
  20.  
  21. == LogCat
  22. adb logcat
  23. adb logcat -c // clear // The parameter -c will clear the current logs on the device.
  24. adb logcat -d > [path_to_file] // Save the logcat output to a file on the local system.
  25. adb bugreport > [path_to_file] // Will dump the whole device information like dumpstate, dumpsys and logcat output.
  26.  
  27. == Files
  28. adb push [source] [destination]    // Copy files from your computer to your phone.
  29. adb pull [device file location] [local file location] // Copy files from your phone to your computer.
  30.  
  31. == App install
  32. adb -e install path/to/app.apk
  33.  
  34. -d                        - directs command to the only connected USB device...
  35. -e                        - directs command to the only running emulator...
  36. -s <serial number>        ...
  37. -p <product name or path> ...
  38. The flag you decide to use has to come before the actual adb command:
  39.  
  40. adb devices | tail -n +2 | cut -sf 1 | xargs -IX adb -s X install -r com.myAppPackage // Install the given app on all connected devices.
  41.  
  42. == Uninstalling app from device
  43. adb uninstall com.myAppPackage
  44. adb uninstall <app .apk name>
  45. adb uninstall -k <app .apk name> -> "Uninstall .apk withour deleting data"
  46.  
  47. adb shell pm uninstall com.example.MyApp
  48. adb shell pm clear [package] // Deletes all data associated with a package.
  49.  
  50. adb devices | tail -n +2 | cut -sf 1 | xargs -IX adb -s X uninstall com.myAppPackage //Uninstall the given app from all connected devices
  51.  
  52. == Update app
  53. adb install -r yourApp.apk  //  -r means re-install the app and keep its data on the device.
  54. adb install –k <.apk file path on computer>
  55.  
  56. == Home button
  57. adb shell am start -W -c android.intent.category.HOME -a android.intent.action.MAIN
  58.  
  59. == Activity Manager
  60. adb shell am start -a android.intent.action.VIEW
  61. adb shell am broadcast -a 'my_action'
  62.  
  63. adb shell am start -a android.intent.action.CALL -d tel:+972527300294 // Make a call
  64.  
  65. // Open send sms screen with phone number and the message:
  66. adb shell am start -a android.intent.action.SENDTO -d sms:+972527300294   --es  sms_body "Test --ez exit_on_sent false
  67.  
  68. // Reset permissions
  69. adb shell pm reset-permissions -p your.app.package
  70. adb shell pm grant [packageName] [ Permission]  // Grant a permission to an app.
  71. adb shell pm revoke [packageName] [ Permission]   // Revoke a permission from an app.
  72.  
  73.  
  74. // Emulate device
  75. adb shell wm size 2048x1536
  76. adb shell wm density 288
  77. // And reset to default
  78. adb shell wm size reset
  79. adb shell wm density reset
  80.  
  81. == Print text
  82. adb shell input text 'Wow, it so cool feature'
  83.  
  84. == Screenshot
  85. adb shell screencap -p /sdcard/screenshot.png
  86.  
  87. $ adb shell
  88. shell@ $ screencap /sdcard/screen.png
  89. shell@ $ exit
  90. $ adb pull /sdcard/screen.png
  91.  
  92. ---
  93. adb shell screenrecord /sdcard/NotAbleToLogin.mp4
  94.  
  95. $ adb shell
  96. shell@ $ screenrecord --verbose /sdcard/demo.mp4
  97. (press Control + C to stop)
  98. shell@ $ exit
  99. $ adb pull /sdcard/demo.mp4
  100.  
  101. == Key event
  102. adb shell input keyevent 3 // Home btn
  103. adb shell input keyevent 4 // Back btn
  104. adb shell input keyevent 5 // Call
  105. adb shell input keyevent 6 // End call
  106. adb shell input keyevent 26  // Turn Android device ON and OFF. It will toggle device to on/off status.
  107. adb shell input keyevent 27 // Camera
  108. adb shell input keyevent 64 // Open browser
  109. adb shell input keyevent 66 // Enter
  110. adb shell input keyevent 67 // Delete (backspace)
  111. adb shell input keyevent 207 // Contacts
  112. adb shell input keyevent 220 / 221 // Brightness down/up
  113. adb shell input keyevent 277 / 278 /279 // Cut/Copy/Paste
  114. // https://developer.android.com/reference/android/view/KeyEvent.html
  115.  
  116. == ShPref
  117. # replace org.example.app with your application id
  118.  
  119. # Add a value to default shared preferences.
  120. adb shell 'am broadcast -a org.example.app.sp.PUT --es key key_name --es value "hello world!"'
  121.  
  122. # Remove a value to default shared preferences.
  123. adb shell 'am broadcast -a org.example.app.sp.REMOVE --es key key_name'
  124.  
  125. # Clear all default shared preferences.
  126. adb shell 'am broadcast -a org.example.app.sp.CLEAR --es key key_name'
  127.  
  128. # It's also possible to specify shared preferences file.
  129. adb shell 'am broadcast -a org.example.app.sp.PUT --es name Game --es key level --ei value 10'
  130.  
  131. # Data types
  132. adb shell 'am broadcast -a org.example.app.sp.PUT --es key string --es value "hello world!"'
  133. adb shell 'am broadcast -a org.example.app.sp.PUT --es key boolean --ez value true'
  134. adb shell 'am broadcast -a org.example.app.sp.PUT --es key float --ef value 3.14159'
  135. adb shell 'am broadcast -a org.example.app.sp.PUT --es key int --ei value 2015'
  136. adb shell 'am broadcast -a org.example.app.sp.PUT --es key long --el value 9223372036854775807'
  137.  
  138. # Restart application process after making changes
  139. adb shell 'am broadcast -a org.example.app.sp.CLEAR --ez restart true'
  140.  
  141. == Monkey
  142. adb shell monkey -p com.myAppPackage -v 10000 -s 100 // monkey tool is generating 10.000 random events on the real device
  143.  
  144.  
  145. == Other
  146. adb backup // Create a full backup of your phone and save to the computer.
  147. adb restore // Restore a backup to your phone.
  148. adb sideload //  Push and flash custom ROMs and zips from your computer.
  149.  
  150. fastboot devices
  151. // Check connection and get basic information about devices connected to the computer.
  152. // This is essentially the same command as adb devices from earlier.
  153. //However, it works in the bootloader, which ADB does not. Handy for ensuring that you have properly established a connection.
  154.  
  155.  
  156. --------------------------------------------------------------------------------
  157. Shared Preferences
  158.  
  159. # replace org.example.app with your application id
  160.  
  161. # Add a value to default shared preferences.
  162. adb shell 'am broadcast -a org.example.app.sp.PUT --es key key_name --es value "hello world!"'
  163.  
  164. # Remove a value to default shared preferences.
  165. adb shell 'am broadcast -a org.example.app.sp.REMOVE --es key key_name'
  166.  
  167. # Clear all default shared preferences.
  168. adb shell 'am broadcast -a org.example.app.sp.CLEAR --es key key_name'
  169.  
  170. # It's also possible to specify shared preferences file.
  171. adb shell 'am broadcast -a org.example.app.sp.PUT --es name Game --es key level --ei value 10'
  172.  
  173. # Data types
  174. adb shell 'am broadcast -a org.example.app.sp.PUT --es key string --es value "hello world!"'
  175. adb shell 'am broadcast -a org.example.app.sp.PUT --es key boolean --ez value true'
  176. adb shell 'am broadcast -a org.example.app.sp.PUT --es key float --ef value 3.14159'
  177. adb shell 'am broadcast -a org.example.app.sp.PUT --es key int --ei value 2015'
  178. adb shell 'am broadcast -a org.example.app.sp.PUT --es key long --el value 9223372036854775807'
  179.  
  180. # Restart application process after making changes
  181. adb shell 'am broadcast -a org.example.app.sp.CLEAR --ez restart true'
  182. --------------------------------------------------------------------------------
  183.  
  184. === Few bash snippets ===
  185. @Source (https://jonfhancock.com/bash-your-way-to-better-android-development-1169bc3e0424)
  186.  
  187. === Using tail -n
  188. //Use tail to remove the first line. Actually two lines. The first one is just a newline. The second is “List of devices attached.�
  189. $ adb devices | tail -n +2
  190.  
  191. === Using cut -sf
  192. // Cut the last word and any white space off the end of each line.
  193. $ adb devices | tail -n +2 | cut -sf -1
  194.  
  195. === Using xargs -I
  196. // Given the -I option, xargs will perform an action for each line of text that we feed into it.
  197. // We can give the line a variable name to use in commands that xargs can execute.
  198. $ adb devices | tail -n +2 | cut -sf -1 | xargs -I X echo X aw yiss
  199.  
  200. === Three options below together
  201. // Will print android version of all connected devices
  202. adb devices | tail -n +2 | cut -sf -1 | xargs -I X adb -s X shell getprop ro.build.version.release  
  203.  
  204. === Using alias
  205. -- Example 1
  206. alias tellMeMore=echo
  207. tellMeMore "hi there"
  208. Output => hi there
  209. -- Example 2
  210. // Define alias
  211. alias apkinstall="adb devices | tail -n +2 | cut -sf 1 | xargs -I X adb -s X install -r $1"
  212. // And you can use it later
  213. apkinstall ~/Downloads/MyAppRelease.apk  // Install an apk on all devices
  214. -- Example 3
  215. alias rmapp="adb devices | tail -n +2 | cut -sf 1 | xargs -I X adb -s X uninstall $1"
  216. rmapp com.example.myapp // Uninstall a package from all devices
  217. -- Example 4
  218. alias clearapp="adb devices | tail -n +2 | cut -sf 1 | xargs -I X adb -s X shell pm clear $1"
  219. clearapp com.example.myapp  // Clear data on all devices (leave installed)
  220. -- Example 5
  221. alias startintent="adb devices | tail -n +2 | cut -sf 1 | xargs -I X adb -s X shell am start $1"
  222. startintent https://twitter.com/JonFHancock // Launch a deep link on all devices
  223.  
  224.  
  225. Setting up your .bash_profile
  226. Finally, to make this all reusable even after rebooting your computer (aliases only last through the current session), we have to add these to your .bash_profile. You might or might not already have a .bash_profile, so let’s make sure we append to it rather than overwriting it. Just open a terminal, and run the following command
  227.  
  228. touch .bash_profile && open .bash_profile
  229.  
  230. This will create it if it doesn’t already exist, and open it in a text editor either way. Now just copy and paste all of the aliases into it, save, and close.
  231.  
  232. alias startintent="adb devices | tail -n +2 | cut -sf 1 | xargs -I X adb -s X shell am start $1"
  233. alias apkinstall="adb devices | tail -n +2 | cut -sf 1 | xargs -I X adb -s X install -r $1"
  234. alias rmapp="adb devices | tail -n +2 | cut -sf 1 | xargs -I X adb -s X uninstall $1"
  235. alias clearapp="adb devices | tail -n +2 | cut -sf 1 | xargs -I X adb -s X shell pm clear $1"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement