Writing this short guide becuase other guides reference outdated Android Studio versions and do not correctly outline all steps

Preamble

I am writing this guide based on the my setup: Windows 11, Android Studio Hedgehog | 2023.1.1 Patch 1.
As all software described in this post have versions for other OS’s, this guide may also work for them.

Installing mitmproxy

Install it from here.

mitmproxy – an interactive HTTPS proxy

If you want a tool to automatically generate a REST API reference, then also install mitmproxy2swagger.

alufers/mitmproxy2swagger: Automagically reverse-engineer REST APIs via capturing traffic (github.com)

Launching mitmweb

mitmweb is just a frontend for mitmproxy featuring a convenient web UI.
I could not launch it from the start menu shortcut, instead I needed to manually launch it from a command prompt

Preparing Android Studio and AVD

Install an Android 6.0 virtual device. Later Android versions do not allow user certificates to be used for all traffic system wide, instead requiring the app to choose a specific user certificate.

mitmproxy depends on routing AVD traffic through a proxy. Later versions of AVD removed the option to set a proxy. To bring it back:

  • Open the Android Studio global settings window
  • Navigate to Tools -> Emulator
  • Uncheck the option to “Launch in a tool window”

Then set the proxy settings:

  • Open the running AVD settings window
  • Navigate to Settings, and then select the Proxy tab
  • Choose “Manual proxy configurration”
  • Enter 127.0.0.1 as the host name and 8080 for the port

Installing the user certificate

Launch the AVD

Open the web browser, and navigate to “mitm.it”

Click on the button to get certificate next to the Android category

Give the certificate any name you like, and save it.

You have now completed setting up AVD with mitmproxy!

Testing the proxy

Navigate to any website in the AVD web browser. You should see traffic start to appear in mitmweb.

Notes

Why not use the Android Studio built in proxy settings?

The built in proxy settings would route all Android Studio through the mitmproxy, which would pollute the logs with:

  • Version control
  • Update checks
  • Gradle / Maven activities
  • Documentation fetches
  • etc

This is still a viable approach if you used the filter settings in mitmproxy, but for API’s that reference multiple domains this is a little trickier.