Android Emulator — add hosts file

Peter
3 min readJan 25, 2020

During mobile development, you’ll need to access some externally hosted API (backend) to get the necessary information for your App.

When you create an Android Emulator it will start as a brand new independent device, so you won’t be able to access your local server using your local domain from your emulator.

Using your IP address is a solution when you don’t have virtualhost(s) configured that you use to access several websites within your server.

When you have virtualhost(s) configured you need to teach your emulated android device to know your host(s).

For example, I use VAGRANT to serve my Laravel project, I can access it with `https://vagrant.project1.com/` from my laptop’s browser

When I go to my emulator open Chrome and open `https://vagrant.project1.com/`I’ve got an error about vagrant.project1.com host doesn’t exist (the browser can’t find the belonging IP address of the host).

hosts file

We can tell our OS what’s the belonging IP address to our custom/local host.

My VAGRANT server’s IP address is 192.168.10.11

The project’s virtualhost is: vagrant.project1.com

So, using your hosts file you can add this line:

192.168.10.11 vagrant.project1.com

After rebooting your Android emulator you’ll be able to access your custom domain.

How can I add this to the hosts file?

Important: You need to root your device and you need make your filesystem writable to change your hosts file.

Please, open Android Studio -> Tools -> AVD Manager and check if you have added any devices before or you need to create a new one.

Be careful with creating:

You can’t root a device with Play Store installed on it!

So, please, before you’d go to the next step, make sure your device has been created without the Play Store. You can see a Play Store icon in the Play Store column in the list of your AVD Manager.

Please, start your device.

On Windows you can start your device from Android Studio -> Tools -> AVD Manager (this should be open currently if you followed my instructions well).

If you’d prefer starting your device from command line (CMD) you can use this:

%ANDROID_HOME%\emulator\emulator -avd NAME_OF_YOUR_DEVICE -writable-system

The -writable-system parameter is very important!

When your emulator is ready (booted) open the command line (Start -> Run -> CMD) and run these commands:

adb root
adb remount
md c:\temp
adb pull /system/etc/hosts c:\temp\hosts
echo 192.168.10.11 vagrant.project1.com >> c:\temp\hosts
adb push c:\temp\hosts /system/etc/hosts
adb reboot

After these, your device should reboot and if you open vagrant.project1.com in your browser or you ping it from your device it should resolve the 192.168.10.11 IP address.

Explanation

If you’re interested in, here’s some explanation what we’ve done technically.

adb root — rooting your device because you’d like to change a system file

adb remount — since we’re root (admins) we need to remount the filesystem based on our new power to get the write-access (God Mode) on your device system files and folder.

md c:\temp — create a temporary folder to download the original hosts file from the device

adb pull /system/etc/hosts c:\temp\hosts — download the hosts file to our computer because we want to modify it

echo 192.168.10.11 vagrant.project1.com >> c:\temp\hosts — add the necessary line to the hosts file

adb push c:\temp\hosts /system/etc/hosts — Upload the modified hosts file to the device

adb reboot — reboot your device to activate the changes in the hosts file

I highlighted those parts that you might change/can be different on your side.

Bonus shell script

I’ve created a very low-key basic shell-script to boot my device and update the hosts file (be careful this is going to add the same line to your device’s hosts file every time that you run it).

Works on Windows only (with small changes it’s OK on Linux too)

START %ANDROID_HOME%\emulator\emulator -avd P3_root -writable-system

cd /d %ANDROID_HOME%\emulator
echo “Waiting emulator is ready…”

adb wait-for-device shell ‘while [[ -z $(getprop sys.boot_completed) ]]; do sleep 1; done; input keyevent 82’

echo “Emulator is ready!”

adb root
adb remount
md c:\temp
adb pull /system/etc/hosts c:\temp\hosts
echo 192.168.10.11 vagrant.mtp.travel >> c:\temp\hosts
adb push c:\temp\hosts /system/etc/hosts
adb reboot

--

--