Blog Infos
Author
Published
Topics
, , , ,
Published
Photo by Masakaze Kawakami on Unsplash


In my previous post
, I covered everything you need to know about ADB (Android Debug Bridge) — from what it is to how it works. If you’re new to ADB or curious to dive deeper into the basics, feel free to check it out by clicking here.

But for those of you already familiar with ADB and ready to level up your Android development workflow, this post is for you. Today, we’ll explore 7 powerful ADB commands that often fly under the radar, yet they can seriously boost your productivity and make debugging a breeze. Whether you’re installing apps, capturing logs, or pushing files, these hidden gems will make you feel like an Android command-line ninja 😛

Let’s dive in!

1. ADB Screen Recording Command

ADB’s screen recording feature is an incredibly useful tool for developers, especially when you need to create video demonstrations, app walkthroughs, or capture bugs that are hard to describe in screenshots. With a simple command from the terminal, you can start recording your Android device’s screen, save the video, and even control the recording parameters like bitrate and duration.

Basic Screen Recording Command

To start recording the screen, use the following command:

adb shell screenrecord /sdcard/screenrecord.mp4

This command will start recording your Android device’s screen and save the file as screenrecord.mp4 in the /sdcard/ directory (the root directory of your device’s internal storage).

Here’s a breakdown of the command:

  • adb shell: This starts a remote shell on your Android device.
  • screenrecord: This is the ADB command that triggers the screen recording.
  • /sdcard/screenrecord.mp4: Specifies the location and name of the output file. You can change the file path and name as needed!
Specifying Recording Parameters

While the basic command works well, ADB also gives you the option to tweak the recording quality by adjusting parameters like bitrate and duration. Here are some additional options you can use:

1. Set a Specific Bitrate

By default, ADB records video at 4 Mbps, but you can increase or decrease this depending on your quality or file size needs.

To change the bitrate, use the --bit-rate option. For example:

adb shell screenrecord --bit-rate 8000000 /sdcard/screenrecord.mp4

In this case, the bitrate is set to 8 Mbps (8,000,000 bits per second), resulting in higher quality and a larger file size. A lower bitrate will reduce quality but decrease file size, which is useful if you’re recording for a long time or trying to save space.

2. Set a Time Limit for Recording

By default, screen recordings have a maximum duration of 180 seconds (3 minutes). You can adjust this with the --time-limit option to specify a custom recording time.

For example, to record for 60 seconds, use:

adb shell screenrecord --time-limit 60 /sdcard/screenrecord.mp4

This command will automatically stop recording after 60 seconds.

3. Record with Higher Resolution

To record at a higher resolution or limit the output size to match your device’s display, you can use the --size option. The format is width x height, where you specify the exact resolution.

For instance:

adb shell screenrecord --size 1280x720 /sdcard/screenrecord.mp4

This will record at 1280×720 pixels, ensuring HD quality. Make sure to match the resolution to your device’s screen ratio for the best results.

Stopping the Recording

If you want to manually stop the recording before the time limit, simply press Ctrl + C in the terminal. This will immediately end the recording and save the file.

Retrieving the Video File

Once your recording is finished, you’ll likely want to transfer the video file to your computer. Use the adb pull command to retrieve it:

adb pull /sdcard/screenrecord.mp4

This will copy the video from your Android device to the directory where your terminal is currently located on your computer.

2. Install and Uninstall Apps Using ADB

ADB provides an easy way to install and uninstall apps directly from your terminal, bypassing the need to manually interact with the device’s UI. This is especially useful for developers who frequently test different versions of an app or need to automate these processes during development.

Installing APKs Using ADB

If you have an APK file that you want to install on your device, you can use the adb install command. This eliminates the need to navigate through your device’s file manager or Google Play Store.

Basic Install Command:

adb install /Users/erfan/Documents/apps/myApp.apk

Installing APK Updates:

If the APK is an updated version of an app already installed on the device, you might need to use the -r flag to allow ADB to reinstall it without requiring an uninstall:

adb install -r yourApp.apk

This command forces ADB to replace the existing app with the new version, retaining the app’s data.

Handling Installation Errors:

Sometimes, you might encounter installation errors such as “INSTALL_FAILED_VERSION_DOWNGRADE”. This happens when you attempt to install an APK with a version code lower than the version already on the device. To handle this, uninstall the previous version or force an installation using -d (downgrade) flag:

adb install -r -d yourApp.apk
Uninstalling Apps Using ADB

