Reputation: 123
I need to configure hosts file in emulator with sdk 30 or higher. To do this I follow next steps from my command line:
My hosts files is for example:
127.0.0.1 localhost
::1 ip6-localhost
192.168.1.1 testhost.loc
Once I reboot emulator I do adb shell cat /system/etc/hosts to check that changes are made and all is ok. This command print:
127.0.0.1 localhost
::1 ip6-localhost
192.168.1.1 testhost.loc
Oh right, all is working.
Now I shutdown emulator and I start it from Android Studio with Virtual Devices Manager. If I execute adb shell cat /system/etc/hosts the command prints:
127.0.0.1 localhost
::1 ip6-localhost
What???
Finally I shutdown again emulator and run with command emulator -avd Pixel_4a_API_34 -writable-system and executing again adb shell cat /system/etc/hosts it prints:
127.0.0.1 localhost
::1 ip6-localhost
192.168.1.1 testhost.loc
In summary, if I run from Android Studio, hosts file prints as default but if I run with emulator command with option -writable-system, then I have my hosts file. I do too a test running emulator with emulator -avd Pixel_4a_API_34 (without -writable-system) and the result is the same than running from vmd in android studio.
This doesn't happen with sdk previous to 30. With previos sdk you do changes in hosts file and if you do a cat it show right content whether you boot from android studio or from the console.
Someone can explain me what is happening and if it can be resolve?
For me there is no problem to run with command and option writable-system but I would like to understand what is happening.
Thanks.
Upvotes: 1
Views: 510
Reputation: 69189
If you want to run an emulator with a writable system from Android Studio, you can save emulator
as emulator.ORIG
and in the same directory create an executable shell script, named emulator
:
#! /bin/bash
exec "$0.ORIG" -writable-system "$@"
This works on macOS and linux.
You could do a more sophisticated processing or command line options if you want.
Upvotes: 1