5

I try to edit android emulator's host file.I follow steps:

step1
adb root
step2
adb remount
step3
adb pull /system/etc/hosts .

then I add new line for customer host information,then I push hosts file,I get some errors:

   step4
   adb push hosts /system/etc/hosts

error information:

adb: error: failed to copy 'hosts' to '/system/etc/hosts': couldn't create file: Read-only file system

envirements information:

   osx ei capitan  
   android emulator: android 7.1.1  
   android studio: 2.2.2  
haibinpark
  • 63
  • 1
  • 5

3 Answers3

31

I just ran into the same problem and made it work.

Run the emulator:

emulator -avd <EMULATORNAME> -partition-size 512 -writable-system

I believe that -writable-system param is the thing that fixed it for me.

In another terminal/cmd prompt:

adb root

adb -s emulator-5554 remount

adb -s emulator-5554 pull /system/etc/hosts hosts

Now, work your magic (add the IP to host file)

adb -s emulator-5554 push hosts /system/etc/hosts

Additional info:

  • emulator-5554 is the device name, you can find yours with adb devices
  • emulator program is located inside android-sdk/tools
  • adb program is located in android-sdk/platform-tools

Important update from comments:

Please check the comment from steven to this post about running adb reboot before running adb root. This might be important to being able to start the emulator from AVD manager.

Captain suggests to run adb reboot after pushing the host file to avoid blackscreen error.

Happy debugging :)

JoeRoot
  • 474
  • 5
  • 6
  • 1
    Now in ubuntu change to ./emulator -avd -partition-size 512 -writable-system – Jackson Chengalai May 25 '17 at 12:29
  • Just a warning that if you use this (working) method, you'll have to run this emulator with the writable-system param every time or the emulator screen will be black. – airowe Aug 04 '17 at 12:27
  • Please add the `adb reboot` to your answer! it is very important. Otherwise you could not start the emulator from avd manager any more! – steven Dec 22 '17 at 20:50
3

After pushing the hosts file run adb reboot and you will have no problem with the black screens anymore.

Captain
  • 31
  • 1
  • this answer is really important! You will get an `qemu-system-i368.exe has stopped working` crash in avd-manager if you don't reboot after starting in -writable-system mode. I would give you 10 upvotes if i could. – steven Dec 22 '17 at 20:48
2

I had problems with the other solutions, so i tested a lot and now i decided to write another answer and i hope it is useful for others.

Here are my steps to success (Android Studio on win 10):

  • Start Android Studio and open the AVD Manager and start your Emulator.
  • Open a powershell and navigate to your android sdk/platform-tools/
  • .\adb.exe reboot -writable-system (your emulator should reboot now)
  • .\adb.exe root
  • .\adb.exe remount
  • .\adb.exe pull /system/etc/hosts C:\Temp\hosts
  • Èdit your hosts file
  • .\adb.exe push C:\Temp\hosts /system/etc/hosts
  • .\adb.exe unroot
  • Now you can open a browser in your emulator and open your custom domain to check if your hosts file has been changed.
  • Click the close-icon of your emulator to force a shutdown with snapshot saving. You should see a "Saving state..." message.
  • Now start your emulator again from AVD Manager. Your hosts file should persist. So check your domain again.

I had very strange issues with unstartable emulators after booting in writable-system mode, rooting and stopping emulator without doing the unroot. The problem is: whenever the snapshot fails the emulator will do a cold boot and your hosts file is gone. You need to repeat all the steps in that case.

steven
  • 4,868
  • 2
  • 28
  • 58