• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps Android Emulator: edit hosts file doesn't resolve custom domain name

obakasan

Lurker
Dec 20, 2010
4
0
Halo All,

I'm developing a mobile website and I want to see how it looks in Android Simulator.

I manage to edit my android's /etc/hosts file. Here's what I did:
1. Create an Android Virtual Device (AVD)
2. Open the AVD with this command
Code:
./emulator -avd android -partition-size 128
3. Execute the following commands
Code:
./adb remount
4. Get the android hosts file
Code:
./adb pull /etc/hosts
5. Add my mobile website into the android's hosts file. It looks like the following
Code:
127.0.0.1          localhost
172.19.14.46     m.myweb.localhost
6. Push the hosts file to the simulator
Code:
./adb push hosts /etc/hosts
7. Open the web mobile in the simulator and Host do not found. The web mobile should appear.

Did I miss something here?

Btw I use Ubuntu 10.10 and android-sdk_r08-linux_86 API 7 (Android SDK 2.1)
 
I've made the same steps like you, afterwards tried to enter hostname, got google search page (ie. not found). Then, I've tried http://hostname and it worked. I've tried it in 1.6 and 2.2 emulator and was working well on both.
Can you pull hosts after pushing it? Just to be sure, it's updated (I got error on 2.2 due to low space and had to use 256MB memory).
I've tried pulling and pushing symlink in /etc and also real file in /system/etc, both with success.
Good luck!

PS: I've installed aLogcat (I don't have .apk, have emulator with Market enabled) to see log messages in emulator. On 12-22 17:58:13.090 (see screenshot) you can see the adress pulled from /ets/hosts (I've enterted http://kejly into browser).
 

Attachments

  • alog.png
    alog.png
    27.8 KB · Views: 231
Upvote 0
Hey folk, I had a similar issue - I don't know if this will help you or not, but there's a couple things you should know about the android hosts file:

#1 - it doesn't seem to appreciate standard comments that begin with the pound sign: "#", in fact, it seems to just break the hosts file.

#2 - Special characters like ^M will render it useless as well.

What I was doing was using a master host list, of which had dozens of comments, and I realized that the comment lines were causing dismay, so I tried stripping it using the following command:

cat myhostsfile | sed -e 's;#.*;;' -e '/^[ ]*$/d' > ./mynewhostsfile

Not surprisingly, if I used vi editor to open 'mynewhostsfile' I found those pesky ^M characters flooding all over the place in place of my comments...

Android OS also doesn't appreciate these characters in it's hosts file, and I wasn't about to vi edit every single line where there's hundreds of host lines to work with.

I tried to remove the ^M using vi editor via the terminal emulator on my android first, however, my CTRL-v CTRL-M combo in vi editor was failing, so I had to end up using vi on a Linux desktop to fix this problem...if you don't have linux, I suggest using an Ubuntu Live CD (Ubuntu homepage | Ubuntu) or install VirtualBox (VirtualBox) or tiny linux/etc. (Please note, UnxUtils on Win32/64 encounters a problem with the sed command).

It May or May Not work on Your android, it certainly Doesn't work on my android's terminal emulator (Galaxy S / Captivate)

Once you copy/upload your modified hosts file to your linux box, to remove all "^M" characters from the host file, open a terminal and open the hosts file in vi editor:

vi myhostsfile

...and use this command within vi:

:%s/^M//g

The ^M in the above line has to be typed in by pressing CTRL+v and then CTRL+M.

Then type:

:wq

Then all I did was transfer this new file back, open android terminal emulator again, and type:

head /system/etc/hosts

Confirm that my hosts file is working proper, then type:

sync

Then ping a host in that hosts file...you Should now get a response! Enjoy!

You might also wish to reboot afterwards:

reboot

Hope this clarifies some things, I'm sure this is buried in the documentation somewhere, but whatever - some of us aren't doing this stuff professionally.
 
Upvote 0
@kejlyCZ, I'm having a trouble in installing Market in Android Emulator. So I haven't installed the logcat. It would be great if you could tell me how to install it.

@ouroboros2012, my hosts file for the Android Emulator came from the Android Emulator itself. And then I edited it. So there's no "#" or "^M" characters or some kind... The content of my hosts file is like number 5 of my first post.


OK here's the things I found recently:
- It works Android Emulator installed on Windows (Windows XP).
- In Android Emulator installed on Ubuntu, I can't open a site with a port whereas on Windows I can.

For example, my dev machine is pc283.domain.com. I set up 2 website in my machine. They are: http://pc283.domain.com/ and http://pc283.domain.com:81/.
In Android Emulator Windows, I can open both of them.
In Android Emulator Ubuntu I can open the http://pc283.domain.com/ however I cannot open http://pc283.domain.com:81/. "Web page not available" page appears.

The thing that makes it more strange is I can open http://pc283.domain.com:81/ on my Ubuntu machine!

Is there some thing wrong with my Android Emulator Ubuntu?
Wierd, isn't it?

Anyway, thanks for all of you who reply :)
 
Upvote 0

BEST TECH IN 2023

We've been tracking upcoming products and ranking the best tech since 2007. Thanks for trusting our opinion: we get rewarded through affiliate links that earn us a commission and we invite you to learn more about us.

Smartphones