Soup to Nuts: Getting Your Android Emulator to Read Virtual Hosts on your Development Machine

ADV Results

So you’re setting up a new website, and need to test out your mobile functionality. Sadly you don’t have a development server in which you can push everything up to and test that way. Instead, you need to take the “localhost” route. In addition, you do a lot of web development, so you have your websites setup using virtual hosts in Apache. So to get your site, you type in something like “demopyrocms.localhost” to access your individual sites. Here’s a breakdown of what you need to do to get everything running smooth.

If you’re using a Mac, you’ll kick up your iOS simulator, type in your url, and you’re all set:

iOS Screenshot
Click for larger view.

However, if you start up your Android emulator, and try the same thing, you’ll get something like this:

AVD Screenshot
Click for larger view.

The reason for this, is that unlike the iPad / iPhone emulators, which are simply simulated, the Android emulators are truly emulated, so they get their own networking environment. The Android environment does provide a workaround for this. They setup the address as a “redirect” of sorts to localhost on the development machine that you’re working on. This works great if you have a single site.

However, as mentioned before, I’m running multiple sites, and I’m using the virtual hosts feature. When you type the address in above, it looks on the device itself, and obviously the website isn’t there. The solution is to update the hosts file on the device with a record that has our virtual host address that points to Here’s how to make that happen.

1. Starting up the Emulator

The first step is to fire up the virtual machine and give it a little extra space on the SD drive to handle the larger file size. Otherwise you’ll get an “out of memory” or different type of error later on. You can’t do this through the AVD manager, you have to do it through the command prompt. AVD uses special device IDs for the emulators that you have created. If you don’t know the name of your device, you can execute the following command to get a list of them:

./android list avd

And you’ll get a listing something like this:

Android Device List
Click for larger view.

The android program will be in the “tools” directory within your install path. For me it’s ~/Applications/android-sdk/tools. Most likely it’ll be different for you. Once you know the name of the device you need, you can start it up from the command prompt:

./emulator -avd [AvdName] -partition-size 128

The emulator program will be in the “tools” directory within your install path. For me it’s ~/Applications/android-sdk/tools. Most likely it’ll be different for you.

2. Mount the emulator

Once the emulator is up and running, we need to mount it through into the debugger so we can update the hosts file. First we need to get the special id of the device:

./adb devices

The adb program will be in the “platform-tools” directory within your install path. For me it’s ~/Applications/android-sdk/platform-tools. Most likely it’ll be different for you.

Sometimes the adb manager may be running, but has not properly detected the emulator. To do this you simply need to stop and restart the adb server:

./adb kill-server
./adb start-server

Your device list will look something like this:

ADB Screenshot
Click for larger view

Now that you have the ID of the device, you need to mount it to the debugger:

./adb -s [DeviceID] remount

You’ll get a confirmation that the device has been mounted:

ADB Remount
Click for larger view.

3. Update the hosts file

From here, there are two way you can update your hosts file. If one doesn’t work, try the other.

Option 1: Pull/Push a copy of the hosts file from the emulator.

Grab a copy of the hosts file from the emulator with the following command:

./adb -s [DeviceID] pull /etc/hosts [YourLocalPath]

The [YourLocalPath] value can be anywhere that is easy for you to access. Then edit the file and add the following line:   [YourVirtualHostName]

Then push it back on to the emulator:

./adb -s [DeviceID] push [YourLocalPath]/hosts /etc/

It’ll look something like this through your command prompt:

ADB Pull/Push
Click for larger view.

Option 2: Edit the hosts file through the ADB shell.

Open up a shell prompt on the device through the following command:

./adb -s [DeviceID] shell

From there, write the virtual host name directly into the hosts file:

echo '   [YourVirtualHostName]' >> /etc/hosts

Then exit out of the shell.


Iti’ll look something like this through the command prompt:

ADB Echo
Click for larger view.

Note: After some additional testing, I’ve found that your emulators will vary as to the location of your hosts file. Some devices like to store it in /system/etc/, some like to store it in /etc/. If one path doesn’t work, try the other.

4. Verify the results

Open up the browser and load up the site. You should now be able to see it through the emulator:

ADV Results
Click for larger view.

There you have it! Now you can do some local testing with your Android emulator. I haven’t tried it yet, but I believe if you plug in your Android phone and load it up through ADB, you can do the same thing. I’ll try to report back on that a little later.

Hat Tips Galore

I wouldn’t be doing right by people if I didn’t list the links that helps me piece this all together. There were plenty of sites that had 2 or 3 pieces of the puzzle, but didn’t account for an error I ran into, so I’d go to another place. So here’s the HT list that helped me out.



6 thoughts on “Soup to Nuts: Getting Your Android Emulator to Read Virtual Hosts on your Development Machine”

  1. Hello,

    Thank you for your directions! They almost worked for me :) I get to the point where I am trying to copy the hosts file, and then I get the following error msg:

    failed to copy ‘c:\temp1\hosts’ to ‘/system/etc/hosts’: Read-only file system

    Have you seen this before? Any way you can think of to get around it?

    Thank you in advance :)

    1. Hmm, that is an odd one. I have not run into that before. Here are a few ideas to try:

      1. I’ve found that some devices prefer to use /etc/hosts as opposed to /system/etc/hosts. Have you tried pushing there?

      2. The other thing to try would be doing the “direct write” method I also outlined where you echo the content directly into the file.

      3. Depending on the device you’re emulating, you may need to increase the partition disk space larger than what I outlined here. Instead of executing

      ./emulator -avd [AvdName] -partition-size 128

      You’ll want to try something like

      ./emulator -avd [AvdName] -partition-size 256

      Hope this helps!

  2. Thanks Dillie.. option 1 is the easiest & practical when it comes to editing a large number vhosts especially.. Thanks for the step by step approach..

What are your 10 bits on the matter? I want to know!

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s