Some time, we will need to test web development in mobile devices.
Previously, I used to test in iOS simulator, the virtual host will just work.

E.g. http://myproject.test, by inputing this URL to simulator Safari, will just work fine.

Recently I tried to install AVD, unfortunately, the device (simulator) just don’t know about myproject.test.
In this case, need to edit the hosts file

First, find out which device you want to use

1
2
3
4
$ /path/to/android/sdk/platform-tools/adb devices
List of devices attached
emulator-5554 device
emulator-5556 device

Set the emulator to writable (nexus_5 here is the device name)

AVD create device

Choose x86 Images, then choose Android 7.1.1 (non Google APIs) (see reference below for more details)

1
2
3
4
5
6
$ /path/to/android/sdk/tools/emulator -writable-system -netdelay none -netspeed full -avd nexus_5
$ /path/to/android/sdk/platform-tools/adb -s emulator-5556 root
$ /path/to/android/sdk/platform-tools/adb -s emulator-5556 remount

# push /etc/hosts
$ /path/to/android/sdk/platform-tools/adb -s emulator-5556 push /local/path/to/hosts /system/etc/hosts

Open browser in android device, type in the URL http://myproject.test

References: