How to use ADB
📌Introduction to adb
ADB (Android Debug Bridge) is the tool we use to interact and debug on our target device or emulator.
ADB consists of three parts
ADB-Client
ADB-Server
daemon (ADBD)
The Client and Server run on the computer, while the daemon runs on the device and is called
adbd.The ADB server running on our computer communicates with the daemon via USB or TCP/IP.
📍How do they work?

The ADB Client sends a command (e.g.,
adb shell) to the server.The ADB server handles the connection to the device.
The device executes the command and sends a response back to the server, which forwards it to the client for display.
If you have installed
adbvia Android Studio you still need to configure thePATHvariable to make sure your shell is able to find theadbtool.
📌ADB Port Forwarding
Port forwarding is super handy when you want to access a certain port on a device.
Example:
Let’s say you have a certain app on your device running on port
31415. If you want to connect to this port from your Ubuntu machine, it is not possible because it is only available on the local device.

📌 This is where ADB forwarding becomes really useful.
You can use the existing ADB connection to access this port using the
adb forwardcommand:In our example, the command would be:
So the command would do the following:
This will open port
1337on your Ubuntu machine.All the packets sent to this port will be forwarded to
31415on the device, allowing you to communicate with this port.
📌ADB Port reverse
This is the exact reverse process.
It allows the smartphone to access a service running on your Ubuntu machine.
You can use the existing ADB connection to access a port on your Ubuntu machine using the
adb reversecommand.Example:
Let’s say you have a web server running on your Ubuntu machine at port
8080, and you want to access this server from an app on your smartphone. Normally, the app on the phone wouldn't be able to directly access the web server running on your computer.With ADB reverse, you can make this possible:
What this command does:
It opens port
9090on your phone.Any traffic sent to
9090on your phone will be forwarded to8080on your Ubuntu machine, allowing the phone to access the web server running on your computer.This enables your smartphone to access the service running on port
8080of your Ubuntu machine.
📌ADB commands
📍Test adb
To ensure
adbis working, just run:
📍Using adb
Run
adb devicesto get a full list of devices. If you have multiple devices installed, you can:Specify the active device using the
sparameter, for example:adb -s emulator-5554 shellSpecify to use a single USB device using the
dparameter, for example:adb -d shell
📍adb shell
Using
adb shellyou get dropped into a regular Linux shell on your device. HitCtrl+Dor typeexitto leave the shell.
📌Transferring Files
📍adb push
To transfer files between our computer and the device, we use the commands
adb pushandadb pull.With
adb pushwe can push a file or a directory from our computer to the device. We have to specify a destination path - a common one is/sdcard/, which is not an external SD-card, but generally mounts to the internal storage of the device.Example to push test.png from the Desktop to the Download folder of the device:
📍adb pull
Command
With
adb pullwe can pull files from the device to the computer. For example, to download the entire Download folder from the device, we can use:
Note that with
adb pullwe can only access the files we have access to withadb shell, and so you will find that you can not download a lot of application files this way.
📍Android Studio Device Explorer
Android Studio features an integrated Device Explorer, which enables us to explore device storage with a user-friendly UI.
You need to click on the Device Explorer icon; you will then get a nice UI to browse through the device's file system. You can even drop files simply by dragging and dropping.
📌Managing Apps
Using adb install, we can manually install packages using the command line.
Uninstalls the specified application.
Lists all installed packages, including system packages.
List only third party packages.
Clear the application data without removing the actual application
List information such as activities and permissions of a package.
Starts the activity of the specified package.
pm = package manager
It's an Android tool to manage packages, permissions, etc.
dumpsys
Provides information about system services and packages.
am = Activity Manager
Let us start & stop activities, services, etc.
Backup all applications that have the ”backup=true“ in their Manifest.xml file
📌Exploring Logs with logcat
Logcat is a tool in Android that shows system and app logs, allowing developers to view real-time log messages and debug their apps.
adb logcat
Change the log format - for example, using
briefto get a more condensed version of the log.In some cases, there can be lots of log entries which makes it hard to focus on the things that matter. For example, if you are only interested in the logs produced by the
MainActivity, you can use a log filter for that:Filter format:
MainActivity:Vensures that logs from the tag MainActivity with severity of Verbose and above are logged:SEnsures that all other Tags are ignored (as nothing will log with log-level Silent or above)
Logging severities
Symbol
Log level
V
Verbose
D
Debug
I
Info
W
Warning
E
Error
F
Fatal
S
Silent
Example

I ⇒ Info
Finsky ⇒ It's a tag for this log message. It is chosen by the app developer, and in most cases indicates the service or the activity from which this log message originated.
( 9874) ⇒ It’s the process ID of the logging process.
Last updated