10

I having problem in using my system hosts file details in Android Emulators. I have my website (www.example.com) deployed in Webserver. I am trying to access those website in Emulator from my desktop. My desktop has a hosts file with the entry of

10.xx.xx.xx www.example.com

I am trying to access the www.example.com from the android emulator - browser and it is not working.

I took a reference from the below website, but most of the website says how to use the hosts file if the website is deployed in the same server.

http://sadhanasiblog.blogspot.in/2012/07/local-environment-setup-for-android.html

Please let me know if anyone has answers. Thanks in Advance.

-Senthil

1

3 Answers 3

16

With latest Android studio and tools. You can now use follow this instructions. You only need to run this once per emulator.

  1. Start the emulator with following command. This command is located under your [sdk folder]/tools directory

    emulator @[emulator name] -writable-system
    
  2. Open another command prompt and then switch your directory to [sdk folder]/platform-tools and run following commands

    adb root
    adb remount
    adb shell
    echo '10.0.2.2 [localserver dns name]'>>/etc/hosts        -- example echo '10.0.2.2 xxxx.com'>>/etc/hosts
    
3
  • the -writable-system is the most important bit you won't find mentioned anywhere else
    – Ali Kazi
    Commented Mar 22, 2018 at 0:34
  • I think one more step was remaining which rebooting the device to apply these changes, by executing this command: adb reboot
    – MrDEV
    Commented Sep 16, 2019 at 14:18
  • This only work with emulators without the play store. Otherwise adb root fails.
    – Richard
    Commented Jul 3, 2020 at 1:04
10

One important thing to check is, if you create your HOSTS file on Windows or MacOS machine, that the HOSTS file contains linux line endings ('\n', LF, #10, #0x0A). Android ignores the HOSTS file if it contains Windows' 2-char line endings ('\r\n', CRLF, #13#10, #0x0D0A) or MacOS-style line endings ('\r', CR, #13, #0x0D).

Except for letters/numbers/dots (used for IP and host names) the file should contain only characters 0x20 (space), 0x09 (tab) and 0x0A (new line).

1
  • THANK YOU! I spent a day on trying to figure this out, until I finally found this awesome post!
    – Gadzair
    Commented Oct 14, 2014 at 17:07
3

To acces to your localhost the IP is

10.0.2.2 Special alias to your host loopback interface (i.e., 127.0.0.1 on your development machine)

From Android docs:

At startup, the emulator reads the list of DNS servers that your system is currently using. It then stores the IP addresses of up to four servers on this list and sets up aliases to them on the emulated addresses 10.0.2.3, 10.0.2.4, 10.0.2.5 and 10.0.2.6 as needed.

On Linux and OS X, the emulator obtains the DNS server addresses by parsing the file /etc/resolv.conf. On Windows, the emulator obtains the addresses by calling the GetNetworkParams() API. Note that this usually means that the emulator ignores the content of your "hosts" file (/etc/hosts on Linux/OS X, %WINDOWS%/system32/HOSTS on Windows).

When starting the emulator at the command line, you can also use the -dns-server option to manually specify the addresses of DNS servers to use, where is a comma-separated list of server names or IP addresses. You might find this option useful if you encounter DNS resolution problems in the emulated network (for example, an "Unknown Host error" message that appears when using the web browser).

At your console use:

emulator -avd <you_avd_name> -dns-server <serverList>

I used when I had to connect to a VirtualBox with a linux distro on the same PC and it worked from the emulator webview http://10.0.2.3/servlet-name

Reference: Emulator Networking

Not the answer you're looking for? Browse other questions tagged or ask your own question.