Different Ways to Debug Android API Calls

Different Ways to Debug Android API Calls

While developing Android apps many developers face the question “How to debug outgoing and incoming traffic on Android?”. QA Engineers during testing Android Apps also need a way using which they can see all the logs related to the API’s calls so that they can check what data is being sent from the app and whether the data sent is valid or not.


API monitoring helps developers as well as QA Engineers to gain a better understanding of how their application communicates with the server, which helps in troubleshooting and enhances user experience. In this article, the following sections will cover popular tools and techniques that can help you effectively track API calls, analyze the data you gather, and improve your Android Application.

So let's first explore some of the latest free options available, and then we will cover some paid options as well.


Android Studio

Monitoring the application's API calls may also be done using Android Studio's Network Inspector. No other tools or libraries are required; all is done through Android Studio.

To open the Network Inspector in Studio, follow these steps:

  1. To inspect an app, click View -> Tool Windows -> App Inspection from the Android Studio navigation bar. Choose Network Inspector from the tabs when the app inspection window automatically establishes a connection with an app process. You might have to choose an app process manually if the app inspection window is unable to connect to one automatically.
  2. The App Inspection window allows you to select the device and app process that you wish to analyze.

The below window will open if everything is working fine

ADB Logcat:

We can also use ADB logcat to monitor API calls as they are made. The Logcat window in Android Studio helps us to debug our app by displaying logs from your device in real-time. It will also show exceptions which is thrown by an App,  Logcat shows a message followed by the associated stack trace containing links to the line of code. If we want particular logs we can add filters according to our requirements using OkHttp interceptor

Steps to add OkHttp interceptor

Open build.gradle file and add the following

dependencyimplementation("com.squareup.okhttp3:logging-interceptor:4.12.0")

Now add this in  OkHttp instance as below:

val httpLogging = new HttpLoggingInterceptor()
val loggingLevel = if (BuildConfig.DEBUG) Level.BASIC else Level.NONE
httpLogging.setLevel(loggingLevel)
val okHttpClient = OkHttpClient.Builder()
  .addInterceptor(httpLogging)
  .build()

We can change the log level at any time by calling setLevel().


Chucker

Chucker is an Android library that can be used for inspecting HTTP API directly on the device. Using this we will be able to see what API calls are being made when testing the app and ensuring that everything is in order; you're not only restricted to seeing the calls, but you can also share the whole request and response headers and body, as well as the cURL command. For every API call, a notification is created which can quickly be accessed when needed.

Steps to add Chucker

Open build.gradle file and add the following

Please note that you should add both the library and the library-no-op a variant to isolate Chucker from release builds as follows:

debugImplementation "com.github.chuckerteam.chucker:library:4.0.0"
releaseImplementation "com.github.chuckerteam.chucker:library-no-op:4.0.0"

To start using Chucker, just plug in a new ChuckerInterceptor to your OkHttp Client Builder:

val client = OkHttpClient.Builder() .addInterceptor(ChuckerInterceptor(context)) .build()

That's it! ? Chucker will now record all HTTP interactions made by your OkHttp client.

To see just click on the notification and then the below screen will open

0:00
/

Flipper by Facebook:

Flipper is a library from Facebook a desktop debugging platform for mobile developers. It provides many more features and debugging tools besides monitoring API calls, such as the ability to view Shared Preferences, Check the XML layout, See Databases within the app, and more. Let's see how we can use this to monitor API calls.

Steps to add Flipper

Open build.gradle file and add the following

dependencies {
  val flipper_version = "0.250.0"
  debugImplementation("com.facebook.flipper:flipper:$flipper_version")
  debugImplementation("com.facebook.soloader:soloader:0.10.5")
  debugImplementation("com.facebook.flipper:flipper-network-plugin:$flipper_version")
  
  releaseImplementation("com.facebook.flipper:flipper-noop:$flipper_version")
}

To start using Flipper, initialize it and add which plugin/features of Flipper you are interested in.

override fun onCreate() {
  super.onCreate()
  AndroidFlipperClient.getInstance(this@App).apply {
      addPlugin(FlipperModule.networkFlipperPlugin)
      start()
  }
}

Now let's add a flipper interceptor to an instance of OkHttpClient:

val okHttpClient = OkHttpClient.Builder()
      .addInterceptor(FlipperOkhttpInterceptor(NetworkFlipperPlugin())
      .build()

That is it! Now go to https://fbflipper.com/ and download a Flipper client that is compatible with our system. Once downloaded, we must configure it and assist it in determining where adb is located on our system. After that, if we launch the app on an emulator or a device, we will see the following in the Flipper client:

using this we can:

  • Inspect all properties of a request and response
  • Share cURL command with the QA Engineer or API team
  • Add different filters
  • Inspect layout and many more things

And much more!

Now let's discuss in brief about the paid options available ?

  1. AppSpector — Uncover & Resolve App Issues Instantly
  2. Shipbook — ​Find What You are Logging For.
  3. Bugfender — Remote Logger, Crash Reporter and In-App User Feedback

Conclusion:

We’ve discussed the following:

  1. Android Studio
  2. ADB Logcat
  3. Chucker
  4. Flipper

Each of them has some pros & cons and based on the requirements and your needs anyone can be used. Chucker is a good solution, in my opinion, since it gives everyone on the team, from QA to managers a very simple tool to use for inspecting API calls when necessary. Flipper contains rich information and can be used for more than just network inspection, so perhaps it would be a suitable choice for developers.

The Network Inspection feature in Android Studio might be a useful choice for rapid debugging if we don't have time to set anything up.

Happy Debugging ?