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: http://sacoskun.blogspot.com/2009/06/configure-hosts-file-in-android.html

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

example:

127.0.0.1      site.local

becomes

10.0.2.2        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