Reader small image

You're reading from  Mastering Kotlin for Android 14

Product typeBook
Published inApr 2024
Reading LevelIntermediate
PublisherPackt
ISBN-139781837631711
Edition1st Edition
Languages
Right arrow
Author (1)
Harun Wangereka
Harun Wangereka
author image
Harun Wangereka

Harun Wangereka is a Google Developer Expert for Android and an Android engineer with over seven years of experience and currently working at Apollo Agriculture. Harun is passionate about Android development, never tired of learning, building the tech community, and helping other developers upscale their skills. He is a co-organizer at Droidcon Kenya and a former Kotlin Kenya and Android254 co-organizer. Through these communities, he has been able to impact thousands of developers. He is also an Android author at Kodeco where he has written 8 articles, published a book; Saving Data on Android, Second Edition, and is also a video course instructor. He has given numerous sessions on Android and Kotlin across different communities worldwide.
Read more about Harun Wangereka

Right arrow

Debugging Your App

Debugging is a very important aspect of developing apps. It helps us identify and fix bugs in our code. It is a very important skill to have as a developer. It also helps us avoid bugs in the future. Many tools can help us debug our code. In this chapter, we will be looking at some of the tools that can help us debug our code.

In this chapter, we will learn debugging tips and tricks, how to detect leaks using LeakCanary, how to inspect network requests/responses fired by our app using Chucker, and how to inspect our Room database, network requests, and background tasks using App Inspection.

In this chapter, we’re going to cover the following main topics:

  • General debugging tips and tricks
  • Detecting leaks with LeakCanary
  • Inspecting network requests with Chucker
  • Using App Inspection

Technical requirements

To follow the instructions in this chapter, you will need to have Android Studio Hedgehog or later (https://developer.android.com/studio) downloaded.

You can use the previous chapter’s code to follow the instructions in this chapter. You can find the code for this chapter at https://github.com/PacktPublishing/Mastering-Kotlin-for-Android/tree/main/chapterten.

General debugging tips and tricks

Android Studio provides us with a variety of features that help us debug our code. Some of the features are listed in the following points:

  • Logcat
  • Stack Traces
  • Breakpoints

Let us look at each of these closely.

Logcat

Logcat in Android Studio displays log messages in real-time from our apps. Each log message has a priority level attached to it. We add log messages in our app using the Log class. This class offers different priority levels that we can use to log messages. The different priority levels are as follows:

  • V: Verbose (lowest priority)
  • D: Debug
  • I: Info
  • W: Warning
  • E: Error
  • F: Fatal
  • S: Silent (highest priority)

We use the preceding letters to specify the log level. For example, if we want to log a message with the debug level, we will use the following code:

Log.d("TAG", "Message")

The first parameter is the tag. The tag is used to identify the source...

Detecting memory leaks with LeakCanary

LeakCanary is an open-source library developed by Square that helps us detect memory leaks in our apps. The library has knowledge of the internals of the Android Framework that allows it to narrow down the cause of the memory leak. It helps reduce the Application Not Responding (ANR) errors and out-of-memory crashes in our apps. Here are some of the most common causes of memory leaks:

  • Storing instances of Activity as Context filed in an object that survives activity recreation due to configuration changes
  • Forgetting to unregister broadcast receivers, listeners, callbacks, or RxJava subscriptions when they are no longer needed
  • Storing references to Context in a background thread

LeakCanary is quite easy to set up and no code implementation is needed to use it. We just need to add the leakcanary-android dependency in our libs.version.toml file:

leakcanary-android = "com.squareup.leakcanary:leakcanary-android:2.12"...

Inspecting network requests with Chucker

This is from the Chucker GitHub (https://github.com/ChuckerTeam/chucker) page:

Chucker simplifies the inspection of HTTP(S) requests/responses fired by our Android apps. Chucker works as an OkHttp Interceptor persisting all those events inside our application, and providing a UI for inspecting and sharing their content. Chucker displays a notification showing a summary of the ongoing network request.

Tapping the Chucker notification mentioned previously launches the Chucker UI. Chucker UI shows a list of all the network requests that have been made by our app. We can tap on a request to see the details of the request.

To use Chucker, follow these steps:

  1. Add the chucker dependency in the libs.versions.toml file:
    chucker = "com.github.chuckerteam.chucker:library:4.0.0"
    chucker-no-op = "com.github.chuckerteam.chucker:library-no-op:4.0.0"

    In the preceding code, we have added two dependencies: the first one is the...

Using App Inspection

App Inspection allows us to debug our database, inspect network traffic, and debug our background tasks. It is a very important tool in helping us debug our apps. To use App Inspection, let us run our app and then navigate to View | Tool Windows | App Inspection in Android Studio:

Figure 10.20 – App Inspection

Figure 10.20 – App Inspection

App Inspection automatically connects to our app. The first tab is Database Inspector. On the left, we can see the different databases created by our app. We have the WorkManager, Chucker, LeakCanary, and Cat databases that we created earlier on. Let us click on the Cat database, and we can see the columns of the table that we created in the database:

Figure 10.21 – Cat database

Figure 10.21 – Cat database

This shows the columns and the values that have been saved in the database. We can also run queries in the database. The query option is highlighted in the following figure:

Figure 10.22 – Execute query button

Figure...

Summary

In this chapter, we learned some debugging tips and tricks, how to detect leaks using LeakCanary, how to inspect network requests/responses fired by our app using Chucker, and how to use App Inspection to debug our database, inspect our network requests, and inspect background tasks, too.

In the next chapter, we will learn Kotlin style and the best practices for writing Kotlin code. We will also learn how to use plugins such as Ktlint and Detekt to format, lint, and detect the code smells early.

lock icon
The rest of the chapter is locked
You have been reading a chapter from
Mastering Kotlin for Android 14
Published in: Apr 2024Publisher: PacktISBN-13: 9781837631711
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at €14.99/month. Cancel anytime

Author (1)

author image
Harun Wangereka

Harun Wangereka is a Google Developer Expert for Android and an Android engineer with over seven years of experience and currently working at Apollo Agriculture. Harun is passionate about Android development, never tired of learning, building the tech community, and helping other developers upscale their skills. He is a co-organizer at Droidcon Kenya and a former Kotlin Kenya and Android254 co-organizer. Through these communities, he has been able to impact thousands of developers. He is also an Android author at Kodeco where he has written 8 articles, published a book; Saving Data on Android, Second Edition, and is also a video course instructor. He has given numerous sessions on Android and Kotlin across different communities worldwide.
Read more about Harun Wangereka