29 Sep 2024

Connect to a custom local domain from Android emulator

Submitted by blizzz
Photo by <a href="https://unsplash.com/@rocknrollmonkey?utm_content=creditCopyText&utm_medium=referral&utm_source=unsplash">Rock'n Roll Monkey</a> on <a href="https://unsplash.com/photos/blue-plastic-robot-toy-R4WCbazrD1g">Unsplash</a>

In the middle of September I have been at the Nextcloud conference and took the opportunity to join a workshop on Android dev (I still utterly dislike Android, but that is a different topic). Back in the day I already tried and had troubles setting up Android Studio, so why not trying to get it running while the Pros are around.

Turned out it simply worked after installation. No idea what I did wrong last time, maybe pushing the wrong button. However, it until hold until I tried to connect the checked out and running Nextcloud app against my local dev or test setup. I have the habit to run a few very custom domains that are only made known in the /etc/hosts file, cloud.nextcloud.com with TLS amongst others.

Android runs in some sorts of virtual machine or even docker container, and can connect to the host, but of course is not aware of any of the domains there. My second idea was to use dnsmasq as a DNS server and to specify this per -dns-server flag against the emulator. I did not work and I am not sure why. But I followed the first idea instead, and made it work eventually. This one is modifying the hosts file within the Android emulator.

And actually it is written in detail in Method 3 of this blog post by Jeroen Mols. Various other Internet sources coughSOcough have a shorter subset of these instructions that won’t work anymore. When you take Jeroens advice, eventually you will make it:

Screenshot of the Nextcloud app connected to cloud.example.com running on the host system

I took this few instructions and put it into a small quick bash script. It takes one argument (I did not include a check though), which is the avd that shall be booted. That is the virtual devices, and not all of them are working (the default one did not), as it is necessary to become root and temper with android’s filesystem. Pixel_6_Pro_API_VanillaIceCream is working well for me.

Otherwise the script assumes the Android Studio executables as on an Arch Linux install. Further on, it will copy the /usr/local/etc/hosts – place it there first. It should be something like:

127.0.0.1       localhost
::1             ip6-localhost
10.0.2.2        cloud.example.com all.your.domains

Find my android emulator launch script on Codeberg.

Thanks go out to Alper and Tobi for the help on site!

Add new comment