Android Emulator, localhost and host files on Windows

Most of us use the standard Android SDK emulator to test how our apps and websites behave on virtual mobile devices.

While working on my own applications, I ran into a couple of annoying roadblocks trying to connect the emulator to my local development environment. If you’ve spent an afternoon scratching your head trying to get your virtual device to talk to your computer’s local server, here is how you fix it.

When I first started, I tried pointing my emulator’s browser to 127.0.0.1 and http://localhost a dozen times, but it kept failing.

After digging through the Android Developer documentation, I found out why: 127.0.0.1 points to the emulator’s own loopback interface, not your computer’s. To tell the emulator to access your development machine’s localhost (like an Apache or Node server), you have to use a special alias IP address:

http://10.0.2.2

Using 10.0.2.2 works great for raw IP access, but I ran into another problem. I had set up a custom XML API on my local server and mapped it to a custom domain in my Windows hosts file (C:\Windows\System32\drivers\etc\hosts). Unfortunately, the emulator completely ignores your Windows host entries.

To fix this, you have to manually inject your custom domains directly into the emulator’s own internal hosts file. Here is the step-by-step process to make that happen:

Step 1: Boot the Emulator with Extra Partition Size

Open your command prompt, navigate to your Android SDK tools folder (/android-sdk-windows/tools), and launch your emulator using the command line:

Bash
emulator -avd YourAvdNameGoesHere -partition-size 128

Important: You must include the -partition-size 128 (or larger) flag here. If you skip this, the emulator’s system partition will be locked as out of memory, and you will get a frustrating error during Step 5.

Step 2: Remount the System as Writable

Once the emulator finishes loading completely and you see the home screen, open a second command prompt window. Navigate to your platform tools folder (/android-sdk-windows/platform-tools) and run:

Bash
adb remount

This changes the emulator’s file system from read-only to writable.

Step 3: Pull the Current Hosts File

Next, extract a copy of the emulator’s existing hosts file and save it temporarily to your local C:\ drive:

Bash
adb pull /system/etc/hosts c:\hosts

Step 4: Add Your Custom Mappings

Open the newly pulled c:\hosts file in a text editor (like Notepad) and add your custom local domains pointing to the host machine’s IP (10.0.2.2):

Plaintext
127.0.0.1       localhost
10.0.2.2        www.myfirstdomain.com
10.0.2.2        myotherhost

(Save and close the file when you’re done).

Step 5: Push the File Back to the Emulator

Finally, replace the emulator’s original system hosts file with your modified version:

Bash
adb push c:\hosts /system/etc

Final note

And there you go! Now you can open the emulator’s browser or test your app’s API calls using your custom local domains, and they will route properly.

Just keep in mind that this change isn’t completely permanent; if you wipe or cleanly reboot the emulator, you would have to re-apply it. However, since you already have the modified file sitting on your C:\ drive, you can skip the pulling/editing steps next time and just run Steps 1, 2, and 5.

Let me know in the comments if this worked smoothly for you or if you ran into any hiccups along the way! 🙂

 

7 Comments

  1. Setting is not permanent, Everytime when we re-boot , we have to set the hosts setting , Is there a way to keep it permanent ?

    1. Janaka, Right now there is NO way to keep this settings permanent. You need to do this everytime you re-boot emulator.

  2. I have XP image which has android mobile.I did the same as specified above on android emulator. I also checked the host entry after step-5 throgh “adb -e shell, cat /system/etc/hosts ” command and it shows properly. after that I hit the url which is mentioned in host file but it is not redirecting me to that url. can you please help me sort out this issue. It would also be helpful if anyone know which text editor present on android emulator so that i can update the host entry directly. ( I tried with Pico and Vi but it doesn’t work)

  3. Please , Helpme

    Why it is error?


    failed to copy ‘c:\temp\hosts’ to ‘/system/etc/hosts’: Directory not empty

    Thanks

    1. It’s quite a old post. I’m no more working on Android, so couldn’t find time to share the updated steps.

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.