Android 7+ introduces certificate pinning that makes it more difficult to debug the requests the device makes. This covers the pre-certificate-pinning Android versions.
Install
Download and install. Here's the archive with all available versions: https://developer.android.com/studio/archive. I will be installing 2023.2.1.25.
First Launch; Choosing SDK
If Android Studio crashes every time you try to open it, check your available disk space. The unpacked app itself takes about 2,5-3 GB and then it needs quite a lot of space when you launch it, even if it claims otherwise. I got it to work only after I freed up about 15-20 GB. It may depend on the Android Studio version and the SDK you chose.
In case you want to choose your SDK version: when you launch Android Studio for the first time, you are greeted with Setup Wizard which suggests you install the latest SDK. Agree and let it be downloaded and installed. Then go to Android Studio Preferences -> Languages and Frameworks -> Android SDK. There you will see the list of all available SDKs. (If you skip installing the suggested SDK, all options will be grayed out. It seems you have to install the newest one first.)
Basically, what you're choosing here is how recent Android APIs you will be working with. The newer SDKs are normally larger. Older SDKs are smaller, sometimes significantly.
Create an Android Virtual Device
When Android Studio is launched, you will be presented with the Welcome to Android Studio screen. Under More Actions click Virtual Device Manager. There you can create a new Android Virtual Device (AVD). This will be the actual emulated phone you will be working with.
I selected Pixel (just the old Pixel) with the API 24 and Google Play enabled. Internally my AVD ID is "Pixel_API_24".
MITMProxy
Install on your computer by downloading the binaries.
Launch (on a Mac) by going to Terminal and running /Applications/mitmproxy.app/Contents/MacOS/mitmweb
. This starts MITM Proxy and opens the browser UI.
Remember to install the MITM Proxy certificate on your AVD. If you don't, the HTTPs connections made on your device will fail. A good sign you forgot this step is you start MITM Proxy and all it's intercepting is:
http://connectivitycheck.gstatic.com/generate_204 GET 204 0 39ms
http://www.google.com/gen_204 GET 204 0 57ms
The command line will be complaining as well:
Client TLS handshake failed. The client does not trust the proxy's certificate for www.google.com (OpenSSL Error([('SSL routines', '', 'sslv3 alert certificate unknown')]))
Add the certificate by transferring it to the device:
- go to http://mitm.it and download it from there (choose Android)
- drag and drop it from the host machine onto the emulator
- try
~/Library/Android/sdk/platform-tools/adb push ~/.mitmproxy/mitmproxy-ca-cert.cer /storage/emulated/0/Download
- you can always either e-mail the file to yourself, or upload it to some URL and then download it using the built-in Androind browser
Gotcha: if you don't see the file in your Downloads folder, do not worry and waste hours trying to debug it; chances are the certificate is actually there. Try installing it first.
Install the certificate. If you download it from the browser, you will be prompted right away, otherwise go to Settings -> Wi-Fi -> (…) -> Advanced -> Install Certificates, and browse and select the certificate file. When prompted for Credential use, choose Wi-Fi. You may have to install again for VPN and apps too. You may have to enable PIN on the device.
Configure your Android Virtual Device to use MITM Proxy either internally on the actual AVD, or externally in the emulator settings.
- Internally – on AVD itself.
- Go to Wi-Fi settings and under Advanced options for the default "AndroidWifi" network, configure the proxy for
10.0.2.2
on port8080
. We're using10.0.2.2
to access the host machine from within the emulated device. - If your AVD does not have Wi-Fi at all, go to Settings -> Wireless & networks -> (More) -> Cellular networks -> Access Point Names -> T-Mobile US -> (Enter the same proxy settings as above) -> (…) -> Save
- Go to Wi-Fi settings and under Advanced options for the default "AndroidWifi" network, configure the proxy for
- Externally – in Emulator. With AVD running, click (…) -> Settings -> Proxy. Disable Use Android Studio HTTP proxy settings. Check Manual proxy configuration. Use Host name =
127.0.0.1
, Port number =8080
. Click Apply.
We're using 10.0.2.2
because that's and 127.0.0.1
Gotcha: if using the latter, turn off Wi-Fi on the AVD and switch to Mobile data. (source)
Once configured, connect (or re-connect) to the network.
Gotchas
- Enable play store by editing the ini file
- Make sure your SDK has play store in it
Navigating to the SDK folder: cd ~/Library/Android/sdk
If the AVD doesn't start, debug by using command line:
emulator/emulator -list-avds
emulator/emulator -avd My_Pixel_Example -verbose
If the emulator is slow, consider choosing older APIs (25) and using Desktop Native OpenGL.
If you need to install a specific app, get it from e.g. apkmirror.com or apkpure.com and install by calling platform-tools/adb install "/private/tmp/example.apk"
. You don't have to shut down the device, you can install while it's running and the app will appear in the menu automatically.
Choosing API that's pre Android 7 will make MITM easier:
However, with the Network Security Configuration introduced in Android 7 and app developers trying to prevent MITM attacks using certificate pinning, getting an app to work with an HTTPS proxy has become quite tedious.
If you're trying to add your MITM proxy and it's not working via Settings -> Proxy and you don't have Wi-Fi to try the alternative way, do it via APN settings. Put 10.0.2.2
(source) as server and 8080
as port.
Remember to turn the Airplane Mode on and off after you boot your device. Until you do, your traffic won't be routed through the proxy you configured.
Links: