Android Emulator, localhost and host files on Windows

We all use emulator with comes with Android SDK to test, how app / websites behave in Android devices.

While working on my applications I wanted to access my localhost which was apache server. I tired using IP address as 127.0.0.1 and typing http://localhost many times. But then I gone through the Android Developer site and found that IP Address to access localhost is http://10.0.2.2
This is because 127.0.0.1 is IP address for locahost of Emulator itself.

Going forward came across one more similar problem. I had setup one xml API on my localhost and added host entry in hosts file of windows which is located at “C:\WINDOWS\system32\drivers\etc“. But the host entry was not working for emulator browser and not even in the app. Here goes the solution for the problem:-

Step 1:-
First open start your emulator from command line. Go to /android-sdk-windows\tools and then type below command.

Note:- Remember that you need to pass partition size and If you don’t pass it then will get error “failed to copy ‘c:\temp\hosts’ to ‘/system/etc/hosts’: Out of memory” in the 5th step.

Step 2:-
Now once emulator is loaded completely with home screen. Open another command line, Go to /android-sdk-windows\platform-tools and type below command to remount the device image as writable:

Step 3:-
Now get the copy of existing file from host computer i.e. emulator to some temporary location.

Step 4:-
Go to your C drive and edit the hosts file and put your host entries:-
127.0.0.1 localhost
10.0.2.2 www.myfirstdomain.com
10.0.2.3 myotherhost

Step 5:-
Once you save the file. Now we should replace the original file of emulator with the new one.

Here you go. Now you can browse the site in browser or you can use API which will your host file entry.

Remember that this is not permanent. You need to do this each time you want to use host file entries. You can repeat step1, step2 and step5. You already have file with you so need to pull and make changes again so step 3 and step 4 is avoided.

Please make sure that you put your comments that If you understand this tutorial easily or it was complicated for you. 🙂

 

7 Comment

  1. yao says: Reply

    hi, do you resolve the setSelection for HorizontialListView from http://www.dev-smart.com/archives/34, how do u do that now?thank you

  2. Janaka says: Reply

    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. Umakant says: Reply

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

  3. Rajesh says: Reply

    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)

  4. Cesar says: Reply

    Please , Helpme

    Why it is error?…

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

    Thanks

  5. karthi says: Reply

    its not working

    1. Umakant says: Reply

      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 Reply