问题
Anyone know how to edit /etc/hosts file inside an android studio emulator running in nougat? I will be editing it so I can use my virtual host in my local web server. I tried editing it through terminal using adb however, it is returning Read-only file system. Tried also using chmod but still it fails.
Update: I also tried pulling and pushing files using adb $ ./adb -s emulator-5554 push ~/Desktop/hosts /system/etc/hosts
adb: error: failed to copy '/Users/Christian/Desktop/hosts' to '/system/etc/hosts': couldn't create file: Read-only file system
回答1:
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
回答2:
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 #
回答3:
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.
回答4:
I was able to edit the /etc/hosts file by launching the emulator with -writable-system and remounting the emulator using adb remount. After that the hosts file inside the emulator is editable. I tried pushing/replacing the file and succeeded.
回答5:
Follow the below 3 steps :
- Start emulator in writable mode :
./emulator -avd <emulator_name> -writable-system
- remount :
adb remount
- push the hosts file attached :
adb push hosts /system/etc/
Note :
- Run one and only one emulator_name with above steps
- executable
emulator
is located within android-sdk. For me it was undersdk/emulator
. - Attached hosts file will resolve
www.facebook.com
to 127.0.0.1, hence blockswww.facebook.com
on emulator.
回答6:
You can use the ADB Shell to edit the file by changing the access (Read Only to RW)
回答7:
Try @P.O.W answer,
Make sure you have a blank line after the last entry of the hosts file If you use tabs in the hosts file, replace them with spaces Restart Android and try again:
adb reboot
回答8:
place all these export in z shell using terminal
vim ~/.zshrc press enter
then zshell will open
then press i
past all the export (verify the path i have used all default location for instalation)
then press esc
then press this :wq!
press enter
close terminal and open it again
export PATH="$PATH:$HOME/Dev/flutter/bin"
export GEM_HOME=$HOME/.gem
export PATH=$GEM_HOME/bin:$PATH
export ANDROID_HOME=$HOME/Library/Android/sdk
export ANDROID_SDK_ROOT=$HOME/Library/Android/sdk
export PATH=$PATH:$ANDROID_HOME/emulator
export PATH=$PATH:$ANDROID_HOME/tools
export PATH=$PATH:$ANDROID_HOME/tools/bin
export PATH=$PATH:$ANDROID_HOME/platform-tools
only use google apis image do not usese play image
u will get list of avds
emulator -list-avds
emulator -avd Nexus_5_API_29 -writable-system (do not close terminal) (open a new terminal)
adb root
adb remount
copy mac host file to Downloads from /private/etc/hosts
adb push Downloads/hosts /system/etc/hosts
adb reboot
来源:https://stackoverflow.com/questions/41117715/how-to-edit-etc-hosts-file-in-android-studio-emulator-running-in-nougat