GPGVM
GPGVM

Reputation: 5619

AVD Emulator Browser won't use hosts file

I'm pretty frustrated with this one. I have a dev web server running a service feed for my Android app. I can browse to it from any number of machines.

On the AVD (and I've tried different versions all the same) I remount rw and pull, edit then push back the host file to system/etc/hosts. In the DDMS I see the date updated and if I pull it back off the changes are there.

Further if I go to a command prompt then run adb shell and ping the testhost name I get a reply from the correct ip. So why?????? will the avd browser not load the page. Oh yes the page. Plain HTML that says YES I am working.

In the end if I don't browse to the html page that's fine but since I need my app to query my service (hosted same site) for development and testing purposes I was thinking this would be a good test for connectivity before trying to have my app make a request to the service.

Stumped. .....


Solved!!!

My windows background has me editing a hosts file with IP then tab then host name. Well I followed some instructions that had me edit the hosts from the adb shell. That worked...huh??? so I pulled the file and opened in Edit+ only to find I was shooting muyself in the foot. Only *ONE space between the ip and host name. It is working great now.

Upvotes: 1

Views: 3966

Answers (4)

sk_rover
sk_rover

Reputation: 36

The easiest way and the simple answer is:

It's the end of line(EOL) character for each line in the hosts file. The EOL should be that of linux(LF). The number of spaces or tabs doesn't matter.

For converting EOL character you can follow the following steps in notepad++: Edit-> EOL Conversion-> Linux(LF).

To verify: View -> Show symbol -> show end of line.

Once you have verified the above in your hosts file, simply push it to emulator.

Use the following commands to ensure your hosts file is replaced on emulator:

adb push ./hosts /etc/hosts

adb push ./hosts /system/etc/hosts

adb push ./hosts /etc/system/hosts

Note: You need to have your emulator running in write mode.

Upvotes: 1

Rodrigo García
Rodrigo García

Reputation: 1417

In my particular case, localhost subdomains give this problem on the android emulator browser (even though I can ping the domain), I was using the next mapping on my macOS 10.14:

10.0.2.2    something.localhost

Getting rid of the localhost fixed the issue for me. I don't know why this happens on my Mac, I couldn't reproduce this behavior on Ubuntu 18.04

Upvotes: 1

RiturajDashore
RiturajDashore

Reputation: 31

In windows host file its allowed to have multiple spaces (tabs) between IP address and hostname, but in android host file it requires single space between IP and HostName. I tried with a host file with single entry in which IP and Hostname are separated by single space character using notepad++. But still problem persists, ie I was unable to open the site in emulator.

You can find steps to push host file of your local machine to android emulator -> http://borkweb.com/story/setting-etchosts-on-an-android-emulator-using-a-mac

But still emulator is unable to use this host file which got resolved as -> I tried this (Worked): 1. pushed a empty host file in emulator 2. Edited the same host file using adb shell echo command

#adb shell    
#echo '172.30.1.227 fat.choice.plus' >> system/etc/hosts    
#echo '\n172.30.1.227 static-fat.loyrewards.com' >> system/etc/hosts    
  • use your IP address and hostname in above commands
  • To come out of adb shell use shortcut Ctrl + D You can add multiple entries via echo using \n character for newline. Emulator is now able to use this modified host file and site is loaded successfully. :-)

Upvotes: 1

GPGVM
GPGVM

Reputation: 5619

My windows background has me editing a hosts file with IP then tab then host name. Well I followed some instructions that had me edit the hosts from the adb shell. That worked...huh??? so I pulled the file and opened in Edit+ only to find I was shooting muyself in the foot. Only *ONE space between the ip and host name. It is working great now. If in doubt I would highly recommend updating your host file without pulling the file to the desktop. This is the command from the shell to do so.

./adb -s [DeviceID] shell
echo '###.###.###.###   [HostName/URl]' >> /etc/hosts
exit

NOTE The above command is not every step needed just the command to actually edit the file from the shell. You still need to have write perms etc.

Upvotes: 1

Related Questions