Uninstalling an app through ADB is also straightforward, especially when you don’t want to interact with the device’s UI. Instead of manually navigating through the device settings, you can run a simple command.

Basic Uninstall Command:

adb uninstall com.example.app

Here’s a breakdown of the command:

  • adb uninstall: This is the command to remove an app from your Android device.
  • com.example.app: This is the package name of the app you want to uninstall. Every Android app has a unique package name (e.g., com.whatsappcom.instagram.android). You can find the package name of any app by checking its AndroidManifest.xml file or using a tool like App Inspector.

Also keep in mind that if you want to unistal system apps, you typically need root access!

Uninstalling but Retaining Data:

If you want to uninstall the app but keep the app’s data (useful when testing reinstallation or upgrades), you can use the -k flag:

adb uninstall -k com.example.app

This command will remove the app’s APK but retain the data in the app’s directory. If you reinstall the app later, the data will still be there.

3. Capture and Save Screenshots Using ADB

Capturing screenshots directly from the terminal is a super handy feature of ADB, especially when you’re testing apps or troubleshooting bugs. It lets you take a screenshot of your Android device’s current screen and then transfer that image to your computer.

Basic Screenshot Command

To capture a screenshot, use the following command:

adb shell screencap /sdcard/screenshot.png

This command tells ADB to capture the screen and save the screenshot as screenshot.png on your device’s internal storage in the /sdcard/ directory.

Retrieving the Screenshot

Once the screenshot is saved on your device, you’ll probably want to transfer it to your computer for review. To do this, use the adb pull command:

adb pull /sdcard/screenshot.png

This command will copy the screenshot from your device to the directory where your terminal is currently open. You can also specify a destination path on your computer:

db pull /sdcard/screenshot.png /Users/yourusername/Desktop/screenshot.png
Additional Options:
  • You can specify a custom path or filename for the screenshot on the device.
  • If you need multiple screenshots during testing, make sure to name each file uniquely (e.g., screenshot1.pngscreenshot2.png) to avoid overwriting.
4. Debugging Wi-Fi Connectivity with ADB

One of ADB’s powerful but lesser-known features is the ability to connect to your Android device wirelessly over Wi-Fi. This is especially helpful if your device is too far to be connected with a cable, or if you want the freedom to move around while debugging.

Step 1: Connect Device via USB

Before you can connect your device to ADB over Wi-Fi, you need to first connect it via a USB cable to configure the Wi-Fi debugging settings.

Once connected via USB, run the following command to enable ADB over Wi-Fi on port 5555:

adb tcpip 5555

This command sets ADB to listen for incoming connections on port 5555 (the default port for ADB over Wi-Fi).

Step 2: Find the Device’s IP Address

To connect your device over Wi-Fi, you need to know its IP address. You can either check the IP in your device’s Wi-Fi settings or run this command from the terminal:

adb shell ip route

Look for the IP address listed under the “default” route. It should be something like 192.168.1.xxx.

Step 3: Connect to the Device Over Wi-Fi

Now that you have the device’s IP address, you can connect to it wirelessly using the adb connect command:

adb connect <device_ip>:5555

Replace <device_ip> with the actual IP address of your Android device. For example:

adb connect 192.168.1.101:5555

if everything is set up correctly, ADB will establish a connection to your device over Wi-Fi, and you’ll be able to run ADB commands as if it were connected via USB.

Use Cases for ADB Over Wi-Fi:
  • Wireless Debugging: No need to keep your device tethered to your computer. This is useful for situations where you’re testing on multiple devices or you simply want more mobility.
  • Remote Device Management: You can debug or test on a device that is physically far away, such as a device in a different room or even across a lab.
  • Quick Fixes Without Reconnecting: If you’re constantly unplugging and reconnecting a device, Wi-Fi debugging saves time and reduces wear on your USB ports.

Job Offers

Job Offers

There are currently no vacancies.

OUR VIDEO RECOMMENDATION

, ,

Cutting-Edge-to-Edge in Android 15: Using Previews and Testing in Jetpack Compose to Manage Insets.

With the advent of Android 15, edge-to-edge design has become the default configuration. Consequently, applications must be capable of accommodating window insets, including the system status bar and navigation bar, as well as supporting drawing…
Watch Video

Cutting-Edge-to-Edge in Android 15: Using Previews and Testing in Jetpack Compose to Manage Insets.

Timo Drick
Lead Android developer
Seven Principles Mobility GmbH

