Eclectic Dreams

A Web Design and Development Blog

Getting a hosts file onto an Android Emulator

Friday, July 29th, 2011

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.

11 Responses to “Getting a hosts file onto an Android Emulator”

  1. Troy noted:

    This doesn’t seem to work for me, even though it says that it pushed the host file. Any ideas?

  2. Matt noted:

    Hmm. Did you run the emulator from the command line and remount? You have to do that every time, as far as I can tell.

    Also, you have to wait ’til the emulator’s fully loaded, that might take some time on some machines. Check the link to the wrapped script on Forrst which has a built in wait for emulator to properly load and see if that works.

  3. WilliamB noted:

    It doesn’t work for me. I even checked the file was correctly updated by pulling it back, it just looks like Android is ignoring the changes made to the host file.

    I even added regular site ip to check if it was because of my local setting or not but it just doesn’t take into account the change to host file.

    I’m using android sdk tools revision 16 and adroid sdk plateform tools rev 10.

  4. Matt noted:

    Sounds like you’ve hit the read only filesystem step, did you run the remount? If so did it give you any errors when you tried?

    The more up to date version of this is at: Forrst so it might be worth using that.

  5. Geir Freysson noted:

    I just thought I’d let you know that post was quite useful to me just now.

    All I wanted was to replace the emulator’s host file, and that did the trick. There were lots of answers on Stackoverflow that were so much more complicated.

    Thanks!

  6. Ilya noted:

    Thanks.

  7. Brian noted:

    If you can confirm the correct hostfile has been pushed to the emulator, and it still doesn’t work, maybe just try restart the browser.

  8. dasaneleq noted:

    i use “-partition-size 128” and steel got a “Out of memory” msg. so i use “-partition-size 256” and that’s help me:)

    Also i got same problem with ignoring the hosts file. but after rebooting the system (rebooting of the device give me nothing) it start to work. just use step-by-step instruction posted here. thx guys!

  9. ifool noted:

    It works, very helpful, thanks a lot!

  10. mightysam noted:

    Thanks a ton Dude. You just saved me a hell lot of trouble. This is really helpful. Dasaneleq’s comment also helped about the memory issue.

  11. Matt noted:

    @mightysam you’re welcome!