Helper Script: Hostfile on Android Virtual Device
June 2, 2015 9:29 pm Leave your thoughtsThese 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.
- Download the android standalone SDK from here if you haven’t
- Copy and paste or download the helper script from here
- Place the helper script in the root directory of the sdk path. As an example: /Users/yourname/android-sdk-macosx/avd_mount_hostfile.sh
- Run the script by supplying the -h parameter. Eg. ./avd_mount_hostfile.sh -h /etc/hosts
- The script will then show the list of possible virtual devices.
- 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)
- The script will delay for the android device start up. It’ll then list the virtual devices that are running on your system.
- Simply enter the device name and press enter. Eg. emulator-5554
- 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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
#!/usr/bin/env bash while getopts ":b:t:s:V:v:p:n:h:N:" opt; do case $opt in h) echo "Host file target is $OPTARG" >&2 TARGET_HOSTFILE=$OPTARG ;; \?) echo "Invalid option: -$OPTARG" >&2 PROCEED_BUILD=0 ;; esac done if [ -z "$TARGET_HOSTFILE" ] then echo "You need to supply the hostfile. Use the -h parameter" exit fi ./tools/android list avd echo "Enter the name of the android virtual device that you want to use" read avd_selection echo "Selected AVD is" $avd_selection if [ -z "$avd_selection" ] then echo "No avd selected, opening dialog for android avd creation" ./tools/android echo "Enter the name of the android virtual device that you want to use" read avd_selection fi ./tools/emulator -avd $avd_selection -partition-size 512 & echo "Delaying for virtual device boot up" sleep 30s ./platform-tools/adb devices echo "Enter the adb device name. (eg. emulator-5554)" read adb_device_name echo "Selected adb device name is" $adb_device_name ./platform-tools/adb -s "$adb_device_name" remount ./platform-tools/adb -s "$adb_device_name" pull /etc/hosts cat $TARGET_HOSTFILE > ./hosts ./platform-tools/adb -s "$adb_device_name" push hosts /etc/hosts echo "Hostfile has been injected to your android virtual device" |
Tags: Android, android virtual device, avd, hostfile, web application
Categorised in: Android, Coding, Tools