Saturday, August 22, 2009

Viewing local websites in the Android emulator

I've been working on a mobile UI for a website. I'm using iUI to make the site work well in Mobile Safari on the iPhone and iPod touch. iUI should work well for the browser on Android, too, since it's also WebKit-based. With that in mind, I downloaded the Android SDK and Eclipse, created an Android Virtual Device (AVD), and launched the emulator.

Unlike the iPhone Simulator (included with the iPhone SDK), the Android emulator just wouldn't load my site by the name I was using locally. Strangely, 127.0.0.1 wouldn't work either, not even with a port number. Frustratingly, I entered my local IP address and a port number in the browser, and I was able to view my site on the emulator.

This morning, some searching revealed that many people hit this same wall. What I found is that localhost and 127.0.0.1 in the Android emulator refer to its own loopback interfaces. To browse to websites on your machine, use 10.0.2.2 instead. Here's the official word from the Android Developer's Guide.