Cutting-Edge-to-Edge in Android 15: Using Previews and Testing in Jetpack Compose to Manage Insets.

Timo Drick
Lead Android develop ...
Seven Principles Mob ...

Cutting-Edge-to-Edge in Android 15: Using Previews and Testing in Jetpack Compose to Manage Insets.

Timo Drick
Lead Android developer
Seven Principles Mobility ...

Jobs

Step 4: Disconnect the Device

When you’re done with wireless debugging, you can disconnect the device by running:

adb disconnect <device_ip>:5555

This command terminates the Wi-Fi connection and stops the ADB session.

Security Note:

Keep in mind that connecting to a device over Wi-Fi does expose it to the local network. Ensure that your Wi-Fi is secured, and avoid using ADB over public or untrusted networks.

5. Grant or Remove Permissions via ADB

ADB is incredibly powerful when it comes to managing app permissions programmatically. Instead of navigating through the Android settings or handling permissions through your app’s UI, you can use ADB to directly grant or revoke permissions for an app. This is particularly useful for automated testing, CI/CD pipelines, and debugging.

Granting Permissions

You can use the pm (package manager) command to grant specific permissions to any app installed on your Android device. Here’s the basic syntax:

adb shell pm grant <package_name> <permission>
  • adb shell pm: This command enters the Android shell and invokes the package manager (pm).
  • grant: Tells ADB to grant a specific permission.
  • <package_name>: Replace this with the package name of the app you want to grant permissions to (e.g., com.example.app).
  • <permission>: Replace this with the specific permission you want to grant (e.g., android.permission.CAMERA).
Example of Granting a Permission:

Let’s say you want to grant the camera permission to your app:

adb shell pm grant com.example.app android.permission.CAMERA

This command tells ADB to grant the camera permission to the app with the package name com.example.app.

Common Permissions to Grant:
  • CAMERA: android.permission.CAMERA
  • Location: android.permission.ACCESS_FINE_LOCATION or android.permission.ACCESS_COARSE_LOCATION
  • Microphone: android.permission.RECORD_AUDIO
  • Storage (Read/Write): android.permission.READ_EXTERNAL_STORAGE or android.permission.WRITE_EXTERNAL_STORAGE

This is extremely useful when automating tests, especially in cases where permissions are required for the app to perform certain tasks. Instead of manually allowing the permission every time the app runs, ADB handles it automatically.

Revoking Permissions

In some cases, you might want to revoke permissions from an app. This could be for security testing, debugging, or to simulate how your app behaves when a permission is missing. The pm command also supports revoking permissions:

adb shell pm revoke <package_name> <permission>
Example of Revoking a Permission:

Let’s say you want to revoke the camera permission from the app:

adb shell pm revoke com.example.app android.permission.CAMERA

Now, the app will no longer have access to the device’s camera, and you can test how it handles this.

6. Network Configuration & Testing with ADB

Network conditions can greatly affect how your app behaves. Testing your app under different network conditions, such as simulating a weak or lost connection, is crucial for understanding its reliability! ADB provides tools to help developers simulate different network environments and debug connectivity issues.

Simulating Network Conditions

You can use ADB commands to control network interfaces on your Android device, allowing you to simulate network disconnections or poor connectivity. This is helpful when testing apps that rely on internet access, like streaming services, social media apps, or apps that sync data in real time.

Disabling Network Connectivity

To disable a specific network interface (e.g., Wi-Fi), use the following command:

adb shell ifconfig wlan0 down

This command disables the wlan0 interface, which controls Wi-Fi on most Android devices. Once Wi-Fi is disabled, your app will have to rely on mobile data (if available), or it will behave as if it’s offline.

Enabling Network Connectivity

To bring the Wi-Fi connection back up, simply run the opposite command:

adb shell ifconfig wlan0 up

This will re-enable the Wi-Fi interface, reconnecting your device to the local network and restoring network access to the app.

Simulating Weak or Unstable Network Conditions

You can also simulate poor network conditions to test how your app behaves when there’s limited bandwidth or high latency. While ADB doesn’t provide direct tools for bandwidth throttling, you can use third-party tools (like a proxy or emulator) to simulate these conditions in tandem with ADB commands.

Use Cases for Network Simulation:
  • Testing Offline Functionality: Some apps need to handle offline scenarios gracefully. For example, apps that sync data to the cloud should queue tasks for later execution when the device is back online.
  • Poor Network Conditions: Test how your app behaves when the network is slow or unreliable, ensuring it doesn’t crash or freeze under these conditions.
  • Testing Data Sync: If your app syncs data in the background, testing how it handles network drops and retries is crucial.
