Skip to content

Instantly share code, notes, and snippets.

@pellea
Created January 13, 2020 14:44
Show Gist options
  • Save pellea/9d1c39bbc561f781d4190f1d1439f653 to your computer and use it in GitHub Desktop.
Save pellea/9d1c39bbc561f781d4190f1d1439f653 to your computer and use it in GitHub Desktop.
Start ADB for WSL2 in Windows 10
$currentSript = $PSScriptRoot + "\adb-start.ps1"
$adbPath = "C:\Program Files (x86)\Android\android-sdk\platform-tools"
$adbPort = 5037
$adbListenAddress = '0.0.0.0'
$adbConnectAddress = '127.0.0.1'
$currentPrincipal = New-Object Security.Principal.WindowsPrincipal([Security.Principal.WindowsIdentity]::GetCurrent())
if ($currentPrincipal.IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator) -eq $FALSE)
{
Start-Process "powershell.exe" -ArgumentList $currentSript -WindowStyle hidden -Verb runAs
}
# Set ADB path
Set-Location $adbPath
# Remove port mapping
netsh interface portproxy delete v4tov4 listenport=$adbPort listenaddr=$adbListenAddress
# Kill server
./adb.exe kill-server
# Start server
./adb.exe start-server
# Add port mapping
netsh interface portproxy add v4tov4 listenport=$adbPort listenaddr=$adbListenAddress connectport=$adbPort connectaddr=$adbConnectAddress
# Display portproxy
netsh interface portproxy show v4tov4
# Start WSL
wsl --exec exit
# Remove firewall rule for WSL
Set-NetFirewallProfile -DisabledInterfaceAliases "vEthernet (WSL)"
@mununki
Copy link

mununki commented Jan 30, 2020

I've tried your PSScript but, still adb devices in bash has nothing even I started the android emulator in Windows. Actually I've tried to run adb.exe start-server in CMD, then trying adb devices from WSL, but no devices show up. Also, just a note - but when I run start-server in CMD, it starts up a server on a port, then when I run adb devices from WSL, it also starts up a port (same port number as CMD).

@pellea
Copy link
Author

pellea commented Jan 31, 2020

It's not that simple. The emulator you want to use in Windows need to be installed in WSL too (the exact same version).
Your WSL environment need to be set up correctly.

DISCLAIMER:
Please read the full answer before executing command !

Here is some command lines I used in WSL (Ubuntu) to set up my environment:

cd ~
sudo apt install openjdk-8-jdk # --------> no sure if really needed

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

sdkmanager "system-images;android-28;google_apis_playstore;x86_64" "build-tools;28.0.3" "platforms;android-28" --sdk_root="$ANDROID_SDK_ROOT"
sdkmanager "platform-tools" --sdk_root="$ANDROID_SDK_ROOT"
sdkmanager --licenses
touch ~/.android/repositories.cfg

cd ~/.android/platform-tools
adb --version
adb kill-server
adb devices

And I set up in my .zshrc file these environment variables:

# JDK
if [[ -e /usr/lib/jvm/java-8-openjdk-amd64 ]]; then
  export JAVA_HOME="/usr/lib/jvm/java-8-openjdk-amd64"
  export PATH="$PATH:$JAVA_HOME/bin"
fi

# Android tools
if [[ -e $HOME/android-sdk-tools ]]; then
    export PATH="$HOME/android-sdk-tools:$HOME/android-sdk-tools/bin:$PATH"
    export PATH="$HOME/.android/platform-tools:$PATH"
    export ANDROID_HOME="$HOME/.android"
    export ANDROID_SDK_ROOT="$HOME/.android"
    export REPO_OS_OVERRIDE="linux"

    adb kill-server 2> /dev/null
    export ADB_SERVER_SOCKET=tcp:$(cat /etc/resolv.conf | grep nameserver | cut -d' ' -f2):5037
fi

You need to have those env. variables BEFORE using the sdk-manager.
Be aware that the adb server must no be started in WSL so do a "adb kill-server" in WSL before starting the one in Windows.

Hope it helps you,
Adrien.

@mununki
Copy link

mununki commented Feb 2, 2020

Thank you for your response. I followed your additional guide, and I finally get a device list in WSL2 with adb devices. So I tried to run react-native run-android in WSL2, but it tooks a very very long time in processing app:installDebug. I stopped the process about 7m later, but it still processing. Is it normal? or I have something to fix more?

@pellea
Copy link
Author

pellea commented Feb 22, 2020

Yes it takes a little time to complete but I'm more about in a 4 minutes time range.

@wesleyfeitosa
Copy link

I followed your tips and when I run 'adb devices' on powershell this error appears:
adb.exe: failed to check server version: protocol fault (couldn't read status): No error

@pellea
Copy link
Author

pellea commented Apr 11, 2020

You usually get this kind of error when the ADB server is not started.

@angelod1as
Copy link

The first script breaks my windows ADB and Pellea's answer still doesn't make it. I dunno what else I should do to make this work...

@pellea
Copy link
Author

pellea commented Jun 17, 2020

@angelod1as If you don't want to use WSL you can simply remove the port mapping and restart your computer. There is no other windows settings changed. So if anything is still broken after that, I don't think that's from the script. You can also start again the environment using Visual Studio > Tools > Android > restart adb server.

@Sp4Rx
Copy link

Sp4Rx commented Sep 14, 2021

How to remove the DisabledInterfaceAliases ? @pellea

@pellea
Copy link
Author

pellea commented Sep 14, 2021

How to remove the DisabledInterfaceAliases ? @pellea

You can enable the firewall again in the normal windows settings UI.

@Sp4Rx
Copy link

Sp4Rx commented Sep 14, 2021

A screenshot would be helpful @pellea

Anyways on newer version of wsl we can access the windows version of adb inside wsl buy using adb.exe devices
To make thinks easier we can create an alias alias adb='adb.exe' in wsl

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment