Android™ Debug Bridge (adb)
The Swiss Army Knife for Android
Installation
1) Download the Android SDK Platform Tools ZIP file for Linux.
2) Extract the ZIP to an easily-accessible location (like the Desktop for example).
3) Open a Terminal window.
4) Enter the following command: cd /path/to/extracted/folder/
5) This will change the directory to where you extracted the ADB files.
6) So for example: cd /Users/Doug/Desktop/platform-tools/
7. Connect your device to your Linux machine with your USB cable.
Change the connection mode to “file transfer (MTP)” mode.
This is not always necessary for every device, but it’s recommended so you don’t run into any issues.
8) Once the Terminal is in the same folder your ADB tools are in, you can execute the following command to launch the ADB daemon: ./adb devices
9) Back on your smartphone or tablet device, you’ll see a prompt asking you to allow USB debugging. Go ahead and grant it.
1) Download: https://dl.google.com/android/repository/platform-tools-latest-windows.zip
2) Extract the contents of this ZIP file into an easily accessible folder (such as C:\platform-tools)
3) Open Windows explorer and browse to where you extracted the contents of this ZIP file
4) Then open up a Command Prompt from the same directory as this ADB binary. This can be done by holding
Shift and Right-clicking within the folder then click the “Open command window here” option.
5) Connect your smartphone or tablet to your computer with a USB cable.
Change the USB mode to “file transfer (MTP)” mode. Some OEMs may or may not require this,
but it’s best to just leave it in this mode for general compatibility.
6) In the Command Prompt window, enter the following command to launch the ADB daemon: adb devices
7) On your phone’s screen, you should see a prompt to allow or deny USB Debugging access. Naturally,
you will want to grant USB Debugging access when prompted (and tap the always allow check box if you never want to see that prompt again).
8) Finally, re-enter the command from step
you should now see your device’s serial number in the command prompt (or the PowerShell window).
Bash: Aliases For ADB
#!/bin/bash
cat <<! >> ~/.bashrc
function startintent() {
adb devices \
|tail -n +2 \
|cut -sf 1 \
|xargs -I X adb -s X shell am start $(1)
function apkinstall() {
adb devices \
|tail -n +2 \
|cut -sf 1 \
|xargs -I X \
adb -s X install -r $(1)
}
function rmapp() {
adb devices \
|tail -n +2 \
|cut -sf 1 \
|xargs -I X adb -s X uninstall $(1)
}
function clearapp() {
adb devices \
|tail -n +2 \
|cut -sf 1 \
|xargs -I X adb -s X shell cmd package clear $(1)
}
Disable sim warning for service provider
Execute the help command -h
on all options in cmd list for find valid commands in shell
Dump all cmd commands with -h
option ready to use
Follow and monitor the battery charging in realtime
adb logcat -c
adb logcat -s AODBatteryManager | awk -F' ' '/mRemainingChargeTime/ {
split($15, arr, "=")
gsub(",", "", arr[2])
minutes = int(arr[2] / 60000)
seconds = int((arr[2] % 60000) / 1000)
if (!found) {
print "Estimated time until fully charged: " minutes " min " seconds " seconds"
found=1
exit
}
}'
Capture all touches and create the adb shell commands in realtime
- Shared on this gist: Adb useful commands list
adb shell getevent -l | awk '
/EV_ABS/ {
event[$3] = strtonum("0x" $NF)
}
/EV_SYN/ {
if (event["ABS_MT_POSITION_X"] && event["ABS_MT_POSITION_Y"]) {
printf "adb shell input touchscreen swipe %d %d %d %d 1000\n",
event["ABS_MT_POSITION_X"], event["ABS_MT_POSITION_Y"],
event["ABS_MT_POSITION_X"], event["ABS_MT_POSITION_Y"]
}
}
Sniff network traffic to stdout
Dump all cmd commands with -h option ready to use
- Launch this when you working in in the native shell
Execute -h
on all options in cmd list for find valid commands in shell
- Launch this when you working in in the native shell
Efficient Retrieval of ADB Shell Command Help Information
- This command will launch --help IF
-h
is not working
#!/system/bin/sh
### Script works as: If `-h` is unknown, try `--help`
### If "-h" worked, print its output and skip `--help` for the same command
handle_command() {
cmd_output=$(adb shell cmd "$1" -h 2>&1)
if echo "$cmd_output" | grep -q "Unknown command"; then
adb shell cmd "$1" --help 2>/dev/null
else
echo "$cmd_output"
fi
}
export -f handle_command
adb shell cmd -l | parallel -j $(( $(nproc) + 1 )) handle_command
Enhancing Command-Line Productivity and Debugging - Random Colorized Output
- Launch this when you working in in the native shell
adb shell cmd -l | xargs -n 1 -P $(( $(nproc) + 1 )) sh -c '
color=$((RANDOM%254+1))
printf "\033[38;5;${color}m"
printf "%-80s\n" | tr " " "-"
printf "cmd $0 -h\033[0m\n"
cmd "$0" -h | awk -v color="$color" "
BEGIN {
line=sprintf(\"%-80s\", \"\")
gsub(/ /, \"-\", line)
print \"\033[38;5;\" color \"m\" line \"\033[0m\"
}
{
print \"\033[38;5;\" color \"m\" \$0 \"\033[0m\"
}
"
'
Print device uptime in days
Print how many times device has booted since last factory reset
Connect to adb over wifi by copy/paste
#!/bin/bash
# Author: wuseman
port="5555"
interface=$(adb shell ip addr | awk '/state UP/ {print $2}' | sed 's/.$//'; )
ip=$(adb shell ifconfig ${interface} \
|egrep -o '(\<([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\>\.){3}\<([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\>' 2> /dev/null \
|head -n 1)
adb tcpip ${port};sleep 0.5
adb connect $ip:${port}; sleep 1.0
adb devices; adb shell
Listing Currently Running Applications
Add a Contact, fill info and press save on device
adb shell am start -a android.intent.action.INSERT \
-t vnd.android.cursor.dir/contact \
-e name 'wuseman' \
-e phone '+467777701' \
-e email 'wuseman@nr1.nu' \
-e postal 'Street 10, New York'
- For press save contact via shell from above command
Launch accessbility window
Initiates a Wi-Fi scan on the Android device
Dumping Information of Current Application in Use (Android 12/13)
Print Warranty Status (Samsung)
Indicating Device Completion
Dumping All Launchable Activities for a Preferred Application
Dump Unique Package Names from 'dumpsys package' History
Dumping Launchable Activities for All Installed Applications on the Device
packages=$(adb shell cmd package list packages | awk -F':' '{ print $2 }' | sort)
do_work() {
package=$1
echo "Processing $package"
# Dump package info, find activities and create adb commands
adb shell dumpsys package $package | \
grep -io com.*activity | \
sed "s/^/adb shell am start -n '/;s/$/\'/g" | sed 's/\$/\\$/g'
}
export -f do_work
echo "$packages" | parallel -j4 do_work
#!/bin/bash
packages=$(adb shell cmd package list packages | awk -F':' '{ print $2 }' | sort)
do_work() {
package=$1
echo "Processing $package"
adb shell dumpsys package $package | \
grep -io com.*activity | \
sed "s/^/adb shell am start -n '/;s/$/\'/g" | sed 's/\$/\\$/g'
}
export -f do_work
echo "$packages" | xargs -I {} -P 4 -n 1 bash -c 'do_work "$@"' _ {}
Dumping Samsung Telephony UI Receivers
Dump IMEI
Read screen via uiautomor and print IMEI for all active esim/sim cards on device
Dump screen state
Chroot into termux env from android shell
export PREFIX='/data/data/com.termux/files/usr'
export HOME='/data/data/com.termux/files/home'
export LD_LIBRARY_PATH='/data/data/com.termux/files/usr/lib'
export PATH="/data/data/com.termux/files/usr/bin:/data/data/com.termux/files/usr/bin/applets:$PATH"
export LANG='en_US.UTF-8'
export SHELL='/data/data/com.termux/files/usr/bin/bash'
cd "$HOME"
exec "$SHELL" -l
Follow activities by dumpsys the apps that launch the activites
previous_activity=""
while true; do
current_activity=$(adb shell dumpsys activity | grep -i mCurrentFocus | awk 'NR==1{print $3}' | cut -d'}' -f1)
if [ "$current_activity" != "$previous_activity" ]; then
echo "$current_activity"
previous_activity="$current_activity"
fi
sleep 1 # Adjust the sleep interval as needed
done
Find Activities for Currently Running Applications - Follow activities
- Updated: 2024-02-19
adb logcat | awk '
/act=android.intent.action.MAIN/ && /cmp=/ {
match($0, /act=[^ ]+/);
act=substr($0, RSTART+4, RLENGTH-4);
match($0, /cmp=[^ ]+/);
cmp=substr($0, RSTART+4, RLENGTH-4);
# Exclude lines where cmp ends with "}"
if (cmp ~ /\}$/) next;
key = act " -n " cmp;
if (!seen[key]++) {
printf "adb shell am start -a '\''";
# Apply color to action
printf "\033[38;5;202m" act "\033[0m";
printf "'\'' -n '\''";
# Apply color to component
printf "\033[38;5;46m" cmp "\033[0m";
print "'\''";
}
}'
Find Broadcast Receiver for Currently Running Applications (follow services)
Find Services for Currently Running Applications (follow services)
Follow and filter lines that indicate an intent is being broadcast
This script extracts the action (act), category (cat), flags (flg), and package (pkg) from the intent and constructs an adb shell am broadcast command.
-
This script will process adb logcat output, looking for lines that indicate an intent is being broadcast. It extracts the action (act), category ( cat), flags (flg), and package (pkg) from the intent and constructs an adb shell am broadcast command.
-
The command is color-coded for easier reading: action in blue, category in yellow, flags in green, and package in purple.
-
The script assumes that the intent will always have an action and a package. If additional parameters like category and flags are present, they are included in the command.
adb logcat | awk -F'[ =[]' '/Broadcasting: Intent/ {
act=""; cat=""; flg=""; ez=""; pkg="";
for (i = 1; i <= NF; i++) {
if ($i == "act") { act = $(i + 1) }
else if ($i == "cat") { cat = $(i + 1) }
else if ($i == "flg") { flg = $(i + 1) }
else if ($i == "ez") { ez = $(i + 1) }
else if ($i == "pkg") { pkg = $(i + 1) }
}
if (act != "" && pkg != "") {
printf "adb shell am broadcast -a \033[1;34m'\''%s'\''\033[0m ", act;
if (cat != "") { printf "-c \033[1;33m'\''%s'\''\033[0m ", cat }
if (ez != "") { printf "--ez \033[1;32m'\''%s'\''\033[0m ", ez }
printf "-p \033[1;35m'\''%s'\''\033[0m\n", pkg
}
}'
Stream device screen via FFmpeg
Stream device screen via Mplayer
Search for AT commands with ripgrep
sourcePath="/path/to/android/files"
mkdir -p ~/search_index
rg --stats --no-column --colors 'match:fg:141' --colors 'match:bg:234' '(AT[+%$]([A-Z]+|\d+)|PACMD|lock(_?|Settings)ettings|weaver.*matched)' $sourcePath | while read line; do
echo "$line"
if [[ "$line" =~ PACMD ]]; then
echo "$line" >> ~/search_index/pacmd.log
fi
if [[ "$line" =~ AT[+%$] ]]; then
echo "$line" >> ~/search_index/at-commands.log
fi
if [[ "$line" =~ weaver ]]; then
echo "$line" >> ~/search_index/weaver-commands.log
fi
done
Receiver used for testing emergency cell broadcasts
Setting up Device Provisioning and Completing Setup
Move ring and notification sounds to default path
Create symlink of our custom ringtones and notification path to default media path
Set ringtone from /sdcard/
Set ringtone to foo.ogg
over adb from PC
UDEV Rules Mostly Brands
SUBSYSTEM=="usb", ATTR{idVendor}=="0502", MODE="0666", GROUP="plugdev" #Acer
SUBSYSTEM=="usb", ATTR{idVendor}=="0b05", MODE="0666", GROUP="plugdev" #ASUS
SUBSYSTEM=="usb", ATTR{idVendor}=="413c", MODE="0666", GROUP="plugdev" #Dell
SUBSYSTEM=="usb", ATTR{idVendor}=="0489", MODE="0666", GROUP="plugdev" #Foxconn
SUBSYSTEM=="usb", ATTR{idVendor}=="04c5", MODE="0666", GROUP="plugdev" #Fujitsu
SUBSYSTEM=="usb", ATTR{idVendor}=="04c5", MODE="0666", GROUP="plugdev" #Fujitsu Toshiba
SUBSYSTEM=="usb", ATTR{idVendor}=="091e", MODE="0666", GROUP="plugdev" #Garmin-Asus
SUBSYSTEM=="usb", ATTR{idVendor}=="18d1", MODE="0666", GROUP="plugdev" #Google
SUBSYSTEM=="usb", ATTR{idVendor}=="201E", MODE="0666", GROUP="plugdev" #Haier
SUBSYSTEM=="usb", ATTR{idVendor}=="109b", MODE="0666", GROUP="plugdev" #Hisense
SUBSYSTEM=="usb", ATTR{idVendor}=="0bb4", MODE="0666", GROUP="plugdev" #HTC
SUBSYSTEM=="usb", ATTR{idVendor}=="12d1", MODE="0666", GROUP="plugdev" #Huawei
SUBSYSTEM=="usb", ATTR{idVendor}=="24e3", MODE="0666", GROUP="plugdev" #K-Touch
SUBSYSTEM=="usb", ATTR{idVendor}=="2116", MODE="0666", GROUP="plugdev" #KT Tech
SUBSYSTEM=="usb", ATTR{idVendor}=="0482", MODE="0666", GROUP="plugdev" #Kyocera
SUBSYSTEM=="usb", ATTR{idVendor}=="17ef", MODE="0666", GROUP="plugdev" #Lenovo
SUBSYSTEM=="usb", ATTR{idVendor}=="1004", MODE="0666", GROUP="plugdev" #LG
SUBSYSTEM=="usb", ATTR{idVendor}=="22b8", MODE="0666", GROUP="plugdev" #Motorola
SUBSYSTEM=="usb", ATTR{idVendor}=="0e8d", MODE="0666", GROUP="plugdev" #MTK
SUBSYSTEM=="usb", ATTR{idVendor}=="0409", MODE="0666", GROUP="plugdev" #NEC
SUBSYSTEM=="usb", ATTR{idVendor}=="2080", MODE="0666", GROUP="plugdev" #Nook
SUBSYSTEM=="usb", ATTR{idVendor}=="0955", MODE="0666", GROUP="plugdev" #Nvidia
SUBSYSTEM=="usb", ATTR{idVendor}=="2257", MODE="0666", GROUP="plugdev" #OTGV
SUBSYSTEM=="usb", ATTR{idVendor}=="10a9", MODE="0666", GROUP="plugdev" #Pantech
SUBSYSTEM=="usb", ATTR{idVendor}=="1d4d", MODE="0666", GROUP="plugdev" #Pegatron
SUBSYSTEM=="usb", ATTR{idVendor}=="0471", MODE="0666", GROUP="plugdev" #Philips
SUBSYSTEM=="usb", ATTR{idVendor}=="04da", MODE="0666", GROUP="plugdev" #PMC-Sierra
SUBSYSTEM=="usb", ATTR{idVendor}=="05c6", MODE="0666", GROUP="plugdev" #Qualcomm
SUBSYSTEM=="usb", ATTR{idVendor}=="1f53", MODE="0666", GROUP="plugdev" #SK Telesys
SUBSYSTEM=="usb", ATTR{idVendor}=="04e8", MODE="0666", GROUP="plugdev" #Samsung
SUBSYSTEM=="usb", ATTR{idVendor}=="04dd", MODE="0666", GROUP="plugdev" #Sharp
SUBSYSTEM=="usb", ATTR{idVendor}=="054c", MODE="0666", GROUP="plugdev" #Sony
SUBSYSTEM=="usb", ATTR{idVendor}=="0fce", MODE="0666", GROUP="plugdev" #Sony Ericsson
SUBSYSTEM=="usb", ATTR{idVendor}=="2340", MODE="0666", GROUP="plugdev" #Teleepoch
SUBSYSTEM=="usb", ATTR{idVendor}=="0930", MODE="0666", GROUP="plugdev" #Toshiba
SUBSYSTEM=="usb", ATTR{idVendor}=="19d2", MODE="0666", GROUP="plugdev" #ZTE
Happy Debugging!
- Author: Patrik Gissleholm