Seeing your MAMP localhost site inside a web browser in an Android Emulator
On OS X your local IP is usually http://127.0.0.1:8080, which is what’s pulled up by http://localhost:8080.
Inside an Android Emulator instance or AVD, that same IP and localhost don’t work.
Android docs point us to this number instead:
Seeing MAMP Virtual Hosts from inside the Android Emulator
According to this post, you need to login into the Emulator’s shell:
$adb -e shell
Then edit the Emulator’s /etc/hosts/ file to point to your http://mySite.localhost
# echo '10.0.2.2 prototypes.localhost' >> /etc/hosts
First time I tried this, I got this error:
root@android:/ # echo '10.0.2.2 prototypes.localhost' >> /etc/hosts /system/bin/sh: can't create /etc/hosts: Read-only file system
Then I found a great step by step tutorial on the subject from http://dillieodigital.wordpress.com. His post reminded me to check that my emulator exists, has been started up and is running properly via “./android list avd”, “./adb devices”, “./adb kill server”, “./adb start server” and “./adb -s emulator-5554 remount”.
I tried his “Option 1”, pulling the Emulator’s /etc/hosts file, editing it off my Desktop and pushing it back to the Emulator but it didn’t work:
# ./adb -s emulator-5554 pull /etc/hosts /Users/your.name/Desktop # ### edit hosts file ### # ./adb -s emulator-5554 push /Users/your.name/Desktop/hosts /etc/
“Option 2” from the above quoted tutorial worked fine for me, like so:
# ./adb -s emulator-5554 shell # echo '10.0.2.2 prototypes.localhost' >> /etc/hosts # exit
Now, if I ever get that “can’t create /etc/hosts: Read-only file system” error from the Emulator’s shell, I have to “exit” of the Emulator’s shell and remount again:
"./adb -s emulator-5554 remount"
Then, “Option 2” works fine for me again.