Hosts files and the Google Android emulator

Hosts files and the Google Android emulator

Using the Google Android emulator is a good way to test how a website behaves on Android devices.

Connecting to your local computer

http://10.0.2.2 is the URL to connect to a website hosted on your local computer. localhost or 127.0.0.1 will not work! This is all defined in the documentation on Android emulator networking.

Using a hosts file

The Android emulator will not make use of your local hosts file. This is unfortunate when your website relies on host headers to work correctly (eg. SharePoint). What you need to do is edit the hosts file on the Android emulator image itself. 

  1. start your Android Virtual Device (AVD):emulator -avd myAvdNameHere -partition-size 128 This is located in C:\Program Files (x86)\Android\android-sdk-windows\tools\ on Windows 64bit.Note: The partition-size parameter is needed to expand the image size to prevent an error in step 5. If you try and perform these steps after starting the emulator from the UI you will receive the following error: failed to copy ‘c:\temp\hosts’ to ‘/system/etc/hosts’: Out of memory
  2. remount the device image as writable:adb remount This is located in C:\Program Files (x86)\Android\android-sdk-windows\platform-tools\ on Windows 64bit.
  3. save a copy of the existing hosts file to a temporary location on your host computer:adb pull /system/etc/hosts c:\temp
  4. edit the hosts file adding an entry pointing to your host computer:127.0.0.1 localhost 10.0.2.2 mytesthost
  5. save the edited hosts file to your Android emulator:adb push c:\temp\hosts /system/etc You can now browse to your entered hostname in the Android browser.

Other method:

Step by Step

Don’t Create the AVD with a Google Play image. 
Use for example Google APIs Intel x86 Atom System Image. 
Start the emulator with the following command…

emulator.exe –avd <avd name> -writable-system
For example:

    C:\Users\ilyas.mamun\AppData\Local\Android\Sdk\emulator>emulator.exe -avd Pixel_API_25 -writable-system

    emulator: WARNING: System image is writable
    HAX is working and emulator runs in fast virt mode.
    audio: Failed to create voice `goldfish_audio_in'
    qemu-system-i386.exe: warning: opening audio input failed
    audio: Failed to create voice `adc'
Root and Remount the AVD like the followings…

C:\Users\ilyas.mamun\AppData\Local\Android\Sdk\platform-tools>adb root

C:\Users\ilyas.mamun\AppData\Local\Android\Sdk\platform-tools>adb remount
remount succeeded

C:\Users\ilyas.mamun\AppData\Local\Android\Sdk\platform-tools>adb shell
eneric_x86:/ # cd system
generic_x86:/system # cd etc
generic_x86:/system/etc # cat hosts
127.0.0.1       localhost
::1             ip6-localhost

generic_x86:/system/etc # echo "192.168.1.120   ilyasmamun.blogspot.com" >> hosts
generic_x86:/system/etc # cat hosts

127.0.0.1       localhost
::1             ip6-localhost
192.168.1.120     ilyasmamun.blogspot.com
generic_x86:/system/etc #

Leave a Reply