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?

  1. The ADB Client sends a command (e.g., adb shell) to the server.

  2. The ADB server handles the connection to the device.

  3. 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 adb via Android Studio you still need to configure the PATH variable to make sure your shell is able to find the adb tool.

📌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 forward command:

      adb forward tcp:<computer_port> tcp:<phone_port>
    • In our example, the command would be:

      adb forward tcp:1337 tcp:31415
    • So the command would do the following:

      • This will open port 1337 on your Ubuntu machine.

      • All the packets sent to this port will be forwarded to 31415 on 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 reverse command.

    adb reverse tcp:<phone_port> tcp:<computer_port>
  • 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:

      adb reverse tcp:9090 tcp:8080
    • What this command does:

      • It opens port 9090 on your phone.

      • Any traffic sent to 9090 on your phone will be forwarded to 8080 on 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 8080 of your Ubuntu machine.


📌ADB commands

📍Test adb

  • To ensure adb is working, just run:

    $ adb version

📍Using adb

  • Run adb devices to get a full list of devices. If you have multiple devices installed, you can:

    • Specify the active device using the s parameter, for example: adb -s emulator-5554 shell

    • Specify to use a single USB device using the d parameter, for example: adb -d shell

📍adb shell

  • Using adb shell you get dropped into a regular Linux shell on your device. Hit Ctrl+D or type exit to leave the shell.


📌Transferring Files

📍adb push

  • To transfer files between our computer and the device, we use the commands adb push and adb pull.

    adb push <local_file_on_computer> <target_path_on_device>
  • With adb push we 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 push Desktop/test.png /sdcard/Downloads/

📍adb pull

  • Command

    adb pull <file_path_on_device> [<optional_target path_on_the_computer>]
  • With adb pull we can pull files from the device to the computer. For example, to download the entire Download folder from the device, we can use:

    adb pull /sdcard/Downloads

Note that with adb pull we can only access the files we have access to with adb 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.

    adb install <path to .apk>
  • Uninstalls the specified application.

    adb uninstall <package_name>
  • Lists all installed packages, including system packages.

    adb shell pm list packages
  • List only third party packages.

    adb shell pm list packages -3
  • Clear the application data without removing the actual application

    adb shell pm clear <package_name>
  • List information such as activities and permissions of a package.

    adb shell dumpsys package <package_name>
  • Starts the activity of the specified package.

    adb shell am start <package_name>/<activity_name>
  • 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

    adb backup

📌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

    adb logcat 
  • Change the log format - for example, using brief to get a more condensed version of the log.

    adb logcat -v <log_format>
  • 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:

    adb logcat "MainActivity:V *:S"
    • Filter format:

      • MainActivity:V ensures that logs from the tag MainActivity with severity of Verbose and above are logged

      • :S Ensures 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