A quick reference to ADB.
Created on: 2019-02-03
Tag: cheat_sheet
To see the list of all packages:
adb shell 'pm list packages'
To uninstall an app:
adb uninstall -k --user 0 $package
source: How to uninstall a system app using adb uninstall command (NOT remove via rm or any other way)
To backup system and app data but not the apps:
adb backup -all
By default it saves device data to the platform-tools folder as backup.ab
To backup system and app data to a file but not the apps:
adb backup -all -f $path_to_backup_file.ab
To restore data:
adb restore $path_to_backup_file.ab
to backup and restore a specific app, first list all the packages with:
adb shell pm list packages
Now backup the specific package with:
adb backup -f com.whatsapp.ab -noapk com.whatsapp
The command above indicates that we want to backup com.whatsapp package to com.whatsapp.ab file as indicated with the -f flag. The .ab says it is an Adroid Backup file. The -noapk flag indicates that we don't want the .apk file to be backed up. Now we should see a confirmation dialog prompt to start the backup. The backup will not start until we press Back up my data.
We can convert the ADB backup file to a TAR archive. First we need to stripe the 24-bite header and the use openssl to decompress the raw zlib data:
dd if=com.whatsapp.ab ibs=24 skip=1 | openssl zlib -d > com.whatsapp.tar
Or we can use tail command instead of dd:
tail -c +25 com.whatsapp.ab | openssl zlib -d > com.whatsapp.tar
Now it is plain simple decompress with tar:
tar xvf com.whatsapp.tar
To restore just do:
adb restore com.whatsapp.ab
But we have to take one more step to add 24-bite header to the .ab file if we changed the contains. For this we will also need the original .ab file.:
dd if=com.whatsapp.ab ibs=24 count=1 of=com.whatsapp-restore.ab ; openssl zlib -in com.whatsapp.tar >> com.whatsapp-restore.ab
We can also use the head command to do that as well:
head -c +24 com.whatsapp.ab > com.whatsapp-restore.ab ; openssl zlib -in com.whatsapp.tar >> com.whatsapp-restore.ab
Now restore it like before:
adb restore com.whatsapp-restore.ab
source: https://stackpointer.io/mobile/android-adb-backup-extract-restore-repack/372/ and https://forum.xda-developers.com/showthread.php?t=2011811
TO see the current activity:
adb shell dumpsys window windows | grep -E 'mCurrentFocus|mFocusedApp'
source: ADB - Android - Getting the name of the current activity
To start an application aka an activity of an application:
adb shell am start -n com.package.name/com.package.name.ActivityName
source: How to start an application using android ADB tools?
To scroll up and down:
adb shell input swipe 300 300 500 1000 #up adb shell input swipe 500 1000 300 300 #down
To send text using virtual keyboard:
adb shell input text "Hello World"
To send keyevent as physical keyboard:
adb shell input keyevent 66 #66 is key_code for enter
List of all key_code
To tap aka click on screen:
adb shell input tap x y
source: How to use ADB to send touch events to device using sendevent command?
to see log of a specific package
adb shell 'logcat --pid=$(pidof -s <package_name>)'
we can use adb over wifi or specifically with a tcp connection. To use adb over wifi, first connect the phone via usb and enable usb debug. then list all device:
adb devices
this should give a output like this:
device_id device
now check the ip of the android device with:
adb shell ifconfig
output:
wlan0 Link encap:UNSPEC Driver icnss inet addr:XXX.XXX.X.XX Bcast:XXX.XXX.X.XXX
take note of the ip address after inet addr. we will need it later. now restart tcpip at some port with:
adb tcpip $port
for example 5555:
adb tcpip 5555
you can disconnect the use now. to connect to the device now just give the following command:
adb connect $ip:$port
like:
adb connect 192.168.1.4:5555
source: How can I connect to Android with ADB over TCP? and Android Debug Bridge (adb) | Android Developers: Connect to a device over Wi-Fi
to take a screenshot:
adb exec-out screencap -p > screen.png
https://stackoverflow.com/a/37191719/5350059
or save it in phone then pull:
adb shell /system/bin/screencap -p /sdcard/screenshot.png adb pull /sdcard/screenshot.png screenshot.png
source: which commands line are used to take a screenshot on android device (except screencap)
changes are divided into 3 namespace: system, secure, global. we can get, put, delete individual keys and list all in a namespace. to list all in system:
adb shell settings --user 0 list system
get, put and delete the same:
adb shell settings --user 0 get $namespace $key adb shell settings --user 0 put $namespace $key $value adb shell settings --user 0 delete $namespace $key
adb shell settings --user 0 put system hotspot_max_station_num $num # num=0-6 where 0 is unlimited
adb shell su -c 'svc wifi enable'
adb shell su -c 'svc wifi disable'
source: https://stackoverflow.com/a/10038568
Note
both of this requires root access
to specify particular device among multiple devices:
adb -s device_id shell
source: https://stackoverflow.com/a/14655015 source: https://developer.android.com/studio/command-line/adb#directingcommands
to copy files to a device:
adb push local remote
to copy files from a device:
adb push local remote
source: https://developer.android.com/studio/command-line/adb#copyfiles
to launch fragments:
adb shell am start -n com.android.settings/com.android.settings.SubSettings -e :android:show_fragment com.android.settings.users.UserSettings
The -e :android:show_fragment part is important here.
to get events:
adb shell su -- getevent -lt /dev/input/event1
to kill an app:
adb shell am force-stop com.my.app.package