3

I had to edit my emulator' hosts file in order to connect to a local development environment. I have followed this steps:

Run the emulator:

~/Library/Android/sdk/tools/emulator -writable-system -avd Nexus_5_API_19

In another terminal/cmd prompt:

adb reboot

adb root

adb -s emulator-5554 remount

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

add the IP to host file

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

adb reboot

Now whenever I try to start my emulator from AVD Manager it is not responding. I always have to start it with:

 ~/Library/Android/sdk/tools/emulator -writable-system -avd Nexus_5_API_19

I mention that I work from High Sierra.

Mention: I have tried with adb to make my emulator rw but it did not allow me to push my hosts file.

3 Answers 3

1

There is an other way to manipulate the /etc/hosts (You need a Linux machine )

Use the system.img which come from Google

go to your Android SDK home

  1. cd $ANDROID_HOME

find the right system.img for your emulator

  1. sudo mount -w system-images/android-25/google_apis/x86_64/system.img

edit the /etc/hosts

  1. sudo vi /mnt/etc/hosts
  2. sudo umount /mnt

Create an emulator (from Android Studio)

  1. ASD Manager
  2. → "Create Virtual Device"
  3. → Nexus 5X
  4. → Next
  5. → Tab x86 Images
  6. → Nougart - 25 - x86_64
  7. → Next
  8. → "AVD Name" : "Nexus 5X API 25 with WLAN hosts"
  9. → Finish

I have gone this way because emulator -writable-system -avd xx didn't work in my environment. Also mounting the system.img in my ~/.android/avd/xxxx directory didn't work anymore.

1

Step by Step

  1. Don’t Create the AVD with Google Play image.
  2. Use for example Google APIs Intel x86 Atom System Image.
  3. 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'
  1. 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.181     sandbox.dmoney.com.bd" >> hosts
    generic_x86:/system/etc # cat hosts
    
    127.0.0.1       localhost
    ::1             ip6-localhost
    192.168.1.181     sandbox.dmoney.com.bd
    generic_x86:/system/etc #
    
0

Perhaps the reason you can't remount adb as root is you are using a production emulator image rather that a debug image see: ADB root is not working on emulator

To overcome this try rooting your avd as outlined here, then install a hosts editor to easily modify the hosts file. Try rebooting as usual as you still need to append with -writable -system for changes to persist.

This problem occurs because emulator from android studio has been changed and partition /system is no longer writable between reboots.

An alternative workaround will be to replace the original files in <sdk_path>/system-images/android-xx/default/x86/system.img.

  • Copy the system.img: cp /home/a/Android/Sdk/system-images/android-xx/default/x86/system.img ~/.android/avd/[avdname].avd/system-qemu.img

  • Put the new system.img in .android/avd/avd name/. Rename it to system-qemu.img, thus the emulator will use this new image instead of the original one, and this won't your other emulators.

10
  • Sorry for the late response I was away in the past few days. I have trie to copy the system.img but I cannot seem to find it in /home/a/Android/Sdk/system-images/android-xx/default/x86/system.img Cannot seem to find system-images anywhere actually
    – Alphonse
    Feb 9, 2018 at 16:00
  • And I am not using a production image
    – Alphonse
    Feb 9, 2018 at 16:04
  • I have successfully copied system.img but it reseted my hosts file
    – Alphonse
    Feb 9, 2018 at 16:55
  • Sorry for late reply, did you update hosts after you root? Feb 9, 2018 at 16:58
  • 1
    yes I did, and afterwords I have copied the system.img, now I have pulled the hosts file from the emulator and it no longer contains my edits
    – Alphonse
    Feb 9, 2018 at 17:01

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .