How to connect an Android emulator to a proxy

10.0.2.2 instead of localhost, three ways to set the proxy, and the certificate in the system store. And why the emulator can lose its internet afterwards.

Not localhost but 10.0.2.2

Inside the emulator 127.0.0.1 is the emulator itself. The computer is 10.0.2.2. A proxy of 127.0.0.1:7070 in Android's settings points the emulator at itself and leaves it with no network.

One exception: the proxy in Extended controls is applied on the computer's side, so 127.0.0.1 is right there.

Three ways to set the proxy

Extended controls. Toolbar … → Settings → Proxy. Clear Use Android Studio HTTP proxy settings, choose Manual proxy configuration, enter 127.0.0.1 and the proxy port, press Apply.

adb, on a running emulator:

adb shell settings put global http_proxy 10.0.2.2:7070

At launch:

emulator -avd Pixel_8_API_33 -http-proxy http://127.0.0.1:7070

Restart the app you debug: running apps do not always pick the proxy up.

The certificate

Without the proxy's root certificate you see only failed HTTPS connections. Two stores:

User. Drag the certificate file onto the emulator window, then Settings → Security → Encryption & credentials → Install a certificate → CA certificate. Chrome trusts this store; apps targeting Android 7+ do not without a network_security_config, see Android does not trust the proxy certificate.

System. Trusted by every app. Writable only on a Google APIs image, not Google Play (pick it in Device Manager). Start with write access and add the certificate under its hash name:

emulator -avd Pixel_8_API_33 -writable-system

# in another terminal
HASH=$(openssl x509 -inform PEM -subject_hash_old -in proxy-ca.pem | head -1)
cp proxy-ca.pem "$HASH.0"
adb root
adb remount
adb push "$HASH.0" /system/etc/security/cacerts/
adb shell chmod 644 /system/etc/security/cacerts/$HASH.0
adb reboot

If adb remount asks for a reboot, run adb reboot, then adb root and adb remount again. On Android 14+ system certificates live in an APEX module and copying into /system has no effect. Use an image with API 33 or lower.

If the emulator has no internet

The proxy is closed but the setting stayed. The adb global proxy survives emulator reboots until removed. With the proxy closed, requests go nowhere. Remove it:

adb shell settings put global http_proxy :0

127.0.0.1 in Android's settings. Use 10.0.2.2, see above.

Wrong port. Compare with the port the proxy shows.

No certificate. http:// pages open, https:// ones do not; apps report no connection.

A VPN on the computer. Some VPN clients break the emulator's network. Test with the VPN off. See proxies and VPNs.

See it on your own traffic

In Solpuga, SSL Certificate → Install certificate on Android Emulator does all of this. It needs the Android SDK with ADB and a running emulator. The same wizard removes the proxy and the certificate. Details: Mobile Devices.

View in Solpuga

Related