7

I develop all my sites on a OS 10.5.8 server with PHP and MySQL, and I've configured my /etc/hosts and httpd.conf files to display my site at example.dev in any browser on that machine.

I recently installed Android's software development kit which I'd like to use to develop and test stylesheets targeted to mobile Webkit-- but I can't access my locally-hosted projects in the Android browser.

This makes sense, since it's emulating the whole Android OS, but is there a workaround? Or can I only test projects that exist somewhere on the actual computer internet, perhaps in a hidden directory?

Someone suggested that I edit my /etc/resolv.conf file, but I don't know the correct way to format a locally-hosted domain in the resolv.conf-- I tried variations of the following, and nothing works:

domain example.dev
nameserver 127.0.0.1

Or...

domain example.dev
nameserver localhost

In my hosts file, it is set up like this:

127.0.0.1   localhost example.dev example2.dev [etc]

5 Answers 5

13

You should be able to use the IP address of your mac in the URL from the android emulator and be able to access your sites that way. Of course, make sure that Web Sharing is turned on under Sharing in System Preferences, and that your firewall isn't blocking the address.

Alternately, you can use the virtual host IP address, which is 10.0.2.2. This will always map to your host mac when running the emulator.

If for some reason you prefer not to use IP addresses in your urls, you can map your mac's IP address to any name you choose by modifying /etc/hosts in your emulator. To do so, you'll need to first make the filesystem read-write, then add your mac's IP and whatever hostname you want to /etc/hosts:

adb -e shell
# mount -o remount,rw -t yaffs2 /dev/block/mtdblock0 /system
# echo '10.0.2.2      cat-yodeling.local' >> /etc/hosts

Then access "http://cat-yodeling.local" from your browser. You should be good to go.

3
  • I had the same question, and this is exactly the sort of answer I was looking for. Unfortunately, I can't make it work. The commands seem to be working, but cat /etc/hosts afterward shows that the new line has not actually been written. Any further thoughts?
    – Spudley
    Jul 15, 2011 at 10:42
  • I think I've worked out what my problem was. I'll post it as a new answer, as it's a bit too much to fit into a comment.
    – Spudley
    Jul 15, 2011 at 11:23
  • 1
    I'd love to get it so that foo.dev on my simulator hits localhost:3000 on my mac. Make sense?
    – Marc
    May 30, 2014 at 18:08
7

I was having this same issue. @emmby's solution sounded like it should do exactly what I needed, but strangely it didn't seem to work. The commands ran, but looking at the hosts file afterward showed that it wasn't actually being updated.

But I did a bit more research, and I've worked out what the problem was.

The core issue I was having was that the emulated device's operating system was already taking up 100% of available storage, so the edits were not getting written to the hosts file. However there was no error message being given either.

My guess is that the reason @emmby didn't have this problem is because the issue varies depending on the version of Android that you put on the emulator; some versions may give you some free space to work with.

If you do have this problem, the solution to this is to start the emulator with more storage. This can be done with the -partition-size argument for the emulator command, like so:

emulator -partition-size 128 @MyEmulatedDevice

You can then use the adb -e shell command and edit the file as per @emmby's answer.

However, if editing the file from the shell is a pain, adb does also give you the facility to copy it down from the emulator to your host OS, edit it locally, and copy it back up again. It also deals with the remount. You would do it like this:

adb pull /system/etc/hosts C:\wherever\you\want\to\save\it

Then edit it in your favourite editor.

Then remount the emulator to read/write mode, and copy the file back:

adb remount
adb push C:\wherever\you\saved\hosts /system/etc/hosts

Note that since the emulator is reset back to default state whenever you restart it, this might be a better solution, because you can keep the edited file locally, so you don't have to repeat the adb pull and editing steps every time; you can simply start the emulator, do the remount and push, and you're in business.

Hope that helps.

2

I worked around similar issue by using computers IP address in local network.

2

Follow the instructions on the following two links:

http://www.codingonstilts.com/2012/04/android-emulator-hosts-file-entry.html (make sure to pay attention to the tabs between the IP address and the hostname and the newline at the bottom of the file) I did a "[SDKLOCATION]/platform-tools/adb pull /system/etc/hosts WHATEVERHOSTFILENAME" to see the format of the original hostfile first and then use that to push it back.

And then: http://www.codingonstilts.com/2012/06/sync-os-x-hosts-file-to-android.html

Works for me - good luck!!

1
  • Links do not work or no longer anyways. Sites load with white screens
    – rhand
    May 26, 2022 at 12:53
2

As per android docs you can access your system 127.0.0.1 IP using 10.0.2.2 on your emulator which is a special alias to your host loopback interface.

1
  • 1
    it's actually 10.0.2.2, not 10.0.0.2
    – jeremy
    Sep 13, 2013 at 13:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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