The other day I was trying to fix an issue that was reported by a user of an older Android device. I am not an Android user, and do not have an actual Android device to test with. So, I thought I’d try using the Android Emulator to reproduce the issue and verify my fix.
These instructions assume you are using Mac OS X, but should be easy to follow with minimal changes if you are on another platform.
Install Android Studio
If you are only interested in using the Android emulator, you could skip the full Android Studio installation and just install the Android SDK Tools. But, the Android Virtual Device (AVD) Manager that comes with Android Studio looks a bit more polished than the core SDK version, so I’d recommend installing the full Android Studio.
Android Studio Download Page
Create a Dummy Project in Android Studio
I couldn’t find a way to access the AVD Manager in Android Studio without having a project open. So, create a dummy project that you can open whenever you want to access the AVD Manager. It really doesn’t matter what you set. I just clicked through with the defaults for everything.
Setup Android Virtual Devices
Open your dummy project in Android Studio. Then, go to the Tools > Android > AVD Manager menu item. This will launch the AVD Manager.
Depending on what you selected when you installed Android Studio, you may have an AVD already configured. If you don’t, or you need to create a customized AVD, click the + Create Virtual Device… button.
Launch AVD in the Emulator
Start your emulator from the command line by doing the following:
$ ~/Library/Android/sdk/tools/emulator -avd avdname -partition-size 512
We are launching it from the command line so that we can set a partition size. This is necessary to avoid an ‘out of memory’ error when we set the
/etc/hosts file below.
It may take a while to launch, especially if it is the first time you are launching the AVD. Eventually the emulator will come up with your AVD booted in it.
Set /etc/hosts on AVD
So, you now have an AVD that you can use to access the web using Android’s web browser. One potential small problem… you may be developing on a local web server and have modified your local
/etc/hosts file to point your hostname to your local development machine.
Example /etc/hosts file:
So, we need to modify the
/etc/hosts file on the AVD to use the same hostname mappings.
I found a good article on setting the /etc/hosts file on an Android Emulator using a Mac. Below is a quick summary.
Check which AVDs are running
$ ~/Library/Android/sdk/platform-tools/adb devices
List of devices attached
Remount the AVD to make it writable
$ ~/Library/Android/sdk/platform-tools/adb -s emulator-5554 remount
Save the current /etc/hosts file from the AVD
$ ~/Library/Android/sdk/platform-tools/adb -s emulator-5554 pull /system/etc/hosts ~/Desktop/
17 KB/s (56 bytes in 0.003s)
Make your changes to the hosts file and then save it back to the AVD
$ ~/Library/Android/sdk/platform-tools/adb -s emulator-5554 push ~/Desktop/hosts /system/etc/hosts
Now you can use the same hostname mappings you are used to on your local development machine from your AVD.
Note: You will need to push the
hosts file changes to the AVD every time you launch it. So, save your modified
hosts file somewhere for future use.