Configuring Charles Web Debugger to work with Android devices and emulator

Quite a mouthful eh?

During my web days I became very attached to Charles as part of my development toolset. It was a sad day when I discovered it was difficult to get it to work with either the emulator, much less a phone.

My first attempt circa Android Froyo (2.2) yielded no success. It’s still doable but you have to root your phone and download an app from the market. It was a bit frustrating given that the Charles documentation didn’t provide a lot of insight.

A second attempt led me to try again on two Motorola phones running 2.3. It turns out that it is now possible to set a proxy when connecting to a wifi access point!!!
Oh happy day!

This means that we can have our phone point to the IP address of the computer running Charles. And just like magic Charles works!

First let’s look at how to configure a phone, since it’s easiest. Then we’ll look at getting the emulator to work.

 

Configuring your android device to work with Charles.

Step 1:
Connect to your Wifi.

(note that your device and your machine running Charles need to be on the same network)

Proxy settings on a Motorola Droid 3!!

Set the port to the IP of your machine running Charles. Set the port to 8888.

To access the menu above on existing wifi networks simply long click the name. New networks will present the option when connecting.

That’s it! Charles should work.

 

Now onto the second part.

Configuring the Android emulator to work with Charles.

 

When starting an emulator you can do one of two things:

Using the command line you can do something like:

emulator -http-proxy localhost.:8888 -port 5554 -debug-proxy @Nexus

Optionally you can set the same emulator  in the run configurations in Eclipse.

Watch out!!! this technique only works with revision 13 and above of the Android SDK. r12 was broken and had a bug logged against it but has since been fixed in 13. Make sure you’re up to date!

Eclipse run configuration settings.

Remember that you have to make sure the emulator starts using these additional arguments. If you already have one running these extra arguments will have no effect as the proxy will not be created.

 

Charles gotchas:

If your apps use https you may run into some problems with the server rejecting your certificates (or lack thereof).

I think there is a way to get Charles to provide a certificate on behalf of your device but I haven’t figured that part out yet.

In the mean time I’ve had luck using the FakeX509TrustManager when creating an https connection from within my app.

 

 

private static SSLContext createEasySSLContext() throws IOException {
		try {
			SSLContext context = SSLContext.getInstance("TLS");
			context.init(null, new TrustManager[] { new FakeX509TrustManager() }, null);
			return context;
		} catch (Exception e) {
			throw new IOException(e.getMessage());
		}
	}

I hope this is helpful!

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>