Start and Stop Android Emulator with Powershell

In order to programmatically start and stop an Android Emulator from Powershell, have a look at the Powershell script I’ve created below. This post assumes you already have the Android SDK, AVD and emulators set up on your machine. It is also assumed that you will only have one emulator instance at a time, to keep things simple.

# Variables
$emulatorExe = ".\emulator.exe"
$androidEmulator= "nexus_emulator"
$partitionSize = "512"
$emulatorPath = "C:\Program Files (x86)\Android\android-sdk\emulator\emulator.exe"

# START EMULATOR
Write-Host "Starting the Emulator..."
Start-Process -FilePath $emulatorPath -ArgumentList "-avd $androidEmulator -partition-size $partitionSize"

# DO STUFF HERE
Start-Sleep -s 10

# STOP EMULATOR
Write-Host "Stopping the Emulator..."
cmd /c "adb -e emu kill"

What this PS1 script will do is start up the emulator with the given variables:

  • $emulatorExe – The location of emulator.exe on your machine
  • $androidEmulator – Name of the emulator you want to open
  • $partitionSize – Internal storage allocated to your emulator
  • $emulatorPath – Path to where your emulator is located

It will then start up another process (outside of the current script process), for running the emulator with the provided variables:

Start-Process -FilePath $emulatorPath -ArgumentList "-avd $androidEmulator -partition-size $partitionSize"

The script will then sleep for 10 seconds before stopping the emulator. (Feel free to modify as you see fit, or perform actions between starting/stopping the emulator. This is only used as an example to show starting and stopping an emulator through powershell.

Afterwards, we will stop the emulator using the following command:

cmd /c "adb -e emu kill"

Programmatically starting/stopping an Android emulator is especially useful for test automation (UI Tests), when ran on build servers. You may want to look into setting this up in your project build script.

Connected Android Device not detected in IDE

If you’re developing an Android app using a connected Android device, Visual Studio or other IDE’s might not be able to detect it right away, especially if it’s a newly set-up device.

By default, Developer Mode is off for Android Devices. To turn this on, you must perform the following steps on the device:

  • Head over to Settings -> Systems -> About Phone. Might be a little different per device, but try and get to an About Page on your device, where the “Build Number” option will appear as a setting. (Try using the search bar as well).
  • Once you’re here, tap the “Build Number” section around 7 times and you should get a pop-up toast message saying Developer Mode is activated.
  • You can then go back up a page, to System, and there should be a new “Developer Options” setting.
  • Tap on Developer Options, and scroll down til you see “USB Debugging”. Turn this feature on. You may need to disconnect and reconnect your device.
  • Now check in Visual Studio or your IDE of choice, and it should appear as an option to run your app.