3

This is the output of adb devices inside a docker container (a windows server image - also the host is win 10)

PS C:\> adb devices
List of devices attached

PS C:\>

I need to create CI for a Xamarin project, all is done except the part where I need to install the app on the phone via adb....until now I found no solution to use adb from inside a win docker contaienr (for linux there is the option to --privileged -v /dev/bus/usb:/dev/bus/usb, but switching to Linux is not an option( due to company decisions))

Alexandru Circus
  • 5,478
  • 7
  • 52
  • 89

3 Answers3

5

ADB can connect to the device over WiFi and from a networking aspect Docker shouldn't cause any problems for this as long as the network of the Docker Host machine can send traffic to the network of the Android device (if they are the same network, then the answer is definitely yes).

First make sure that the Docker Host machine can reach your device over the WiFi network. A test like pinging the IP of your Android device from the Docker Host machine would work. After that, initiate the ADB over WiFi using the ADB on your Docker Host machine (not the one in your container).

adb tcpip 5555

Once that is done, open a terminal in the container and connect to the Android device using the ADB inside the container.

adb connect <ip-address-of-android-device>

adb devices

Usually, when a new connection is established from an "un-trusted" machine, the device requests the user to confirm the connection and so "trust" the machine. An adbkey is created under %userProfile%/.android on the machine which the device uses to establish a trust every time the connection is made after the first time it happens so that the user doesn't need to confirm the connection every time. Copy or volume bind the adbkey into the <container_userProfile_directory>/.android so device doesn't request from the user to confirm the connection every time.

ahasbini
  • 6,761
  • 2
  • 29
  • 45
  • 1
    Thanks, but there is 1 issue. Each time a container is created and adb connect myIp:5555, I need to manually confirm on the phone the dialog saying: "DO you trust this pc" or something like that. I will try today to copy some file generated on the PC to the docker container to see if the dialog confirmation still appears (some adbkey generated file which I need to copy to the %userprofile%/.android folder) – Alexandru Circus Sep 25 '20 at 08:05
  • 1
    I tried and works, please edit your answer with the part where I need to copy the adbkey file from .android folder into the docker image(to %userProfile%/.android/adbkey) (first try to connect from host and trust the PC from the phone and the file will be generated on PC) – Alexandru Circus Sep 25 '20 at 10:02
0

Until android 5 or 6 you need a driver for adb. Most brands give it free to download on their website.

And if you haven't done it yet, you should enable Adb-Debugging via USB in the developer options on the phone.

  • I've done that..the issue is more docker related, since it doesn't allow USB passthrough in a windows docker container. The adb service works ok in the contianer, the problem is that it hasn't access to the USB communication interface – Alexandru Circus Sep 19 '20 at 12:14
0

For adb you'll have to open the TCP port in the Dockerfile with EXPOSE 5037 (default).

Martin Zeitler
  • 1
  • 19
  • 155
  • 216
  • done that, doesn't work. no device is displayed via adb devices (and I stopped the adb service on the host via adb kill-server) – Alexandru Circus Sep 25 '20 at 08:13
  • Try to run with `--network host`, then it should be able to connect (assuming no other `adb-server` is already running on the host machine). Else you'd need to use another port. – Martin Zeitler Sep 25 '20 at 09:58
  • --network host does not work for windfows containers :(, only for linux containers – Alexandru Circus Sep 25 '20 at 09:59
  • If you can't get any network access, you'd need to use a Linux container. Once wrote an [example](https://github.com/syslogic/cloudbuild-android) for Cloud Build... if it has to be power-shell, builds still could be triggered externally. – Martin Zeitler Sep 25 '20 at 10:04
  • One usually does not even need to connect to a CI/CD environment (unless for some manual testing)... because one can run the integration tests on a head-less emulator or on Firebase Test Matrix. – Martin Zeitler Sep 25 '20 at 11:54