title: How to make Flutter work on WSL2 using host's emulator?
body: <p>This issue was a headache, so I wish to share my solution. It starts installing requirements for Android SDK (without installing Android Studio), Dart and Flutter, and it finalizes running a Flutter app on Windows host.</p>
  <p>==================</p>
  <h1>on WSL2</h1>
  <p>==================</p>
  <pre><code>$ lsb_release -a
  Distributor ID: Ubuntu
  Description:    Ubuntu 20.04 LTS
  Release:        20.04
  Codename:       focal
  </code></pre>
  <strike>
  <h3>Installing Dart</h3>
  <pre><code>$ sudo apt-get update &amp;&amp; sudo apt-get upgrade
  $ sudo apt-get install apt-transport-https
  $ sudo sh -c 'wget -qO- https://dl-ssl.google.com/linux/linux_signing_key.pub | apt-key add -'
  $ sudo sh -c 'wget -qO- https://storage.googleapis.com/download.dartlang.org/linux/debian/dart_stable.list &gt; /etc/apt/sources.list.d/dart_stable.list'
  $ sudo apt-get update
  $ sudo apt-get install dart
  $ echo &quot;export PATH=\&quot;/usr/lib/dart/bin:$PATH\&quot;&quot; &gt;&gt; ~/.bashrc
  $ source ~/.barshrc 
  $ dart --version
  Dart VM version: 2.8.4 (stable) (Unknown timestamp) on &quot;linux_x64&quot;
  </code></pre>
  </strike>
  <p>===&gt;&gt;&gt; <em><strong>Note</strong>: The Dart SDK is <a href="https://flutter.dev/docs/get-started/install/linux#run-flutter-doctor" rel="noreferrer">bundled with</a> Flutter</em></p>
  <h3>Installing Android SDK</h3>
  <p>Get <a href="https://developer.android.com/studio#command-tools" rel="noreferrer">Command line tools</a> for Android (Linux) only.</p>
  <pre><code>$ sudo apt install -y lib32z1 default-jdk git unzip zip
  $ cd ~/Downloads
  $ unzip commandlinetools-linux-6609375_latest.zip
  $ rm commandlinetools-linux-6609375_latest.zip
  $ mkdir -p Android/cmdline-tools
  $ mv tools/ Android/cmdline-tools/
  $ mv Android/ ~/Programs/
  </code></pre>
  <p>Append the following lines to <code>.bashrc</code> file:</p>
  <pre><code># Android
  export ANDROID_SDK_ROOT=$HOME/Programs/Android
  export PATH=$ANDROID_SDK_ROOT/cmdline-tools/tools:$PATH
  export PATH=$ANDROID_SDK_ROOT/cmdline-tools/tools/bin:$PATH
  export PATH=$ANDROID_SDK_ROOT/cmdline-tools/tools/lib:$PATH
  export PATH=$ANDROID_SDK_ROOT/platform-tools:$PATH
  export PATH=$ANDROID_SDK_ROOT/emulator:$PATH
  
  # Java
  export JAVA_HOME=/usr/lib/jvm/java-11-openjdk-amd64
  export PATH=$JAVA_HOME/bin:$PATH
  </code></pre>
  <p>Continue the installation</p>
  <pre><code>$ cd ~
  $ source .bashrc
  $ sdkmanager --version
      4.0.1
  $ sdkmanager --list
  $ sdkmanager --install &quot;system-images;android-29;google_apis;x86&quot; &quot;platform-tools&quot; &quot;platforms;android-29&quot; &quot;build-tools;29.0.3&quot;
  $ sdkmanager --install &quot;cmdline-tools;latest&quot;
  $ sdkmanager --update
  $ sdkmanager --list | sed -e '/^$/q'
  $ sdkmanager --licenses
  </code></pre>
  <p>Accept all licenses.</p>
  <h3>Installing Flutter</h3>
  <pre><code>$ cd ~/Downloads
  $ wget https://storage.googleapis.com/flutter_infra/releases/stable/linux/flutter_linux_1.17.5-stable.tar.xz
  $ cd ~/Programs
  $ tar xf ~/Downloads/flutter_linux_1.17.5-stable.tar.xz
  $ cd ~
  $ echo &quot;export PATH=$HOME/Programs/flutter/bin:$PATH&quot; &gt;&gt; .bashrc
  $ source .bashrc
  $ flutter --version
  Flutter 1.17.5 • channel stable • https://github.com/flutter/flutter.git
  Framework • revision 8af6b2f038 (11 days ago) • 2020-06-30 12:53:55 -0700
  Engine • revision ee76268252
  Tools • Dart 2.8.4
  $ flutter config --android-sdk $ANDROID_SDK_ROOT
  </code></pre>
  <h3>Creating the emulator</h3>
  <pre><code>$ cd ~
  $ curl -s &quot;https://get.sdkman.io&quot; | bash
  $ source .sdkman/bin/sdkman-init.sh
  $ sdk v
      SDKMAN 5.8.3+506
  $ sdk install gradle 6.5.1
  $ gradle -v
  $ avdmanager list
  $ echo &quot;no&quot; | avdmanager --verbose create avd --force --name &quot;generic_10&quot; --package &quot;system-images;android-29;google_apis;x86&quot; --tag &quot;google_apis&quot; --abi &quot;x86&quot;
  </code></pre>
  <p>Add/Modify these lines in <code>~/.android/avd/generic_10.avd/config.ini</code> file:</p>
  <pre><code>skin.name=1080x1920
  hw.lcd.density=480
  hw.keyboard=yes
  </code></pre>
  <p>Check emulator created:</p>
  <pre><code>$ emulator -list-avds
  </code></pre>
  <p>==================</p>
  <h1>on Windows 10</h1>
  <p>==================</p>
  <pre><code>&gt; Get-ComputerInfo -Property &quot;WindowsProductName&quot;
  Windows 10 Pro
  &gt; Get-ComputerInfo -Property &quot;WindowsVersion&quot;
  2004
  &gt; Get-ComputerInfo -Property &quot;OsBuildNumber&quot;
  19041
  &gt; Get-ComputerInfo -Property &quot;OsArchitecture&quot;
  64-bit
  &gt; Get-NetIPAddress -AddressFamily IPv4 -InterfaceAlias Ethernet|findstr IPAddress
  IPAddress         : 192.168.0.29
  </code></pre>
  <h3>Installing Android SDK</h3>
  <p>Get <a href="https://developer.android.com/studio#command-tools" rel="noreferrer">Command line tools</a> for Android (Windows) only.</p>
  <p>The steps to follow are basically the same as in the <em><strong>Installing Android SDK on WSL2</strong></em> section, you can read <a href="https://cloudreports.net/install-android-sdk-on-windows-10-without-android-studio/" rel="noreferrer">this page</a> for reference if any doubt.</p>
  <h3>Creating the emulator</h3>
  <p>To install <strong>Gradle</strong> in Windows, follow the instructions in <a href="https://gradle.org/install/" rel="noreferrer">this page</a>.
  The rest of the steps are the same as in the <em><strong>Creating the emulator on WSL2</strong></em> section, where <code>~</code> points out your home folder in Windows.</p>
  <h3>Preparing host for listening</h3>
  <p>You can use any port with <code>adb</code> and <code>emulator</code> tools, but it is more simple if they manage it by themselves using their default ports. For <code>adb</code> tool its default port is 5037. <code>IP Helper</code> service uses that port (in my case), so I had to <a href="https://www.howto-connect.com/disable-enable-ip-helper-service-windows-10-iphlpsvc/" rel="noreferrer">stop it</a>.</p>
  <p>Open a terminal and check the status of port 5037:</p>
  <pre><code>&gt; netstat -aon|findstr 5037
  </code></pre>
  <p>Run these commands:</p>
  <pre><code>&gt; adb kill-server
  &gt; adb -a -P 5037 nodaemon server
  </code></pre>
  <p>Open another terminal (don't close the previous one) and run:</p>
  <pre><code>&gt; emulator -avd generic_10
  </code></pre>
  <p>Open another terminal (don't close the previous ones) and run:</p>
  <pre><code>&gt; adb devices
  List of devices attached
  emulator-5554   device
  </code></pre>
  <p>==================</p>
  <h1>on WSL2</h1>
  <p>==================</p>
  <h3>Working with host's <code>adb</code></h3>
  <pre><code>$ echo &quot;export ADB_SERVER_SOCKET=tcp:192.168.0.29:5037&quot; &gt;&gt; ~/.bashrc
  $ source ~/.bashrc
  $ adb devices
  List of devices attached
  emulator-5554   device
  </code></pre>
  <h3>Running our Flutter app</h3>
  <pre><code>$ flutter doctor
  Doctor summary (to see all details, run flutter doctor -v):
  [✓] Flutter (Channel stable, v1.17.5, on Linux, locale C.UTF-8)
  
  [✓] Android toolchain - develop for Android devices (Android SDK version 29.0.3)
  [!] Android Studio (not installed)
  [✓] Connected device (1 available)
  
  ! Doctor found issues in 1 category.
  $ flutter create hello_world
  $ cd hello_world
  $ flutter run   
  </code></pre>
  <p>==================</p>
  <h1>Observations</h1>
  <p>==================</p>
  <ul>
  <li>It takes a little longer to build/run the app for first time</li>
  <li>VSCode (remote) can detect this connected device, but it cannot deploy correctly to emulator, so it's better to run the app on terminal</li>
  <li>You cannot use &quot;<em>hot reload</em>&quot; features with flutter app because the process of deployment/running never finishes, however the package did be installed and running in your emulator, but for any changes in source code, you need re-run the app.</li>
  <li>This operation consumes too much memory (~90%). There's a <a href="https://medium.com/@lewwybogus/how-to-stop-wsl2-from-hogging-all-your-ram-with-docker-d7846b9c5b37" rel="noreferrer">workaround</a> to respect. My configuration is:</li>
  </ul>
  <p>.</p>
  <pre><code>[wsl2]    
  memory=4GB # Limits VM memory in WSL 2 to 4 GB
  swap=0
  </code></pre>
  <p>====================</p>
  <h1>Pages consulted</h1>
  <p>====================</p>
  <ul>
  <li><a href="https://flutter.dev/docs/get-started/install/linux" rel="noreferrer">https://flutter.dev/docs/get-started/install/linux</a></li>
  <li><a href="https://itnext.io/flutter-with-wsl-2-69ba8fc7682f" rel="noreferrer">Flutter with WSL 2</a></li>
  <li><a href="https://stackoverflow.com/questions/60440509/android-command-line-tools-sdkmanager-always-shows-warning-could-not-create-se">Android Command line tools sdkmanager always shows: Warning: Could not create settings</a></li>
  <li><a href="https://levelup.gitconnected.com/how-to-install-flutter-without-android-studio-on-ubuntu-6d8b55741afa" rel="noreferrer">How to Install Flutter Without Android Studio on Ubuntu</a></li>
  <li><a href="https://gist.github.com/mrk-han/66ac1a724456cadf1c93f4218c6060ae" rel="noreferrer">Install and Create Emulators using AVDMANAGER and SDKMANAGER</a></li>
  <li><a href="https://gist.github.com/bergmannjg/461958db03c6ae41a66d264ae6504ade" rel="noreferrer">Building a react native app in WSL2</a></li>
  <li><a href="https://medium.com/@pellea/using-the-android-emulator-on-windows-10-with-wsl2-39c2bae30c49" rel="noreferrer">Using the Android emulator on Windows 10 with WSL2</a></li>
  <li><a href="https://stackoverflow.com/questions/58441350/developing-flutter-with-vscode-and-wsl2">Developing Flutter with VSCode and WSL2</a></li>
  <li><a href="https://medium.com/@pellea/using-adb-with-wsl2-7aebbb070a47" rel="noreferrer">Using ADB with WSL2</a></li>
  <li><a href="https://www.litets.com/article/2019/5/15/401.html" rel="noreferrer">ADB</a></li>
  <li><a href="https://developer.android.com/studio/command-line/adb" rel="noreferrer">Android Debug Bridge (adb)</a></li>
  <li><a href="https://support.honeywellaidc.com/s/article/How-to-use-adb-over-TCPIP-connect" rel="noreferrer">How to use adb over TCPIP connect?</a></li>
  <li><a href="https://www.thetopsites.net/article/58229368.shtml" rel="noreferrer">Running React Native in WSL with the emulator running directly in Windows</a></li>
  <li><a href="https://stackoverflow.com/questions/27245597/how-can-i-deploy-and-execute-an-application-on-a-device-connected-to-a-remote-sy">How can I deploy and execute an application on a device connected to a remote system?</a></li>
  <li><a href="https://stackoverflow.com/questions/55286542/kotlin-could-not-find-the-required-jdk-tools-in-the-java-installation">Kotlin could not find the required JDK tools in the Java installation</a></li>
  <li><a href="https://stackoverflow.com/questions/60460429/android-studio-installs-without-sdkmanager">Android Studio installs without sdkmanager</a></li>
  </ul>