Ping Test for Connectivity

If you’re troubleshooting network connectivity, you can run a ping command to check if your device can reach external networks. Here’s how you can ping Google’s DNS from your Android device:

adb shell ping 8.8.8.8

If the ping fails, your device likely has no network access.

7. Starting and Stopping Services with ADB

One of the powerful features of ADB (Android Debug Bridge) is the ability to directly control services within an Android application. Services are essential components in Android that allow apps to run background tasks without direct interaction from the user, such as syncing data, playing music, or handling network requests.

Using ADB commands, you can start and stop services from the terminal, which is particularly useful for testing and debugging without needing to interact with the device’s UI.

Starting a Service via ADB

To start a service in an Android application programmatically, the am (Activity Manager) command can be used with the startservice argument. The command syntax is:

adb shell am startservice <service_name>
  • adb shell: Accesses the Android device’s shell.
  • am startservice: The command used to start a specific service.
  • <service_name>: This should include the full package name of the app and the class name of the service you wish to start.
Example: Starting a Service

Assume you want to start a data synchronization service called SyncService in an app with the package name com.example.app. The command would look like this:

adb shell am startservice com.example.app/.SyncService

This command will start the SyncService in the background, allowing it to perform the intended operations (e.g., syncing data or handling long-running tasks) without the app’s UI needing to be open.

Passing Parameters to Services

If your service requires additional parameters (such as an intent or data), you can pass these arguments when starting the service. For example, you can pass an extra parameter with a key-value pair using --es for a string extra or --ei for an integer extra:

adb shell am startservice -n com.example.app/.SyncService --es "syncType" "full"

In this example:

  • -n specifies the service component.
  • --es "syncType" "full" passes a string parameter named syncType with the value full.

This is useful for services that behave differently depending on the input provided, such as starting different types of sync processes or setting up service behaviors.

Stopping a Service via ADB

Just as you can start a service, you can also stop it using the stopservice command:

adb shell am stopservice com.example.app/.SyncService

This command will signal the Android system to stop the specified service, which is particularly useful for controlling background processes during testing or debugging.

This will stop the background operation of the SyncService, freeing up resources on the device.

Use Cases for Starting and Stopping Services
  • Automated Testing: Use these ADB commands to test how your app behaves when a service is manually started or stopped. This is particularly useful for services that perform tasks such as data syncing or background notifications.
  • Performance Testing: Evaluate how the app manages resources by controlling services manually and monitoring performance, such as battery consumption or memory usage.
  • Debugging: Start and stop specific services during debugging sessions to pinpoint issues or validate that services behave correctly under different conditions.
  • Background Task Control: When working on apps that rely heavily on background tasks, such as media playback or network data handling, you can simulate real-world scenarios where these services start and stop based on user interaction or other triggers.

Using ADB to start and stop services allows for greater control over background operations within an Android app. This can be particularly advantageous for developers and testers, enabling fine-tuned testing and debugging without the need to manually interact with the app’s UI. Whether you’re triggering a sync operation or controlling media playback services, ADB provides a streamlined, efficient way to manage these essential components.

Conclusion: Power Up Your Android Workflow with ADB

ADB is a true Swiss Army knife for Android developers and power users alike. Whether you’re starting or stopping services, installing APKs, or recording your device’s screen straight from the terminal, ADB provides unparalleled control and efficiency. Mastering these commands unlocks a new level of productivity, allowing you to seamlessly manage, test, and debug your apps.

By integrating ADB into your workflow, you’re not just saving time — you’re gaining the ability to fine-tune every aspect of your app’s performance and behavior, from development to production. So go ahead, dive deeper into ADB, experiment with these powerful commands, and take full control of your Android development process.

Stay tuned for more advanced tips and tricks as we continue to explore the limitless potential of ADB in future posts 🙂

This article is previously published on proandroiddev.com

YOU MAY BE INTERESTED IN

YOU MAY BE INTERESTED IN

blog
It’s one of the common UX across apps to provide swipe to dismiss so…
READ MORE
blog
In this part of our series on introducing Jetpack Compose into an existing project,…
READ MORE
blog
In the world of Jetpack Compose, where designing reusable and customizable UI components is…
READ MORE
blog

How to animate BottomSheet content using Jetpack Compose

Early this year I started a new pet project for listening to random radio…
READ MORE
Menu