Developing Flutter with Android Emulators on WSL2 — Windows 10

Josh Kautz
6 min readMay 4, 2021

--

Welcome! If you haven’t yet installed Flutter on WSL2, be sure to check out the previous article that steps through that process…

… Congratulations! Now you’re ready to get developing with Flutter on WSL2 for Windows 10. This time around, we will be running some Bash commands on WSL2, as well as some PowerShell commands on Windows. We are doing this because Windows 10 doesn’t easily provide a graphical interface for apps running on WSL2, so we can’t use an Android Emulator running on WSL2. Instead, we will use an Android Emulator running on Windows.

Install Android Command Line Tools on Windows

Download Android Command Line Tools for Windows

In PowerShell, create a Downloads directory if it doesn’t already exist, and navigate to it.

md $HOME/Downloads -ErrorAction Ignore;
cd $HOME/Downloads;

Download the latest Android Command Line Tools for Windows. (Right-click the download button and select “Copy link address” to copy the address of the archive.)

wget -OutFile commandlinetools.zip https://dl.google.com/android/repository/commandlinetools-win-7583922_latest.zip;

Create an Android directory if it doesn’t already exist, and move the contents of the compressed Android Command Line Tools bundle into it. Your new tools should reside in theC:\Android\cmdline-tools\tools\bin directory.

Expand-Archive commandlinetools.zip -DestinationPath $HOME/Android/cmdline-tools;
Rename-Item $HOME/Android/cmdline-tools/cmdline-tools $HOME/Android/cmdline-tools/tools;

Add Android Command Line Tools to Environment Variable

Add the path of these new Android Command Line Tools to the System’s Path Environment Variable. This will allow us to easily use tools like sdkmanagerfrom PowerShell. To do this, press ⊞ Winkey, type env, and click the option for “Edit the system environment variables.”

In the “Advanced” tab, click the “Environment Variables…” button.

In the list of user variables, select the entry for “Path”, and click the “Edit…” button. Alternatively, double-click the entry to edit it.

Add a the path value to the Path variable, which will be C:\Android\cmdline-tools\tools\bin.

Create an Android Emulator on Windows

Install Android SDK packages

Using the freshly installed Android Command Line Tools, we will use sdkmanager to install the Android SDK packages required for developing with an Android Emulator. You may need to open a new PowerShell session to be able to use the sdkmanager command.

sdkmanager --install "system-images;android-31;google_apis;x86_64" "platform-tools" "platforms;android-31" "build-tools;31.0.0" "cmdline-tools;latest"

At this point, we will want to update our System’s Path Environment Variable again because we just downloaded tools like Android Debug Bridge (adb) and Emulator — both of which we’ll be using — so we want easy access to them from PowerShell. To do this, add the values of the new paths to our environment variable. The two new values are C:\Android\platform-tools and C:\Android\emulator.

Create Android Virtual Device

We are going to use avdmanager — a command line tool that allows us to create and manage Android Virtual Devices (AVDs). Since we currently don’t have any AVDs, create one a new one.

avdmanager --verbose create avd --name "Flutter" --package "system-images;android-31;google_apis;x86_64" --device "pixel_5" --tag "google_apis" --abi "x86_64"

Now the fun starts! We can actually run this AVD using the Emulator tool that we installed previously.

emulator -avd Flutter
A lot of output, but that’s just fine.
Android Virtual Device running on Windows!

Run Android Debug Bridge

We’re making tremendous progress, but currently we can’t build our Flutter app from WSL2 to this AVD. We need to somehow expose this device to WSL2. So, we are going to make the AVD available from WSL2 by using Android Debug Bridge (adb).

In a new PowerShell terminal, kill the Android Debug Bridge server if it is running.

adb kill-server

Next, ensure there is an Android Debug Bridge server running and listening on all network interfaces.

adb -a server nodaemon

In a new PowerShell terminal, check the list of connected devices on Android Debug Bridge.

adb devices

Look — We see our new device listed!

Access Android Virtual Device from WSL2

We’re on the home stretch! We are going to be running commands back in the WSL2 terminal, so these are Bash shell commands now. Now we just need to create a new Flutter app and run it on our new Android Virtual Device running on Windows. Right now, if we run the adb devices command on WSL2, we don’t see anything devices listed.

There’s a list, but it’s empty.

That’s because there are no devices connect to this system. Our AVD is connected to the Android Debug Bridge server running on the Windows system, which is a remote system. In order to view the AVD running on Windows, we need to specify the socket.

We’re going to need the IP Address of the Windows system, which we can quickly get by running cat /etc/resolv.conf | grep nameserver | cut -d' ' -f2. However, we want the WSL2 system to remember this address, so we will save the value as an environment variable, so we will create a new Bash shell variable for it in $HOME/.bashrc.

echo -e "export ADB_SERVER_SOCKET=tcp:\$(cat /etc/resolv.conf | grep nameserver | cut -d' ' -f2):5037" >> $HOME/.bashrc;

Execute the source Bash shell command to update the PATH variable in your working session.

source $HOME/.bashrc

Now when we run echo $ADB_SERVER_SOCKET we see something like tcp:172.31.0.1:5037. That’s going to be the new default socket that Android Debug Bridge in WSL2 will listen to. So now if you try running…

adb devices

Voilà! We can see our Android Virtual Device running on Windows from WSL2. What’s more, Flutter can see it too - we now have a connected device!

flutter doctor

Run a Flutter app on the Android Virtual Device

We’re still in WSL2. Create a Projects directory if it doesn’t already exist, and navigate to it.

mkdir -p $HOME/Projects && cd "$_"

Use the flutter create command to create a new project.

flutter create myapp;
cd myapp;

Run your new app!

flutter run

Nice work! We’re now using WSL2 to develop Flutter apps! Next, we’ll explore how to use our physical devices for development and debugging, instead of having to rely on our Android Virtual Devices. But, like usual, that deserves its own post…

It was recently announced that Windows 11 does allow for running Linux GUI apps on WSL2, which means the possibility of running Android Virtual Devices directly on WSL2 itself instead of Windows. We’ll explore that in the future…

--

--