Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I look at locally-hosted projects with the Android SDK emulator?

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]
like image 641
John Stephens Avatar asked Aug 24 '09 02:08

John Stephens


People also ask

Can I access localhost from Android Emulator?

If you're using Android Studio to run the emulator, then localhost of your host computer will be mapped to the IP address, 10.0. 2.2 , inside the emulator. If you're using other programs to run the emulator, then you may need to consult the documentation associated with those programs.

Where are Android Emulator files stored?

All applications and files that you have deployed to the Android emulator are stored in a file named userdata-qemu. img located in the C:\Users\<username>\. android\avd\<avd_name>. avd folder.

How do I see apps on my emulator?

Run your app on the emulator After you have created an Android Virtual Device (AVD), you can start the Android Emulator and run an app in your project: In the toolbar, select the AVD that you want to run your app on from the target device drop-down menu. Click Run.

How can I use Android Emulator instead of Android Studio?

Run your app on the deviceIn the Android Studio toolbar, select your app from the run configurations drop-down menu. From the target device drop-down menu, select the device that you want to run your app on. Select Run ▷. This will launch the app on your connected device.


3 Answers

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.

like image 184
emmby Avatar answered Sep 27 '22 18:09

emmby


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.

like image 43
Spudley Avatar answered Sep 27 '22 17:09

Spudley


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

like image 39
JaanusSiim Avatar answered Sep 27 '22 16:09

JaanusSiim