Applying Rules for Network Inspector in Android Studio Flamingo

Rohit Gupta
4 min readNov 5, 2022

--

twitter @joreilly

The latest canary for Android Studio brings in a really handy new feature to inspect and debug network connections on your android application.

The Network Inspector displays realtime network activity on a timeline, showing data sent and received. This lets you examine how and when your app transfers data, and optimize the underlying code appropriately.

Now, this might not sound exciting if you’re already using some sort of network logging interceptor for your okhttp client, however, the feature which makes this stand out is setting custom ‘rules’ for incoming connections.

With rules, you can change network response / headers at realtime which can help you to validate your app against different scenarios. For example, I can set a rule to return me a different body for a REST API endpoint

{"titleResourceId": "my_id_1","descriptionResourceId": "my_id_1_desc","value": "true","extra": null}

to

{"titleResourceId": "my_id_1","descriptionResourceId": "my_id_1_desc","value": "false","extra": {}}

If you’ve used any network proxy before to alter API responses at run time (ex. Charles), you can think of this as its lite edition 😁

To set rules, all you have to do is open up the App Inspection tool. You can find it at the bottom bar of Android Studio Flamingo, else you can just press double shift and type in ‘App Inspection’. For the sake of this demo, I’m just going to clone the first sample app I see on github, the dice roll goes to Pictrr

App Inspection

Inside App Inspection, navigate to the Network Inspection tab. Once there, you’ll have to attach your inspector to the app you want to inspect..

Now, see the running app above? I’m going to change the response of one of the tabs. Let’s pick ‘3D Renders’ to return me an empty JSON object. This way I can validate how this app handles an empty body. To do this, I’ll identify the network request that is made when I tap on 3D Renders. Let’s do that now.

API Call identified for 3D Renders

In the above picture, we can see the URL that is invoked to get the response for 3D Renders. Let’s copy the URL, we’ll use it to set our rule.

You can check the above photo to see how I broke down the URL to set the rule. We do not need to worry about the query. I’ve set the rule to return me a 500 instead of a 200 status code. In the body section, I’ve also asked it to return me an empty json object. Now let’s see what happens when we try to invoke the API call for 3D Renders again.

As you can see in the above video, our app does not handle empty bodies well.

There are some caveats that you need to keep in mind with Network Inspector however,

  1. The feature is still in development, so expect a few things to change.
  2. Rules are not persistent, once you kill your app, the rule is lost.
  3. Rules are not very friendly with GraphQL (as of now), I could not find a way to make it work with GraphQL requests as all of them have the same endpoint. If you have something then please let me know in the comments :)
  4. Rules only work with OkHttp and HttpURLConnection.

Thanks for taking the time to read my article. I hope if helps you!

--

--

Rohit Gupta
Rohit Gupta

Written by Rohit Gupta

Android developer @Publicis Sapient. ♥️ video games, tech, cyberpunk and sci fi literature.

No responses yet