Despite reading all the answers here and elsewhere, I have lost several hours trying to debug this issue, as the address 10.0.2.2
did not work, even in Chrome browser. If the same is happening to you, here is a step-by-step guide to try to debug and hopefully fix your issue.
Check emulator gateway is 10.0.2.2
Inside the emulated Android, go to Settings > WiFi, check if it is connected to AndroidWiFi hotspot (which represents your host computer), and then click on Advanced at the bottom, then check the Gateway address: it should point to 10.0.2.2 . If not, then you have another issue, maybe changing proxy settings can fix your issue, see here how to do that with Android Studio since 2022, as the proxy setting is now hidden away: How to configure proxy in emulators in new versions of Android Studio?
Check if your server is accessible from your host computer
Simply open a web browser and type http://localhost:<port>
to see if your local web app is accessible. If not, then you likely have an issue with your local server parameters.
Check if your server is accessible from the emulator
Open Chrome browser, and point it to http://10.0.2.2:<port>
(for genymotion, replace with http://10.0.3.2:<port>
). If your web app shows up, great, you're done. If not, then test the other steps below to pinpoint the root issue.
Test with another server
In case your web app can be accessed from your host computer, but not inside the emulator, the root cause can be that your local server is restricting access to some interfaces for some reason, likely for security reasons.
To check this, try to use another server, just a simple HTTP server will do, such as http-server
with nodejs
, or python -m http.server 8000
with Python 3.
Then, try to access this simple server from your emulator's Chrome browser, eg, http://10.0.2.2:8000
. If it works, then this confirms that your local server is restricting access to some interfaces. You need to read your local server's documentation to broaden permissions.
For example, in my case, my server was angular-cli
(AngularJS), which by default restricts serving only to localhost. To make it work, I had to use ng serve --disable-host-check --host 0.0.0.0
instead of just ng serve
, as suggested in this other question. The --host 0.0.0.0 instructs the webserver to serve all interfaces. Similar arguments can be provided to most webservers.
An alternative might be to disable some unused adapters, especially virtual ones such as VPNs.
Your Android app permissions to cleartext
Now, your web app should be accessible from inside the emulator, using Chrome app, with the URL http://10.0.2.2:<port>
. The last piece of the puzzle is to add permissions in your Android app to access 10.0.2.2 and especially cleartext if your local webserver is not equipped with a SSL certificate (the most likely scenario for a local development webserver - just check if https://localhost:<port>
works or only http://localhost:<port>
from the host computer). This will allow your Android app to access your local webserver, just like Chrome does.
Adding specific permissions to access cleartext (ie, http://
) from your Android app is necessary since Android 9 (API 28) upwards. There are several ways to configure your Android app to add this permission, see: https://mcmap.net/q/35675/-android-8-cleartext-http-traffic-not-permitted
Conclusion
Accessing the host from the Android emulator can be tricky, but by careful step-by-step debugging, it can be possible to overcome the issue in most cases.
This tutorial only covers the issue of accessing/reaching a local webserver on the host computer from inside an Android emulator, but once this is fixed, the webapp may remain dysfunctional, even if reachable. One example is to experience an infinite loading loop. To debug further issues once reachability is resolved, you can use chrome://inspect
in the Chrome Browser to attach to the Android WebView inside the Android emulator and live debug it as if it was rendered on your computer.
A last alternative, probably faster, is to get a paid subscription to services such as ngrok, but the free version is useless as they necessarily open the webapp in a web browser, outside of your Android app's webview.