Command Line Power: 10 Android Commands to Boost Your Workflow

Ken Ruiz Inoue
Deuk
Published in
5 min readFeb 13, 2024

--

Introduction

Hello Android learner! Today, I’m sharing ten powerful commands to streamline your development process.

Why opt for the command prompt over the Android Studio GUI, you ask? The answer lies in the unparalleled efficiency of command-line operations. Working directly with command lines bypasses the overhead of UI state management for tasks such as cleaning and building your APK. This means no more time spent navigating and clicking through the UI for routine actions. Furthermore, certain tasks, like exporting logcat to a text file directly in the project’s root, are cumbersome or outright impossible to achieve through the GUI.

Sure, UI-based tools might be fine for occasional actions, but when you’re deep in development, every second counts. Over time, these seconds add up to minutes, then hours, and eventually, days saved by leveraging command-line tools.

So, without further delay, open your favorite command line app, and let’s dive into the ten essential Android commands that promise to streamline your development workflow.

The Commands

1. Compiling and Building the App with Gradle

Command:

  • ./gradlew assembleDebug (macOS/Linux)
  • gradlew.bat assembleDebug (Windows)

Purpose: Build your debug app.

2. Cleaning the Project

Command:

  • ./gradlew clean (macOS/Linux)
  • gradlew.bat clean (Windows)

Purpose: Clean your project by removing the build directory, which is often necessary to resolve issues caused by stale data.

3. Viewing Connected Devices and Emulators

Command: adb devices

Purpose: List all Android devices and emulators that are currently connected to your computer.

Notes: Before running this command, ensure that the adb (Android Debug Bridge) command-line tool is properly set up and included in your system's PATH. adb is a versatile tool that allows you to communicate with an instance of an emulator or connected Android device. It is part of the Android SDK Platform-Tools package, which is typically located in the platform-tools folder within the Android SDK directory.

4. Installing Apps

Command: adb install <path/to/your/app.apk>

<path/to/your/app.apk>: The path where your APK file is located on your development machine.

Purpose: Install your Android app on a connected device or emulator. This command is crucial for testing the application on real devices or emulators.

Example Usage: To install an app located at ./app/build/outputs/apk/debug/app-debug.apk on a connected device or running emulator, you would use:

adb install ./app/build/outputs/apk/debug/app-debug.apk

Notes:

  • Device Connection: Before running the install command, ensure that your Android device or emulator is connected and recognized by your development machine. You can verify this by executing adb devices, which lists the devices and emulators currently connected.
  • Enable USB Debugging: For physical devices, ensure USB debugging is enabled on the device. This setting can be found under Developer options in the device's system settings.
  • Installation Options: The adb install command supports various options to control the installation behavior, such as:
  • r: Reinstalls an existing app, keeping its data.
  • t: Allows the installation of apps with test-only development permissions.
  • d: Allows version code downgrade (app downgrade) during installation.
  • g: Grants all runtime permissions listed in the app manifest.

Example Usage: To re-install an existing app located at ./app/build/outputs/apk/debug/app-debug.apk:

adb -r install ./app/build/outputs/apk/debug/app-debug.apk

5. Consulting Emulators

Command: emulator -list-avds

Purpose: Lists all Android Virtual Devices (AVDs) available on your development machine. This command is useful for identifying the emulators you can launch to test your application.

Output Example: The command will output the names of all available AVDs. For example:

Pixel_4_API_30
Nexus_5X_API_29

This output indicates that there are two AVDs available: one emulating a Pixel 4 running API level 30 and another emulating a Nexus 5X running API level 29.

Notes: Before using this command, ensure that the emulator command-line tool is included in your system's PATH. This tool is part of the Android SDK typically located in the emulator folder within the SDK directory.

6. Launching the Android Emulator

Command: emulator -avd avd_name

Purpose: Start a specific Android Virtual Device (AVD).

7. Pushing Files to Device

Command: adb push <local/path/to/file> <remote/path>

  • <local/path/to/file>: The path on your local machine where the file you want to push to the device is located.
  • <remote/path>: The path on the Android device where the file will be stored.

Purpose: Transfer files from your computer to a connected Android device. This is useful for testing or file operations.

Example: To push a file named example.txt from your local machine to the /sdcard/Download/ directory on the connected Android device, you would use:

adb push example.txt /sdcard/Download/example.txt

This command will copy example.txt from your local machine to the Download folder on the device's SD card.

8. Pulling Files from Device

Command: adb pull <remote/path/to/file> <local/path>

  • <remote/path/to/file>: The path on the Android device where the file or directory you want to copy is located.
  • <local/path>: The path on your local machine where you want to store the copied file or directory. If you do not specify a path, it defaults to the current directory.

Purpose: Copy files from your connected device to your computer, often used for retrieving logs or data files.

Example: To copy a file named log.txt from the /sdcard/Download/ directory on your device to the current directory on your local machine, you would use:

adb pull /sdcard/Download/log.txt .

This command retrieves log.txt from the Download folder on the device's SD card and places it in the current directory on your local machine.

9. Generating a Logcat File

Command: adb logcat -d > logcat.txt

Purpose: Capture the logcat output to a file for analysis or sharing. This is particularly useful for debugging or sending logs to support teams.

Note: This command saves the logcat output to logcat.txt in the current directory. The d option dumps the current logcat buffer.

10. Running Unit Tests

Command:

  • ./gradlew test (macOS/Linux)
  • gradlew.bat test (Windows)

Purpose: Run unit tests for your Android application from the command line. This is crucial for automated testing or continuous integration environments.

Conclusion

And there you have it — the ten android commands that can significantly enhance your Android development workflow. I trust these commands will serve as valuable additions to your development toolkit.

🚀 Elevate Your Android Skills: Hungry for more Android wisdom? Delve into our comprehensive Android Career Guide. It’s your roadmap to mastering Android development, packed with insights, tips, and strategies to propel your career forward.

👏 Spread the Love: Found this guide helpful? A clap (or more!) goes a long way in supporting my work and fueling more content like this. Follow me for updates, and never miss out on new tips, tricks, and guides.

🙏 Thank You: Your time and eagerness to learn are greatly appreciated. Here’s to many more coding adventures together. Happy coding, and I look forward to our next encounter!

Deuk Services: Your Gateway to Leading Android Innovation

Are you looking to boost your business with top-tier Android solutions?Partner with Deuk services and take your projects to unparalleled heights.

--

--