Getting a hosts file onto an Android Emulator

— 2 minute read

So we're doing some mobile/responsive design work and I wanted to check what they might look like on various devices before we actually got to full testing.

Fortunately having a Mac means the iPhone emulator is a mere 3GB download away in the iPhone SDK... And great, it picks up the systems /etc/hosts so all the nice modifications I've made for testing our app locally get picked up. Easy win.

Android proves more of a problem, so I thought I'd document it here as I had to hunt round developer docs, read a couple of blog posts and cobble together with string.

First get the Android SDK.  Switch into the SDK directory and run:

tools/android

to bring up the SDK and AVD (Emulator) manager. Oh, you should probably install the updates first.

Go to virtual devices and click new to create a new virtual device, call it TestDevice, pick a target version and use those defaults. OK this. You could run the emulator here, but not pass in a hosts file... Quit out. Then run:

tools/emulator -avd TestDevice -partition-size 256 &

This runs the emulator directly and gives it some space so you can actually modify the devices hosts file.

Now run:

./platform-tools/adb devices

To see what the emulator thinks it's called. Probably emulator-5554 or something memorable. So now run:

./platform-tools/adb -s  emulator-5554 remount

To remount the filesystem so its not read only and you'll be able to change the hosts file. Then run:

./platform-tools/adb -s  emulator-5554 pull /etc/hosts

This gets the current hosts file from the device. Edit it in whatever text editor, it's been pulled to the current working dir. Finally run:

./platform-tools/adb -s  emulator-5554 push hosts /etc/hosts

To push it back to the device with the changes.

Now test your webpage.

PS. For added fun, this isn't saved when you save the emulator state. Any ideas why?

Edited: Updated memory allocation based on comments below. I've wrapped this into a script and posted it on Forrst.