Skip to content

Instantly share code, notes, and snippets.

@twaddington
Last active April 20, 2024 17:26
Show Gist options
  • Star 38 You must be signed in to star a gist
  • Fork 12 You must be signed in to fork a gist
  • 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

@twaddington
Copy link
Author

Screen Shot 2020-03-24 at 1 21 04 PM

@twaddington
Copy link
Author

Screenshot_1585082949

@dtbarne
Copy link

dtbarne commented Apr 26, 2021

I had to install the root certificate in windows and my browser and then restart before everything worked.

@DanteAndroid
Copy link

After I enable the APN, the emulator cannot access Internet any more

@rveramx
Copy link

rveramx commented Jan 14, 2022

I don't even have that option on the emulator settings, does anybody know how to get to it?

I'm using Android Studio Bumblebee | 2021.1.1 Beta 5

Screen Shot 2022-01-14 at 3 47 29 PM

@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.

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