androidreact-nativefetchhosts-file

Android emulator hosts file not working for android app


I have a react native app running on an android emulator and I'm trying to make a request to my local api(running on my laptop) at https://dev.mywebsite.com/api . This domain is host filed in the emulator like so :

/etc/hosts and /system/etc/hosts and /etc/system/hosts all have:

127.0.0.1       localhost
::1             ip6-localhost
10.0.2.2        dev.mywebsite.com

Note: I have also tried my local ip address of 192.168.1.13

The domain has a real ssl certificate and I'm able to access it from chrome on the emulator without issues.

Changing my api call to target https://mywebsite.com/api which is deployed in production does work.

Running adb shell and pinging dev.mywebsite.com shows 127.0.0.1 for some reason?

Why would chrome in the emulator work but my react native app doesn't? If you help me solve this I'll be so excited :) Maybe there's some workaround to this? I'll toss in a rep bounty.

EDIT

I got it working on a physical device(not emulator). I used an app called "Hosts Go" to host file to 192.168.1.13 and it works. Why does the Hosts Go app work but the emulator hosts file doesn't? I wasn't able to edit the hosts file on my physical device as it's not rooted to test that.


Solution

  • Well if this isn't on the trickier problems to debug...

    Put a blank line at the end of your hosts file.

    Chrome for some reason works if your hosts file doesn't have it, but a request from your app won't.