Skip to content

Instantly share code, notes, and snippets.

@twaddington
Last active November 10, 2024 10:19
Show Gist options
  • Save twaddington/54eda4951fd8d2e858b537bef5f22334 to your computer and use it in GitHub Desktop.
Save twaddington/54eda4951fd8d2e858b537bef5f22334 to your computer and use it in GitHub Desktop.
How to set up Charles Proxy for the Android Emulator.

Charles Proxy Android

Note: Consider using mitmproxy instead of Charles. There is better, more recent, documentation for using mitmproxy on Android.

Steps

1. Add the Network Security Configuration to your app

<network-security-config>
   <debug-overrides>
       <trust-anchors>
           <!-- Trust user added CAs while debuggable only -->
           <certificates src="user" />
       </trust-anchors>
   </debug-overrides>
</network-security-config>

2. Update the Android Manifest

<?xml version="1.0" encoding="utf-8"?>
<manifest ... >
    <application ...
                 android:networkSecurityConfig="@xml/network_security_config"
                 ... >
        ...
    </application>
</manifest>

3. Update the Emulator proxy

  • Launch the emulator and open "Settings -> Proxy"
  • Uncheck "Use Android Studio HTTP proxy settings"
  • Check "Manual proxy configuration"
  • Update the "Hostname" to "127.0.0.1" and "Port number" to "8888"
  • Click "Apply"

Your settings should now look like this.

4. Install the Charles certificate

  • Launch Charles and enable SSL proxying.
  • In your emulator, open a web browser and navigate to https://chls.pro/ssl
  • You should be prompted to download a cert file.
  • Save the file with any name.

5. Create custom APN

Charles should now be proxying requests but instead of hostnames you're going to see IP addresses. This makes it difficult to filter on specific hosts. To fix this you'll need to:

  • In Android, navigate to "Settings -> Network & Internet -> Mobile network -> (Advanced) Access point names"
  • Tap the "Plus" icon in the toolbar to add a new APN.
  • Use any name, set APN to "http://", proxy to "10.0.2.2", and port to "8888"
  • Tap the overflow menu in the upper right and then "Save"
  • Make sure to mark your new APN as active
  • Now you may need to toggle airplane mode on and off a few times to get it to take.

You should now see hostnames correctly in Charles. If not, try restarting the proxy a few times.

Notes

  • You may also have to enable the "Proxy -> macOS Proxy" setting in Charles.
  • You may have to disable WiFi in the emulator to see hostnames in Charles.

References

@Florian14
Copy link

@rveramx I think you are using the embedded emulator. I resolved it by detaching it from the IDE by unchecking Launch in a tool window in AS -> Preferences -> Tools -> Emulator. Then you will have more settings for the emulator.

@bamapookie
Copy link

Thank you so much for this! I was missing the APN step, which I have seen documented nowhere else but here. You win the internet for today.

@Dast1Woop
Copy link

It seems that '3. Update the Emulator proxy' is not necessary!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment