How to add custom mapping of a hostname to IP address in Android Emulator

There is a simple trick which allows your application to resolve a hostname to IP address defined by you even in cases when you do not have access to DNS server. This trick could be also used when you’re connected via VPN and the emulator is not able to resolve DNS record available only in private network.

Let’s add following hostname record which should be resolved by application on Android:

192.168.1.2 test.georgik.rocks

First of all, you need to start your AVD with parameter ‘-writable-system’, because emulator’s filesystem is by default read-only.

Let’s get names of available AVDs.

Example for macOS:

cd ~/Library/Android/sdk/tools
./emulator -avd -list

The output might look like:

Pixel_2_API_23
Pixel_2_API_24

Start the emulator:

./emulator -avd "Pixel_2_API_23" -writable-system

You should see the following warning:

emulator: WARNING: System image is writable

The emulator will start. Now you need to remount the filesystem so it will become writable. This could be done via adb tool which is in platform-tools directory.

Version for macOS:

cd ~/Library/Android/sdk/platform-tools
./adb remount

Now you can start the shell and append the line with configuration to /etc/hosts:

./adb shell 'echo "192.168.1.2 test.georgik.rocks" >> /etc/hosts'

Now you can test the configuration by ping:

root@generic_x86_64:/etc # ping test.georgik.rocks
PING test.georgik.rocks (192.168.1.2) 56(84) bytes of data.