The Android Emulator and Charles Proxy: A Love Story – Mark …
Android Emulators have come a long way over the years. Once they were the bane of every developer’s life, representing the most basic failure of the Android development environment; a painfully slow tool that had the maddeningly unrealized potential to make developer lives infinitely easier. But over the last couple years, Google has wised-up and made the emulator one of the best things about developing for Android — a fast, fluid, and endlessly useful tool for exercising and debugging Android Emulator can also be made into a super-tool for testing API edge and failure cases by employing an proxy — a go between that can intercept, display and even re-map your API calls. My personal choice for this job is Charles Proxy. It has slick interface, easy to use mapping options and great support, with new releases being published all the time. Today I’ll show you how to set up your Emulator to proxy calls, and later, what you can do with CharlesThe first thing you want to do is download and run Charles Proxy. Start it up and you’ll see all the network traffic from your machine start to show up in the side bar. Charles, by default, has local proxying turned on, but since we’re going to use it to proxy an emulator, you can go to the Proxy menu and uncheck the OS proxy option:Then click the little yellow broom icon to clear out all the traffic. You can also modify Charles’ preferences so that this option is not enabled by your Emulator to ProxyNext, start up your emulator. Any old emulator will do, but depending on the Android version, your emulator’s settings might be different then mine. For this tutorial, I’m using a Pixel XL emulator running running Android 8. 0, Oreo. The first thing we need to do is set up the emulator’s proxy settings, so that all the network traffic goes through Charles. To do that, we need your machine’s IP address. Charles actually has a little utility in the Help menu for this:Click that, and then copy the IP address from the dialog that comes, in your emulator, in the Setting bar, click the three little dots:And then in the resulting dialog, click Settings, and then select the Proxy in your IP address, and set the port to 8888. Charles will now prompt you that a device is trying to connect to the proxy, and ask you to allow it. Just click allow and you’ll start seeing IP addresses show up in the side barDon’t worry, we’re going to make that a whole lot stall the Charles SSL CertCharles does some specific SSL magic, which we won’t go over here, but the bottom line it that you need to install the Charles SSL Certificate on your emulator. To do that, just open up a browser on the emulator, and navigate to. The browser may prompt you to allow it to save the cert to disk, and then you can click to install Proxy your application on Android N or above, you also need to install a config file in your app’s resources. From the Charles Website:“AndroidAs of Android N, you need to add configuration to your app in order to have it trust the SSL certificates generated by Charles SSL Proxying. This means that you can only use SSL Proxying with apps that you order to configure your app to trust Charles, you need to add a Network Security Configuration File to your app. This file can override the system default, enabling your app to trust user installed CA certificates (e. g. the Charles Root Certificate). You can specify that this only applies in debug builds of your application, so that production builds use the default trust a file res/xml/ to your app:
How to set up Charles Proxy for the Android Emulator. – gists …
How to set up Charles Proxy for the Android Emulator.
Steps
1. Add the Network Security Configuration to your app
2. Update the Android Manifest
xml version="1. 0" encoding="utf-8"? >
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. 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 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 “”, proxy to “10. 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
Setting Up Charles proxy for the Google Mobile Ads SDK on …
On Android N or higher, ad calls are visible in Charles proxy only when the
following steps are performed:
Update the Google Play services on the mobile device.
Enable Network Tracing on the mobile device.
Install Charles SSL certificate on your device, and set up proxy.
Enable SSL Proxy for your mobile app.
Updating the Google Play services on the mobile device
For an emulator, if you don’t see the Google Play section in the side menu,
you may need to update the Android Studio and download the latest system
images in the Virtual Device Configuration. Make sure to use a system
image with Google Play in its target.
Use the side menu to check and update Google Play services to the latest
version. The minimum working version of the Google Play services is
14. 5. 74.
For a physical mobile device (phone or tablet), you can search for Google
Play or follow this
link
to open Google Play directly.
Enabling Network Tracing on the mobile device
To enable network tracing, you will need to enable developer
options for your
device. Launch the Google Settings app and select Google > Ads > Enable
debug logging for ads. A more detailed
guide is
available for both AdMob and Google Ad Manager publishers.
Installing Charles SSL certificate on your device, and set up proxy
To use Charles as a proxy for your mobile app, you will need to download and
install Charles
on a computer. Follow Charles’ instructions to install an SSL certificate on
the Android emulator or mobile device.
It is simpler to use the emulator with a
proxy because the
emulator is already connected to the same wifi network with the computer running
Charles. When using the emulator with a proxy, set the proxy to localhost
() and the port that Charles proxy is running on (found in
Charles menu option Proxy > Proxy Settings).
If you’re using a physical mobile device (phone or tablet), you’ll need to
connect the mobile device to the same wifi network with your computer
running Charles using the advanced network
settings. When setting up the
proxy settings for your physical device, use the Charles menu option Help >
Local IP address to get the IP address of your computer, to enter for the proxy
address on your device (you must be on the same wifi network for this to work).
Use the port that Charles proxy is running on.
Enabling SSL Proxy for your mobile app
For Charles to intercept your mobile app’s SSL traffic, you will need to declare
that your app can trust a user-provided SSL certificate.
First, you will need to add a new XML resource file for Network Security
Configuration under
the path res/xml/
Next, update the file to use the network security
configuration.
xml version="1. 0" encoding="utf-8"? >
After that, you can launch the mobile app and look for ad requests in the
Charles log.
If you still have questions or need a one-on-one consultation, please reach out
to your Google Technical Account Manager to sign up for office hours with our
Google Ads Developer Relations team.
Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4. 0 License, and code samples are licensed under the Apache 2. 0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.
Last updated 2020-05-14 UTC.
[{
“type”: “thumb-down”,
“id”: “missingTheInformationINeed”,
“label”:”Missing the information I need”}, {
“id”: “tooComplicatedTooManySteps”,
“label”:”Too complicated / too many steps”}, {
“id”: “outOfDate”,
“label”:”Out of date”}, {
“id”: “samplesCodeIssue”,
“label”:”Samples / code issue”}, {
“id”: “otherDown”,
“label”:”Other”}]
“type”: “thumb-up”,
“id”: “easyToUnderstand”,
“label”:”Easy to understand”}, {
“id”: “solvedMyProblem”,
“label”:”Solved my problem”}, {
“id”: “otherUp”,
“label”:”Other”}]