Sunday, March 23, 2014

Debugging Android HTTP traffic with Charles Proxy and Genymotion emulator

Genymotion (http://www.genymotion.com/) is a great tool for debugging your Android code.
It is much faster than the Android emulator as it is based on x-86 native code, and unless you really do some low level work, is indistinguishable from a real device. It actually is faster and smoother than most devices, so make sure you also test on real devices...
Anyway, it became an important tool for me while developing. Highly recommended!

Occasionally you need to debug HTTP calls (to web sites, or more often to server API end points), and for that an HTTP proxy as Charles (http://www.charlesproxy.com/) is another great tool. Its paid version already returned its cost 10 fold for me...

So, first you will need to setup charles on your Mac as the proxy for the emulated Android machine. The key is to know the local mac IP is 10.0.3.2 and the default port for the proxy is 8888 (if you have not played with Charles settings).
Detailed instructions for setting the proxy on the Android virtual machine in Genymotion are available here: http://rexstjohn.com/using-genymotion-charles-proxy/:
And repeated for your convenience
  • In your Genymotion Android emulator…
  • Settings -> Wifi -> Press and hold your active network
  • Select “Modify Network”
  • Select “Show Advanced Options”
  • Select “Proxy Settings -> Manual”
  • Set your Proxy to: 10.0.3.2 (Genymotion’s special code for the local workstation)
  • Set your Port to: 8888
  • Press Save
Now, this worked fine as long as I was using unencrypted http, but if you need to work with SSL encrypted APIs and need to get the clear data to debug your stuff, you can set Charles to provide the SSL certificate.
To do that you need to follow these instructions (the instructions are available here: http://stackoverflow.com/questions/3976728/how-to-configure-ssl-certificates-with-charles-web-proxy-and-the-latest-android between the "brakertech" answer and "bkurzius: answer):
  1. Install fully licensed charles version
  2. In Charles, goto Proxy -> Proxy Settings -> check “Enable Transparent HTTP Proxying”
  3. Proxy -> Proxy Settings -> SSL TAB -> check “enable SSL Proxying”
  4. Add your host to the list of Locations (e.g. www.secure.com) with port 443
  5. Download the Charles certificate here: http://www.charlesproxy.com/documentation/using-charles/ssl-certificates/
  6. unzip the certificate, and then send it by email to your emulated device (or make it available for download from the emulated device browser).
  7. When you get it on the device and open it, you will be able to install it by giving it a name. Depending on the Android OS version, you may need to first set a screen lock PIN or password
 Once the certificate is installed, you can intercept encrypted SSL traffic using Charles.




And a message from our advertisers:


Toptal provides remote engineers and designers of high quality. I recommend them. Follow this link (full disclosure: this is my affiliate link):
https://www.toptal.com/#engage-honest-computer-engineers-today



No comments:

Post a Comment