Editing your /etc/hosts file is great for running and testing web apps as they are under development. For a project I’m working on, I needed to be able to access one of my local Rails apps through an Android device, which meant editing the device’s /etc/hosts file. As I haven’t rooted my Nexus-S, so opted to use an emulated Android Virtual Device (AVD), as they are configured by default to allow root access.
I started by creating a simple hosts file on my desktop. It’s this file that I would push to the AVD
cd Desktop vim hosts # or your favourite text editor....
Use your text editor to create a simple hosts file pointing to your local web app. Remember to also include a localhost entry:
# hosts 127.0.0.1 localhost 192.168.1.10 rails-app
Next, copy your new hosts file to the AVD:
adb push hosts /etc/hosts
Despite having root access, though, adb wouldn’t let me push the new hosts file because the system partition is mounted as read-only. You can remount the system partition in read-write mode using adb remount, and then try pushing the hosts file up to the device:
adb remount abd push hosts /etc/hosts
I kept getting an Out of Memory error, which seems to be a common problem. The solution (gleaned from scattered forum posts) is to use the emulator’s -partition-size option.
Using this option means you won’t be able to launch the AVD directly from Eclipse, but instead need to use the command line. Close down any running AVDs, and then run, and then re-launch the AVD passing in the -partition-size option with a reasonable value:
emulator -avd Samsung_Galaxy-Tab -partition-size 128 # ...wait the avd to boot up... adb remount abd push hosts /etc/hosts
The hosts file should now successfully be copied to your AVD. If you launch the AVD’s browser and enter the local URL from your hosts file (e.g. http://rails-app:3000/), the AVD will connect to the local IP address you specified.
Note: You’ll need to keep hold of your hosts file as your settings will be wiped when you shut down the AVD. You can reinstall the file next time by performing the adb remount; adb push hosts /etc/hosts script each time you boot the AVD.