Repository: flutter
Issue: Support custom ADB sockets while debugging
Labels: c: new feature,platform-android,tool,platform-windows,P3,team-android,triaged-android

Hi,

I am working with a vscode devcontainer that contains the flutter installation.
My environment is Windows 10 with WSL2 and Docker (WSL2-backend). The devcontainer runs on this WSL2-backend-Docker instance.
First I created this issue https://github.com/Dart-Code/Dart-Code/issues/2640 but it turned out that it looks like a problem with flutter itself.

Due to the fact that it is currently not possible to attach USB devices directly in WSL2 we need ADB (in the devcontainer) to communicate with the ADB on Windows.

ADB supports this by having the possibility to override its SOCKET (flag `-L` or as we did it by setting the environment variable `ADB_SERVER_SOCKET`). In the devcontainer (where flutter runs) we changed to socket to `tcp:host.docker.internal:5037` via environment variable. On the Windows-side we run `adb -a -P 5037 nodaemon server`. With this setup ADB in the devcontainer has the same output as ADB on Windows (e.g. for `adb devices`).

Unfortunately, with this setup it is not possible to debug a flutter app from the devcontainer.
On startup it says
```
Exception attempting to connect to the VM Service: SocketException: OS Error: Connection refused, errno = 111, address = 127.0.0.1, port = 48590
This was attempt #1. Will retry in 0:00:00.100000.
```
and on the device I get a white screen.

I also tried by setting the argument `--host-vmservice-port=9999` but this has no effect. The output then is
```
Forwarded host port 9999 to device port 46347 for Observatory
Connecting to service protocol: http://127.0.0.1:9999/PH5EDsAqjMw=/
Exception attempting to connect to the VM Service: SocketException: OS Error: Connection refused, errno = 111, address = 127.0.0.1, port = 48590
This was attempt #1. Will retry in 0:00:00.100000.
```

With `adb tcpip 5555` and `adb connect <deviceip>:5555` it works. The probems here are
- because the ip of the device has to be known this is no stable approach that can be automated
- it is not possible to connect to an emulator

The output of flutter doctor is
```
[4781 ms] [✓] Flutter (Channel dev, 1.20.0-7.1.pre, on Linux, locale en_US)
[4782 ms]     • Flutter version 1.20.0-7.1.pre at /flutter
[4782 ms]     • Framework revision 7736f3bc90 (5 days ago), 2020-07-10 16:33:05 -0700
[4783 ms]     • Engine revision d48085141c
    • Dart version 2.9.0 (build 2.9.0-21.2.beta)
[4784 ms] 
[9737 ms] [✓] Android toolchain - develop for Android devices (Android SDK version 28.0.3)
    • Android SDK at /android-sdk
[9738 ms]     • Platform android-28, build-tools 28.0.3
    • Java binary at: /usr/bin/java
[9738 ms]     • Java version OpenJDK Runtime Environment (build 1.8.0_252-8u252-b09-1~18.04-b09)
[9738 ms]     • All Android licenses accepted.

[9739 ms] [!] Android Studio (not installed)
[9739 ms]     • Android Studio not found; download from https://developer.android.com/studio/index.html
[9740 ms]       (or visit https://flutter.dev/docs/get-started/install/linux#android-setup for detailed instructions).
[9740 ms] 
[9741 ms] [✓] Connected device (1 available)
[9741 ms]     • Nexus 5X                     • 0263ecc816b81901 • android-arm64  • Android 8.1.0 (API 27)
```

Am I missing something or is this a bug/missing feature?

Regards,
Florian