How to edit /etc/hosts file in Android Studio emulator running in nougat?
1) android-sdk-macosx/tools/emulator -avd <avdname> -writable-system
2) ./adb root
3) ./adb remount
4) ./adb push <local>/hosts /etc/hosts
Android file host can be
/etc/hosts <--- This worked for me
/etc/system/hosts
/system/etc/hosts
Check
1) ./adb shell
2) cat /etc/hosts
3) ping customsite.com
Here is how i was able to do it working on OSX. After reading a bunch of different instruction nothing seemd to work for me untill someone mentioned that you have a very narrow window for copying the file from your disk to the emulated device or it becomes read-only again
- Start your emulator.
- In your terminal find the folder "platform-tools" for your devices
- Prepare the hosts file you want to copy to your device (in my case i put it on desktop)
- String together a bunch of commands to copy the file quickly. This is what worked for me
./adb root && ./adb -s emulator-5554 remount && ./adb -s emulator-5554 push ~/Desktop/hosts /system/etc/hosts
'emulator-5554' is the name of my device which you can find by typing./adb devices
after that the terminal responded with
restarting adbd as root
remount succeeded
[100%] /system/etc/hosts
you can veryfy the copy was successfull by ./adb shell
and then cat /system/etc/hosts
I was then able to connect to my virtual hosts from the emulated device
Just to be complete here is how my hosts file looked like
10.0.2.2 my-virtual-host
I hope this helps someone as i spet quite some time trying to figure this out.
Below are the steps I followed on my Windows machine on Windows Terminal: Run the following command to know your AVDs:
emulator -list-avds
Run the following command to open the emulator for writable mode:
emulator -avd Pixel_XL_API_29 -writable-system -no-snapshot-load
Replace Pixel_XL_API_29 with your AVD name.
Ignore the warnings there.
In a new Terminal tab run the following commands:
- adb root
- adb shell avbctl disable-verification
- adb reboot
Wait for your emulator to reboot. It can take upto 1 minute.
When the emulator is rebooted, run the following commands:
- adb root
- adb remount
You will get a remount succeeded message after that:
Now is the time to push our host file from Windows machine to Android's emulator
adb push D:\hosts /system/etc/
D:\hosts is the location of the hosts file present at the D drive of my Windows machine. /system/etc/ is the location in Android's emulator where we want to copy that file.
After successfull operation you will see a message like this:
To verify that the hosts file has been pushed you can run the following commands:
- adb shell
- cd system
- cd etc
- cat hosts
You will see the contents of hosts file in the Terminal:
Step by Step
- Don’t Create the AVD with a Google Play image.
- Use for example Google APIs Intel x86 Atom System Image.
Start the emulator with the following command…
emulator.exe –avd <avd name> -writable-system
For example:
C:\Users\ilyas.mamun\AppData\Local\Android\Sdk\emulator>emulator.exe -avd Pixel_API_25 -writable-system
emulator: WARNING: System image is writable
HAX is working and emulator runs in fast virt mode.
audio: Failed to create voice `goldfish_audio_in'
qemu-system-i386.exe: warning: opening audio input failed
audio: Failed to create voice `adc'
Root and Remount the AVD like the followings…
C:\Users\ilyas.mamun\AppData\Local\Android\Sdk\platform-tools>adb root C:\Users\ilyas.mamun\AppData\Local\Android\Sdk\platform-tools>adb remount remount succeeded C:\Users\ilyas.mamun\AppData\Local\Android\Sdk\platform-tools>adb shell eneric_x86:/ # cd system generic_x86:/system # cd etc generic_x86:/system/etc # cat hosts 127.0.0.1 localhost ::1 ip6-localhost generic_x86:/system/etc # echo "192.168.1.120 ilyasmamun.blogspot.com" >> hosts generic_x86:/system/etc # cat hosts 127.0.0.1 localhost ::1 ip6-localhost 192.168.1.120 ilyasmamun.blogspot.com generic_x86:/system/etc #