Using Virtual Hosts on the Android Emulator

We know that it is a very good practice to use Virtual Hosts on your local machine to develop websites locally. Using paths like http://localhost/site1 are not only bad looking, it gives problems when you reference files, images, ... relative to your domain. It also helps you get better organised.

There are some steps to undertake in your systems hosts file and in your apache server in order to get these virtual hosts running. Just google or Bing for a tutorial.

Virtual Hosts and Mobile Emulators

When you are working on mobile projects, emulators are needed to start testing your websites. I mainly use the iPhone and the Android emulator.

While the iPhone emulator does not require any additional configurations to make your virtual hosts work, doing the same on an Android emulator can be quiet frustrating (certainly if you care more about web design then configuring your mobile devices).

After some search and trying several methods, this is the one that made it work for me:

It comes down that you make a copy of your systems hosts file where you replace '' (your local ip-address) by '' (the alias for your local machine in the android device).

example:      site.local

becomes        site.local

once you have saved this file (in the example to /Users/user/AndroidHost/hosts), you can push it to the android device and overwrite the default hosts file. To make the emulator writable you have to first execute the 'adb remount' command and then push the new file to the device:

$ adb remount  
remount succeeded
$ adb push /Users/user/AndroidHost/hosts /system/etc/

You have to do this each time you start an emulator... But it works!
Tips on how to persist this are always welcome!

UPDATE: When starting your emulator you will have to use the -partition-size command to avoid out-of-memory messages.

$ emulator -avd [your-avd] -partition-size 128