DEV Community

Cover image for Ditch the GUI: How to Master Android Emulators with Just the CLI Like a True Terminal Nerd
Sandheep Kumar Patro
Sandheep Kumar Patro

Posted on

Ditch the GUI: How to Master Android Emulators with Just the CLI Like a True Terminal Nerd

Setting Up an Android Emulator Using sdkmanager CLI

Introduction

As a developer, you have multiple ways to set up and run Android emulators, and while Android Studio offers a friendly graphical interface (GUI) to manage emulators, some of us prefer the raw power and flexibility that comes with the command line interface (CLI). That’s where tools like sdkmanager and avdmanager come in handy.

Sure, you could open the emulator through Android Studio with just a few clicks, but where’s the fun in that? As a terminal nerd, I prefer the CLI route—it offers greater control, automation capabilities, and frankly, it just feels more satisfying. Running an emulator via the command line makes me feel more in tune with the system, and it’s perfect for integrating into scripts and automation workflows.

In this guide, I’ll show you how to set up and run an Android emulator using the sdkmanager CLI, bypassing Android Studio altogether. Whether you’re like me and love the terminal or simply need a more streamlined setup for continuous integration, this method is for you.


Prerequisites

Before you begin, ensure you have the following:

  • A Linux, macOS, or Windows system
  • Familiarity with using the command line
  • Java Development Kit (JDK) installed on your system

What Are sdkmanager and avdmanager?

Before diving into the steps, let’s clarify the tools we’ll be using:

sdkmanager

sdkmanager is a command-line tool provided by Google that allows you to download, update, and manage Android SDK components. It is part of the Android SDK Command-Line Tools and can install platform tools, system images, build tools, and more. It’s especially useful in scenarios where you need precise control over the SDK version, automate setup scripts, or work in headless environments (like CI servers) without Android Studio's GUI.

avdmanager

avdmanager is another command-line tool included in the Android SDK. Its primary purpose is to manage Android Virtual Devices (AVDs), which are emulator configurations simulating a particular device, including its Android version, hardware, and software settings. With avdmanager, you can create, delete, and manage AVDs, allowing you to run multiple Android emulator configurations for testing.


Step 1: Installing SDK Manager CLI Tools

1.1 Create a Directory for Android SDK

Start by creating a folder to store the Android SDK. On Linux and Unix-based systems, this directory is typically located under $HOME/Android/sdk. On Windows, it is stored in AppData/local/Android/sdk.

For Linux/Unix systems, run the following commands:



cd ~
mkdir -p Android/sdk


Enter fullscreen mode Exit fullscreen mode

On Windows, create the folder manually in the desired location or use similar commands if using a terminal like Git Bash.

1.2 Download SDK Manager Command-Line Tools

There are two ways to download the SDK manager CLI tools:

  • Using wget Command: Replace {os} with your operating system (e.g., linux, mac, win) and {version} with the appropriate version at the time of download (can be obtained from the Command line tools only section of Android Developer Site):


cd ~/Downloads
wget https://dl.google.com/android/repository/commandlinetools-{os}-{version}.zip


Enter fullscreen mode Exit fullscreen mode
  • Manual Download: If you prefer to download the SDK manager via your browser, visit the official Android Developer Site and navigate to the "Command line tools only" section. Download the .zip file appropriate for your operating system.

1.3 Extract the Command-Line Tools

Extract the downloaded .zip file into the previously created Android/sdk directory:



unzip commandlinetools-{os}-{version}.zip -d $HOME/Android/sdk


Enter fullscreen mode Exit fullscreen mode

The extracted folder should include files like bin/, lib/, and NOTICE.txt.

1.4 Organize the SDK Files

To keep things organized, create a folder called latest inside the cmdline-tools folder and move all the extracted files into this new folder:



