0Pricing
Android Academy · Lesson

Debugging & Profiling

Master Android Studio's debugging and profiling tools: breakpoints, Logcat, Timber, Memory Profiler, LeakCanary, CPU Profiler, Network Inspector, and Layout Inspector.

Debugging & Profiling is a free Android Academy lesson on CoddyKit — lesson 4 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Android Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Debugging Overview

Debugging is the process of finding and fixing bugs. Android Studio provides powerful tools:

  • Debugger — pause execution, inspect variables, step through code
  • Logcat — real-time log output from the device
  • Layout Inspector — inspect the live UI hierarchy
  • Android Profiler — CPU, Memory, and Network analysis
  • Database Inspector — view and query Room databases live

Logcat — Your First Debug Tool

Use Log to print messages to Logcat. Use a TAG to filter your logs:

import android.util.Log

class UserRepository {
    companion object {
        private const val TAG = "UserRepo"
    }

    fun fetchUsers() {
        Log.d(TAG, "fetchUsers() called")        // Debug
        Log.i(TAG, "Fetching ${users.size} users") // Info
        Log.w(TAG, "Cache is empty")               // Warning
        Log.e(TAG, "Network error: $exception")    // Error
    }
}

Timber — Better Logging

Timber is a thin wrapper over Log that strips log calls from release builds and adds the class name as TAG automatically:

// app/build.gradle:
// implementation 'com.jakewharton.timber:timber:5.0.1'

// In Application.onCreate():
if (BuildConfig.DEBUG) {
    Timber.plant(Timber.DebugTree())
}

// Usage anywhere in the app (no TAG needed):
Timber.d("User loaded: %s", user.name)
Timber.e(exception, "Failed to fetch data")
Timber.w("Cache miss for key: %s", key)

Breakpoints and the Debugger

Steps to debug with breakpoints in Android Studio:

  1. Click the gutter (left margin) next to a line of code — a red dot appears
  2. Run the app with Debug (bug icon) instead of Run
  3. When execution hits the breakpoint, it pauses
  4. Inspect variables in the Variables pane
  5. Use Step Over (F8) to go line by line, Step Into (F7) to enter a function

Evaluate Expression

While paused at a breakpoint, use Evaluate Expression (Alt+F8) to run Kotlin code in the current scope. Test assumptions without restarting:

  • Check list.size, user.email.isNotBlank()
  • Call any method and see the return value
  • Inspect complex objects inline

Memory Profiler

Find memory leaks with the Memory Profiler (View → Tool Windows → Profiler):

  • Watch heap size over time — steadily growing = likely leak
  • Force GC and observe if memory drops
  • Take a heap dump to see what objects are alive
  • Look for Activity or Fragment instances that should have been GC'd

LeakCanary — Automatic Leak Detection

LeakCanary automatically detects and reports memory leaks as notifications in debug builds:

// app/build.gradle:
debugImplementation 'com.squareup.leakcanary:leakcanary-android:2.12'

// That's it — no code changes needed!
// LeakCanary auto-installs via ContentProvider.
// When a leak is detected, it shows a notification with the leak trace.

// Common causes to watch for:
// - Static references to Context/Activity
// - Not releasing ViewBinding in onDestroyView()
// - Listeners registered to singletons but not unregistered

CPU Profiler

Find performance bottlenecks with the CPU Profiler:

  • Record a trace while performing an action
  • View a flame chart to see which methods take the most time
  • Look for long-running work on the main thread — this causes janky UI
  • Move heavy computation to Dispatchers.IO or Dispatchers.Default

Network Inspector

The Network Inspector shows all HTTP calls made by your app in real time:

  • View request URL, method, headers, body
  • View response code, body, timing
  • Identify slow or failing requests
  • Works with OkHttp (used by Retrofit) out of the box

Access it via View → Tool Windows → App Inspection → Network Inspector.

Database Inspector

Inspect and query your Room database live on a running device:

  • View tables and their contents
  • Run custom SQL queries
  • Watch data change as you use the app

Access via App Inspection → Database Inspector. Works for Room on API 26+.

Layout Inspector

The Layout Inspector shows a live 3D view of your UI hierarchy:

  • Select any view to see its attributes (margins, padding, text, color)
  • Spot overlapping views or wrong visibility
  • Debug layout issues without guessing

Access via View → Tool Windows → Layout Inspector. Connect to a running app on device or emulator.

StrictMode

StrictMode detects accidentally slow operations on the main thread (disk reads, network calls) and logs or crashes the app in debug builds:

// In Application.onCreate() — debug builds only:
if (BuildConfig.DEBUG) {
    StrictMode.setThreadPolicy(
        StrictMode.ThreadPolicy.Builder()
            .detectDiskReads()
            .detectDiskWrites()
            .detectNetwork()
            .penaltyLog()      // log violations instead of crashing
            .build()
    )
}

Quick Check

Which tool automatically detects memory leaks in Android debug builds with no extra code?

Recap: Debugging & Profiling

Find and fix problems efficiently:

  • Logcat + Timber — lightweight logging, auto-stripped in release
  • Breakpoints + Debugger — pause and inspect live execution
  • Memory Profiler — detect heap growth and leaks
  • LeakCanary — automatic leak detection with a stack trace
  • CPU Profiler — find slow methods on the main thread
  • Network Inspector — monitor HTTP calls
  • Database Inspector — query Room live
  • StrictMode — catch accidental main-thread IO

Congratulations — you've completed the full Android Development course!

Frequently asked questions

Is the “Debugging & Profiling” lesson free?

Yes — the full text of “Debugging & Profiling” is free to read here on the web, and the Android Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Android Academy course, upgrade to CoddyKit PRO.

What will I learn in “Debugging & Profiling”?

Master Android Studio's debugging and profiling tools: breakpoints, Logcat, Timber, Memory Profiler, LeakCanary, CPU Profiler, Network Inspector, and Layout Inspector. You practise Android Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Android Academy?

No prior experience is required. Android Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 4 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Debugging & Profiling” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Android Academy lesson?

Yes. Every Android Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Unit Testing with JUnit
  2. Mocking with Mockito
  3. UI Testing with Espresso
  4. Debugging & Profiling
← Back to Android Academy