How to Modify the Android Emulator Hosts File

The Android emulator is an odd beast. Unlike the iOS emulator, there's no easy way to sync your system hosts file with the Android hosts file. Until now! We will make two files with some special magic so that your emulator mirrors your computer.

These instructions are OSX only!

There is a Github project which does something similar to this, but I didn't write it, and it's not really documented, and it's a lot of crazy code. Feel free to give it a try if you want. I sure didn't. If you want a quick, home grown solution, continue reading.

Install an Android emulator

Duh. I found a decent tutorial if you need help. The important things you need are:

  • The location of where you put the SDK folder
  • The name ("AVD Name") of the emulator you create

Get dependenc(ies)

We will use the utility fswatch to listen for changes to our system hosts file. Kind of.

You need homebrew (duh).

  • brew update. If that fails, try this.
  • brew install fswatch

Set up scripts

Create two files in the root level SDK folder. This is the folder that has
directories like tools, add-ons, and extras.

Name the first file mod-host.sh. The name is arbitrary, and you can change it as long as you you update the second script (below) with the name.

#!/usr/bin/env bash

./platform-tools/adb remount

# Copy our hosts file into a tmp location and replace all 127.0.0.1 with the
# android emulator host ip
cat /etc/hosts | awk '{ sub(/127.0.0.1/, "10.0.2.2"); print }' > /tmp/hosts

./platform-tools/adb push /tmp/hosts /system/etc

echo "Hosts file updated!"  

Then make a file named watch-hosts.sh

#!/usr/bin/env bash

./host-mod.sh
fswatch /etc ./host-mod.sh  

Don't forget to make both scripts executable!

chmod +x mod-host.sh  
chmod +x watch-hosts.sh  

You will notice in the first script that we are replacing 127.0.0.1 with 10.0.2.2. The Android emulator cannot access your system by 127.0.0.1 and instead maps it to 10.0.2.2, similar to VirtualBox VMs. This means any testing you do against localhost will work in the emulator. It may have side effects, but I've been drinking and that's not my concern.

You might also have noticed that we are not actually watching for changes to /etc/hosts, but instead the /etc directory. The fswatch utility does not work on files, just directories. If something is writing to /etc often the script will run often, and you won't be happy.

Party time

We're good to go! First, start your emulator like such, from the SDK folder:

./tools/emulator -avd EMULATOR_NAME -partition-size MEMORY

For example:

./tools/emulator -avd AVD_for_Galaxy_Nexus_by_Google -partition-size 1024

Then start your watcher script in a different shell / tab / whatever:

$ ./watch-hosts.sh
remount succeeded  
154 KB/s (453 bytes in 0.002s)  
79 KB/s (450 bytes in 0.005s)  
Hosts file updated!  

Now whenever you update your system hosts file, the Android hosts file will be updated to match! What a good day.

Other?

If you want to just pull out the hosts file from the emulator to see what's in it, you can run this:

./platform-tools/adb remount
./platform-tools/adb pull /system/etc/hosts /tmp/hosts

If this post helped you modify the Android hosts file, consider following me on Twitter or buying me a coffee :).

comments powered by Disqus