Success is like a party.

Over the holidays I had the time to look at my development environment and decided it was time to make the jump over to using WSL2.

WSL2 really did give me the performance improvements promised but I ran into a problem with doing frontend development with flutter (and I assume will affect react native too) so I thought I’d help others that would like to make this leap.

Environment Setup

Since there are lots of articles out about the different pieces I’m not going to spend the time duplicating the details but just give you a run down of my development environment.

Windows 10 Professional

WSL 2 (Ubuntu 19.04)

VS Code (1.41.1)

Android Studio (3.5.3)

Visual Studio Code Remote WSL

I do most of my development using Visual Studio Code so I was pretty excited when I found the extension to access remote WSL. This is an absolute must if you want to use WSL 2 and get the filesystem performance gains.

Android SDK

I wanted to have everything run inside the WSL 2 container so I installed the Android SDK inside Ubuntu. It was also set up in my Windows side but my goal was to not use it if at all possible.

Here is the following I did to set it up. You’ll want to move the exports into your .bash_profile once you get this working.

cd ~

sudo apt-get install unzip zip

wget https://dl.google.com/android/repository/sdk-tools-linux-4333796.zip

unzip sdk-tools-linux-4333796.zip -d Android

rm sdk-tools-linux-4333796.zip

sudo apt-get install -y lib32z1 openjdk-8-jdk

export JAVA_HOME=/usr/lib/jvm/java-8-openjdk-amd64

export PATH=$PATH:$JAVA_HOME/bin

printf "



export JAVA_HOME=/usr/lib/jvm/java-8-openjdk-amd64

export PATH=\$PATH:\$JAVA_HOME/bin" >> ~/.bashrc

cd Android/tools/bin

./sdkmanager --install "platform-tools" "platforms;android-26" "build-tools;26.0.3"

export ANDROID_HOME=~/Android

export PATH=$ANDROID_HOME/tools:$PATH

export PATH=$ANDROID_HOME/tools/bin:$PATH

export PATH=$ANDROID_HOME/platform-tools:$PATH

printf "



export ANDROID_HOME=~/Android

export PATH=\$PATH:\$ANDROID_HOME/tools

export PATH=\$PATH:\$ANDROID_HOME/platform-tools" >> ~/.bashrc

sdkmanager --update

curl -s "https://get.sdkman.io" | bash

source "$HOME/.sdkman/bin/sdkman-init.sh"

sdk install gradle 5.5.1

gradle -v

Flutter

The last piece I needed was to get the flutter linux setup going. Just using the standard linux flutter install was just fine to get me going.

After the install is done make sure to run flutter doctor to see if you have any issues. I had to correct the SDK location and accept some licences but did not need to have Android Studio installed.

In addition make sure that you launch VS Code and use the remote WSL extension to connect to your environment. Once you have connected you’ll need to install the flutter extension along with any other extensions you like to have running, it seems the remote WSL extension sets up vs code in the ubuntu environment separately.

The Android Emulator

Up to this point everything went just great. I was able to have a working flutter environment but got stuck with not being able to compile my app to an emulator or device. I found 2 solutions to this, one if you were ok with using a real device and another if you wanted to use the emulator.

ADB TCPIP

If you are working with a real device for testing this is a really useful way to set things up. It lets you tell the device that the client connections to it are going to come via TCP instead of USB. To set this up you have to just have the device you want to test connected to windows. In you Windows cmd prompt ensure you have devices connected using

adb devices

If you see the device you want connected then follow that with

adb tcpip 5555

Doing this will make the device ready to receive adb commands over wifi. In the WSL side of things you’ll issue the command

adb connect <ip of phone>:5555

If you get a successfully connected you’ll be ready to go. You can validate this by looking at VSCode and notice it now shows a device in the bottom right status bar.

ADB for Emulator

So I’ll caveat this one with even though I have it working it’s very slow to build on the emulator but wanted to add my findings in case someone else has made it run better.

This setup basically makes the Ubuntu adb connect to the adb server running on the windows host. This works but as I said is very slow.

On the windows host you need to make adb monitor a port for incoming connections

adb -a -P 5037 nodaemon server

Once this is done from the Ubuntu side you need to add a new environment variable to set adb to connect to the remote server rather than the local one.

export ADB_SERVER_SOCKET=tcp:192.168.1.97:5037

adb kill-server

As soon as this is done you should be able to run adb devices and see the ones hosted on the windows side of your setup.

One thing to make note of, you can only run the flutter app via the CLI and not using the VSCode debug option. It has something to do with the Observatory URL used for debugging.

Conclusion

The development setup is quite nice in WSL 2. It’s fast and easy to use but like I was saying the adb setup using the emulator is very slow. It can take 30mins for a build to successfully transfer to the emulator. My hope is that as others try to set this up we find a better way to set this connection up to make it a much more seamless process. Ideally WSL 2 would be able to connect to the devices directly but I’m sure that’s a longer term dream.