mkdir $HOME/Android/sdk/cmdline-tools/latest
mv $HOME/Android/sdk/cmdline-tools/* $HOME/Android/sdk/cmdline-tools/latest/


Enter fullscreen mode Exit fullscreen mode

This results in the following structure:



cmdline-tools/
    latest/
        bin/
        lib/
        NOTICE.txt
        source.properties


Enter fullscreen mode Exit fullscreen mode

Step 2: Configuring the System Path for SDK Tools

2.1 Adding SDK Tools to the System Path

To use sdkmanager globally from the terminal, we need to add its path to the system’s PATH variable. Depending on your shell, follow these instructions:

  • For Bash: Open your .bashrc file using the command below:


  nano ~/.bashrc


Enter fullscreen mode Exit fullscreen mode
  • For Zsh: Edit the .zshrc file:


  nano ~/.zshrc


Enter fullscreen mode Exit fullscreen mode
  • For Fish: Edit the config.fish file:


  nano ~/.config/fish/config.fish


Enter fullscreen mode Exit fullscreen mode

Next, add the following lines to the end of the file to set up the ANDROID_HOME environment variable and update the PATH:



# Bash/Zsh
export ANDROID_HOME=$HOME/Android/sdk
export PATH=$PATH:$ANDROID_HOME/cmdline-tools/latest/bin


Enter fullscreen mode Exit fullscreen mode

For Fish shell:



set -x ANDROID_HOME $HOME/Android/sdk
set -x PATH $PATH $ANDROID_HOME/cmdline-tools/latest/bin


Enter fullscreen mode Exit fullscreen mode

2.2 Verifying SDK Installation

Once you've edited and saved the file, reload the shell configuration to apply the changes:



source ~/.bashrc  # for Bash
source ~/.zshrc   # for Zsh
source ~/.config/fish/config.fish  # for Fish


Enter fullscreen mode Exit fullscreen mode

To verify that sdkmanager is installed and accessible, run the following command:



sdkmanager --version


Enter fullscreen mode Exit fullscreen mode

You should see the version number of sdkmanager, confirming that the tool is successfully installed.


Step 3: Accepting Android SDK Licenses

Before you can install additional Android components, you must accept the licenses associated with the Android SDK. Run the following command to accept all the licenses:



sdkmanager --licenses


Enter fullscreen mode Exit fullscreen mode

This will display a list of licenses. Press y to accept each one. Once you've accepted all the licenses, you're ready to install additional SDK packages.


Step 4: Installing the Android Emulator

Now that the SDK manager is configured, it's time to install the necessary packages to set up the Android emulator.

4.1 List Available Packages

To see a list of available packages that you can install via sdkmanager, use the following command:



sdkmanager --list


Enter fullscreen mode Exit fullscreen mode

This command will output a list of installable packages, including build tools, system images, and platform tools.

4.2 Install Necessary Packages

For running an emulator, you'll need the following packages:

  • platform-tools: Contains the Android Debug Bridge (ADB) and fastboot tools, essential for communicating with connected Android devices and emulators.
  • build-tools: A set of tools required to build Android applications (e.g., build-tools;35.0.0).
  • emulator: The Android Emulator binary, which allows you to run the AVD.
  • sources;android-35: Source code for a specific Android platform (version 35).
  • platforms;android-35: Platform image for Android version 35, required by the emulator to simulate that Android version.

Install these packages using the following command:



sdkmanager "platform-tools" "build-tools;35.0.0" "emulator" "sources;android-35" "platforms;android-35"


Enter fullscreen mode Exit fullscreen mode

This command will install all these packages into the Android/sdk directory.

Note: Depending on your system, there might be a package called tools. While it is not required for setting up or using the emulator, it may be necessary for other tasks. You can add it to your system path if needed.


Step 5: Setting Up and Running the Emulator

In this step, we will use the avdmanager tool to create an Android Virtual Device (AVD), which will act as the Android emulator. Then, we’ll use the emulator command to launch the AVD.

5.1 Create an Android Virtual Device (AVD)

To create an Android emulator, you need to use the avdmanager tool. The following command creates a new AVD:



avdmanager create avd -n Android35 -k "system-images;android-35;google_apis;x86_64"


Enter fullscreen mode Exit fullscreen mode

Let’s break down what this command does:

  • avdmanager create avd: This tells avdmanager to create a new Android Virtual Device.
  • -n Android35: This names the new virtual device Android35. You can change this to any name that suits your project.
  • -k "system-images;android-35;google_apis;x86_64": This specifies the system image to use for the AVD. In this case:
    • system-images is the image type.
    • android-35 specifies Android API level 35.
    • google_apis means it includes Google APIs.
    • x86_64 indicates that this image is for 64-bit x86 architecture, which provides better performance on most desktop machines.

Once this command is executed, a new AVD will be created that simulates a device running Android 35 with Google APIs, which is ideal for testing apps that integrate with Google services.

5.2 Running the Emulator

To launch the emulator for the newly created AVD, use the emulator command:



emulator -avd Android35


Enter fullscreen mode Exit fullscreen mode

Here’s what happens when you run this command:

  • emulator: This launches the Android Emulator.
  • -avd Android35: This tells the emulator to run the AVD named Android35, which we created in the previous step.

This command will open a window simulating an Android device. You can interact with the virtual device just like you would with a physical device, testing apps, checking layouts, and debugging.

5.3 Checking Connected Devices

Once the emulator is running, you can check if it's recognized by ADB (Android Debug Bridge) using the following command:



adb devices


Enter fullscreen mode Exit fullscreen mode

This command lists all the devices connected to your system, including any running Android emulators. You should see something like:



List of devices attached
emulator-5554   device


Enter fullscreen mode Exit fullscreen mode

If the emulator is correctly listed, it means it's running and ready for use in your development environment.


Conclusion

By following this guide, you've successfully installed and configured the Android SDK tools using the sdkmanager and avdmanager CLI tools. You can now create and run Android emulators without the need for Android Studio, providing a lightweight, efficient way to test and debug your apps.

Note: In some cases, additional tools may need to be added to the path, such as tools or other components depending on your development needs. For now, this setup should be sufficient for most emulator-related tasks.

This CLI setup is ideal for automating emulator usage in CI/CD pipelines or for developers who prefer working with the command line. From here, you can start developing and testing Android applications, integrate the emulator with Android Studio, or use the emulator in a continuous testing environment.

For further reading and advanced configurations, check out the official Android developer documentation.


Top comments (0)