How to Run Android Emulator for Development Without Android Studio?

Usually, we will install Android Studio to run our code in its Android Emulator. This emulator is usually called AVD (Android Virtual Device) Emulator. But, sometimes when you don’t need Android Studio for the development process but still need the AVD Emulator, it will become a hassle to install a whole Android Studio to your PC.

So I will show you how we will install and run AVD Emulator without installing any Android Studio. Here’s the step to install AVD Emulator to your PC without Android Studio.

Get Tools Package

    tools package is part of Android SDK, a group of packages that are needed for Android development. tools package is used for managing other packages to create AVDs. Here’s the step for getting the tools package:
    1. Go to the Android Studio download page.
    2. Scroll down until you find a section for downloading command tools.
3. Download the appropriate file for your system by clicking one of the links for the zip file.

4. Extract the zip anywhere you want and you will get a folder named cmdline-tools.

    And now you’ve already got the tools package on your OS.

Download Essential Packages

    Now, we will download platform-tools and emulator packages. platform-tools has some tools to communicate with Android devices when you plug them into your computer. emulator is the Android emulator. We will use sdkmanager or sdkmanager.bat which are placed at cmdline-tools/bin to install them.
    First, try running sdkmanager or sdkmanager.bat with this command:

  	./sdkmanager --list
  	
    or

  	./sdkmanager.bat --list
  	
    If you get an error about the Java version that is not compatible, you must install the latest Java to your system. You can get the file needed for installation at this link.
    And if you don’t have any Java error or you’ve already resolved it, try running the sdkmanager with the previous command once again. You will get an error like this:
    This error tells you that they can’t determine the SDK root. The simple solution is to move all the files inside the directory cmdline-tools/ to the directory cmdline-tools/latest/.
    After you’ve moved the files, run it once again. You will get a list of all packages that are available for installation.

Now let’s install the platform-tools and emulator packages with these commands:


    	./sdkmanager platform-tools emulator
    
    or

    	./sdkmanager.bat platform-tools emulator
    
    You will be prompted about android-sdk-licenses, you can read it first or hit type y right away.
    And those packages are installed.

Set The Environment Variables

    You need to set some environment variables. The first one is variables that contain the path to your SDK, if you confused about what is the path is, this is the path to the directory that contains cmdline-tools directory. You can set the name and value of the environment variables like this:

    	ANDROID_SDK_ROOT=Path to your SDK folder
        
        ANDROID_HOME=Same as ANDROID_SDK_ROOT (Already deprecated, but some programs still using it to locate your SDK)
    
    You need to add these directories to PATH variables, so the binary files inside them will be accessible from everywhere:
    – Path to emulator directory, usually it’s at the same level as cmdline-tools directory.
    – Path to paltform-tools directory, usually it’s at the same level as cmdline-tools directory.
    – Path to cmdline-tools/latest/bin.
    Well, how to set these environment variables will be really different considering the operating system that you use and there are already so many tutorials about how to do that, so do your own research for this.

Download the platform-specific packages

    You need some other packages which are platforms, system-images, and build-tools. platforms are required to compile your app for a specified API level. system-images are Android images that are used by the emulator. build-tools are the package that is needed to build your Android apps.
    You can see the version of these packages with sdkmanager --list the command before. To download a package, you can use the command:

    	sdkmanager package_name
    
    For example, I will use API Level 32. So my commands will be:

    	sdkmanager "platforms;android-32"
        
        sdkmanager "build-tools;32.0.0"
        
        sdkmanager "system-images;android-32;google_apis;x86_64"
    

Create an AVD device

    We will use the avdmanager command to create an AVD device. This command is usually placed in cmdline-tools/latest/bin directory, so if you’ve already config the environment variable correctly, you can just call the command from everywhere.
    Here’s an example of a command about how to create an
AVD device:

    	avdmanager create avd --name android32 --package "system-images;android-32;google_apis;x86_64"
    

You will be asked if you want to change some default configurations. You can change it later in config.ini file that is located in the AVD directory, so for now we will just continue the installation.

Run the Android Emulator

    Now, let’s run the emulator with these commands:

    	emulator -avd android32 #use your avd name
    
    or

    	emulator @android32 # user your AVD name
    
    And we can see our AVD emulator running, yay!