I’ve been working on a mobile project recently, using Xamarin, Android/iOS emulators and Visual Studio 2017.
In my local environment (Windows 10), my emulators need to be able to communicate with my machine to access services, using machine name. By default, this is not possible. You will notice that hitting a url, with your machine name in it, from your emulator, does not properly resolve, and this is due to the default hosts file on an Android emulator, and the way it treats routing to 127.0.0.1.
Here are the steps I was able to take, on Windows 10, in order to hit my machine via machine name, from my android emulator, on my local network.
1) Open two separate administrator Visual Studio developer command prompts.
2) In the first prompt, CD to C:\Program Files (x86)\Android\android-sdk\tools (this path may be slightly different on your machine, but should be close enough to figure out the correct path)
3) Then run: emulator -avd YourEmulatorNameHere -writable-system (filling in your emulator name in place of YourEmulatorNameHere)
This opens your emulator, and puts it into a writable mode, which is needed to push a modified hosts file to it.
4) Now from your 2nd command prompt:
5) CD to C:\Program Files (x86)\Android\android-sdk\platform-tools (this path may be slightly different on your machine, but should be close enough to figure out the correct path)
6) Run: adb remount
7) Run: adb pull /system/etc/hosts c:\temp
This will pull a copy of the emulator’s host file to c:\temp\hosts
8) Open the pulled hosts file and modify it to be:
127.0.0.1 localhost
10.0.2.2 yourMachineNameHere
*Note, be sure to include one empty line after your last line in your Hosts file.
9) Run: adb push c:\temp\hosts /system/etc
*If the changes are correct, they should take affect immediately, without the need to restart the emulator
Now, with any luck, on your emulator you should be able to hit your machine via a machine name url.
I hope this helps someone else having these issues. Good luck!
This is an old post, but just wanted to let you know that someone still found it incredibly helpful :)
Awesome, that’s exactly why it’s there!
a semi-good solution. because a normal restart of the emulator from VS 2019 will reset the hosts file for an unknown reason :-(
Very helpful. Why is this not part of Android SDK documentation. Only couple of notes
1) The path for emulator (first command) is now ……/Android/Sdk/emulator/emulator
2) Had to run adb root command before adb remount (second command).
I’m glad it was helpful. Thanks for leaving a comment, and for the updates!