Access a web server on your localhost from Android emulator using a domain name

Cet article a été publié le 10/08/2015
Les informations ou les techniques décrites peuvent être obsolètes.

It’s fairly easy to access your localhost from an Android emulator. The Android documentation explains that is a special alias to your host loopback interface (i.e., on your development machine).

However, if you want to access a domain name (instead of an IP address) it becomes a little trickier. I wanted to use a domain name to validate the behaviour of my Android code when accessing a HTTPS website.

Let’s say you want that resolves in There are two options:

  1. Modify /etc/hosts on the Android emulator (note: the emulator doesn’t read/use your localhost /etc/hosts)
  2. Setup your localhost DNS to answer when he is asked to resolve

There are several blog posts which explains how to modify /etc/hosts on the Android emulator. I went for the second option.

At startup, the emulator reads the list of DNS servers that your system is currently using. Since Ubuntu 12.04, Network-Manager uses Dnsmasq to speed up DNS queries. By default, Dnsmasq doesn’t use /etc/hosts, but it is possible to force it like this:

echo '' >> /etc/hosts
echo 'addn-hosts=/etc/hosts' > /etc/NetworkManager/dnsmasq.d/etc-hosts
service network-manager restart

Restart your emulator. Android will resolve in

Configure DNSMASQ to use /etc/hosts file

Retour haut de page