DISCOVER
X

Helper Script: Hostfile on Android Virtual Device

June 2, 2015 9:29 pm Leave your thoughts
FacebooktwitterredditpinterestlinkedinmailFacebooktwitterredditpinterestlinkedinmail

These days there is no avoiding testing your web applications on multiple devices in order to gain the best user experience possible. However it’s not normally (financially) feasible to buy every single iOS and Android devices, therefore the next best way to test is using virtual devices. The good news is if you are on a Mac you can easily download XCode suite and you’ll be able to emulate iOS devices, while for Android device emulation you can use the Android SDK pack.

The problem with Android virtual device is that it doesn’t automatically read the host file from your Mac, which presents a problem, especially if your web application sits on a virtual host which relies on local host files. Thankfully, there is a way to re-write the host file within the virtual device, but the way to do it is not straightforward. Basically you need to start an android device with writeable storage and re-write the host file within the device. To add to the pain, the change is not persisted after you destroy the device, so you’ll need to do the manual steps over and over again.

I have written a quick shell script to help make our lives easier. Check out the script below, you can copy and paste this into a physical file within the android sdk directory and run this if you want to start a virtual device with a custom host file.

Virtual devicesHere’s a quick instructions:

  1. Download the android standalone SDK from here if you haven’t
  2. Copy and paste or download the helper script from here
  3. Place the helper script in the root directory of the sdk path. As an example: /Users/yourname/android-sdk-macosx/avd_mount_hostfile.sh
  4. Run the script by supplying the -h parameter. Eg. ./avd_mount_hostfile.sh -h /etc/hosts
  5. The script will then show the list of possible virtual devices.
  6. Enter the name of the device that you want to use the press enter. (If nothing is entered it’ll start android virtual device manager so you can create one)
  7. The script will delay for the android device start up. It’ll then list the virtual devices that are running on your system.
  8. Simply enter the device name and press enter. Eg. emulator-5554
  9. Your emulator now ready with the custom host file loaded

If you have some issues with the last part where the device listing is not displayed properly, simply add more delay to the script.

I know the script is quite rough, but hopefully it’s useful for you all. I put this real quick as well for development testing purposes.

Here is the full code at the time this post was written:

 

FacebooktwitterredditpinterestlinkedinmailFacebooktwitterredditpinterestlinkedinmail
Tags: , , , ,

Categorised in: , ,