Search icon
Arrow left icon
All Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletters
Free Learning
Arrow right icon
Mastering Kotlin for Android 14

You're reading from  Mastering Kotlin for Android 14

Product type Book
Published in Apr 2024
Publisher Packt
ISBN-13 9781837631711
Pages 370 pages
Edition 1st Edition
Languages
Author (1):
Harun Wangereka Harun Wangereka
Profile icon Harun Wangereka

Table of Contents (22) Chapters

Preface 1. Part 1: Building Your App
2. Chapter 1: Get Started with Kotlin Android Development 3. Chapter 2: Creating Your First Android App 4. Chapter 3: Jetpack Compose Layout Basics 5. Chapter 4: Design with Material Design 3 6. Part 2: Using Advanced Features
7. Chapter 5: Architect Your App 8. Chapter 6: Network Calls with Kotlin Coroutines 9. Chapter 7: Navigating within Your App 10. Chapter 8: Persisting Data Locally and Doing Background Work 11. Chapter 9: Runtime Permissions 12. Part 3: Code Analysis and Tests
13. Chapter 10: Debugging Your App 14. Chapter 11: Enhancing Code Quality 15. Chapter 12: Testing Your App 16. Part 4: Publishing Your App
17. Chapter 13: Publishing Your App 18. Chapter 14: Continuous Integration and Continuous Deployment 19. Chapter 15: Improving Your App 20. Index 21. Other Books You May Enjoy

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 2024 Publisher: Packt ISBN-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.
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}