When developing Android applications with Kotlin and Jetpack Compose, errors are a normal part of the development process.
Sometimes your application may crash unexpectedly. A button might not respond, a screen might display incorrect data, or an API request might fail.
How do Android developers identify these problems?
The answer is Android Studio Logcat and debugging tools.
Android Studio provides powerful tools to inspect application behavior, monitor logs, analyze exceptions, and debug Kotlin code.
In this tutorial, we’ll learn how to use Android Logcat, understand logging levels, investigate crashes, and debug applications using breakpoints.
What You’ll Learn
By the end of this tutorial, you’ll understand:
- What Android Logcat is
- Why logging is important
- How to open Logcat in Android Studio
- Android logging levels
Log.v(),Log.d(),Log.i(),Log.w(), andLog.e()- How to filter Logcat messages
- How to identify Android application crashes
- How to read exception stack traces
- How to use breakpoints
- How to inspect variables
- How to debug Kotlin and Jetpack Compose applications
- Debug vs Release builds
- Common debugging mistakes
- Android debugging best practices
Let’s begin.
1. What is Android Logcat?
Logcat is a debugging tool that displays system and application log messages from an Android device or emulator.
It helps developers understand what is happening while an application is running.
For example, Logcat can display:
- Application startup messages
- Debug information
- Warning messages
- Exceptions
- Application crashes
- Android framework messages
- Custom logs written by developers
Simple explanation
Think of Logcat as a window into your application’s behavior.
Android Application
|
v
Application Events
|
v
Log Messages
|
v
Android Studio Logcat
|
v
Developer Investigates
When something goes wrong, Logcat is often one of the first tools Android developers use.
2. Why is Logcat Important?
Imagine you’re developing a Jetpack Compose application.
The application launches successfully, but clicking a button doesn’t produce the expected result.
Without debugging tools, you might not know what went wrong.
Using Logcat, you can inspect whether the button click is being triggered.
Example:
Log.d("MainScreen", "Button clicked")
When the user taps the button, the message appears in Logcat.
This helps confirm that the click handler executed.
Logging is especially useful for:
- Tracking application behavior
- Investigating errors
- Understanding lifecycle events
- Debugging user interactions
- Monitoring development-time operations
3. How to Open Logcat in Android Studio
Follow these steps:
Step 1: Open your Android project in Android Studio.
Step 2: Run the application on an Android emulator or physical device.
Step 3: Open the Logcat tool window.
Navigate to:
View → Tool Windows → Logcat
Step 4: Select the correct device.
Step 5: Select your application’s process or apply an appropriate Logcat filter.
You should now see log messages from the connected Android device.
Important
Logcat may contain thousands of messages from Android system components and other applications.
Filtering helps you focus on relevant messages.
4. Android Logging Levels Explained
Android provides several logging levels.
Each level represents the importance or severity of a message.
| Level | Kotlin Method | Purpose |
|---|---|---|
| Verbose | Log.v() | Detailed diagnostic information |
| Debug | Log.d() | Development and debugging |
| Info | Log.i() | General informational messages |
| Warn | Log.w() | Potential problems |
| Error | Log.e() | Errors and failures |
| Assert | Log.wtf() | Conditions that should never occur |
Let’s understand each level.
5. Log.v() — Verbose Logging
Verbose logging is used for highly detailed diagnostic messages.
Example:
Log.v(
"MainActivity",
"Verbose log message"
)
Verbose messages can help investigate detailed application behavior.
However, excessive logging can make debugging difficult.
Use verbose logging selectively.
6. Log.d() — Debug Logging
Debug logging is one of the most commonly used logging methods during development.
Example:
Log.d(
"MainActivity",
"Application started"
)
You can also log variable values.
val username = "Alex"
Log.d(
"UserScreen",
"Username: $username"
)
Expected output:
D/UserScreen: Username: Alex
When should you use Log.d()?
Use debug logs when investigating:
- Button clicks
- Screen navigation
- Function execution
- State changes
- Development-time behavior
Avoid logging passwords, access tokens, or sensitive personal information.
7. Log.i() — Information Logging
Information logging is useful for recording meaningful application events.
Example:
Log.i(
"AppLifecycle",
"MainActivity started"
)
Expected output:
I/AppLifecycle: MainActivity started
This can help track significant application events during development.
8. Log.w() — Warning Logging
Warning logs indicate potential problems that may not immediately cause the application to crash.
Example:
val username = ""
if (username.isEmpty()) {
Log.w(
"Validation",
"Username is empty"
)
}
Expected output:
W/Validation: Username is empty
Warnings can help identify unexpected situations before they become serious problems.
9. Log.e() — Error Logging
Error logs are used when an operation fails.
Example:
Log.e(
"Network",
"Failed to load data"
)
Expected output:
E/Network: Failed to load data
You can also log exceptions.
try {
val result = 10 / 0
Log.d("Calculation", "Result: $result")
} catch (exception: ArithmeticException) {
Log.e(
"Calculation",
"Calculation failed",
exception
)
}
Here, the exception is passed as the third argument.
This allows Logcat to display the exception details and stack trace.
10. Complete Android Logging Example
Let’s create a simple Activity that demonstrates multiple logging levels.
class MainActivity : ComponentActivity() {
private val tag = "MoiksDebug"
override fun onCreate(
savedInstanceState: Bundle?
) {
super.onCreate(savedInstanceState)
Log.v(tag, "Verbose message")
Log.d(tag, "Debug message")
Log.i(tag, "Information message")
Log.w(tag, "Warning message")
Log.e(tag, "Error message")
setContent {
MyFirstAppTheme {
DebugExampleScreen()
}
}
}
}
Required import
import android.util.Log
Replace MyFirstAppTheme with your project’s theme function.
Expected Logcat output
V/MoiksDebug: Verbose message
D/MoiksDebug: Debug message
I/MoiksDebug: Information message
W/MoiksDebug: Warning message
E/MoiksDebug: Error message
The exact formatting depends on your Android Studio version and Logcat settings.
11. How to Filter Logcat Messages
Android Logcat displays messages from many processes.
Filtering allows you to focus on relevant information.
Suppose you use:
Log.d(
"MoiksDebug",
"Button clicked"
)
In the modern Logcat query field, you can filter by tag:
tag:MoiksDebug
You can also filter by your application package:
package:mine
Or filter by minimum log level:
level:ERROR
These filters make it easier to find useful messages.
Example
tag:MoiksDebug level:DEBUG
This filters messages using the specified tag and a minimum debug level.
12. What is an Android Application Crash?
An application crash occurs when an unhandled error or another fatal condition causes the application process to terminate.
For example:
val number = "Hello".toInt()
This code attempts to convert non-numeric text into an integer.
It throws a NumberFormatException.
If the exception is not handled, the application may crash.
Typical crash output
FATAL EXCEPTION: main
java.lang.NumberFormatException:
For input string: "Hello"
The complete stack trace normally contains additional information about where the exception occurred.
13. How to Read Android Stack Traces
A stack trace shows the chain of function calls associated with an exception.
For example:
FATAL EXCEPTION: main
java.lang.NumberFormatException:
For input string: "Hello"
at java.lang.Integer.parseInt(...)
at com.example.myapp.MainActivity.onCreate(MainActivity.kt:25)
This tells us:
Exception type
NumberFormatException
Problem
For input string: "Hello"
Relevant application location
MainActivity.kt:25
The file and line number can help identify the code responsible for the error.
Debugging tip
When investigating a stack trace, look for relevant frames from your application’s package.
Framework and library frames may also matter, but your application’s code is often a useful starting point.
14. Fixing a NumberFormatException
Consider this unsafe code:
val age = "Hello".toInt()
If the string isn’t a valid integer, it throws an exception.
A safer approach is:
val age = "Hello".toIntOrNull()
The result is:
null
instead of throwing a NumberFormatException for invalid input.
Example:
val age = "25".toIntOrNull()
if (age != null) {
Log.d(
"Validation",
"Valid age: $age"
)
} else {
Log.w(
"Validation",
"Invalid age entered"
)
}
This is a simple example of how understanding an exception can help you write safer Kotlin code.
15. What is Debugging in Android Studio?
Debugging is the process of investigating application behavior to identify and fix problems.
Android Studio includes an integrated debugger that allows you to:
- Pause application execution
- Set breakpoints
- Inspect variable values
- Step through code
- Examine function calls
- Evaluate expressions
Instead of relying only on log messages, you can inspect the application’s state while the code is executing.
16. What is a Breakpoint?
A breakpoint tells the debugger to pause execution when a particular line of code is reached.
For example:
fun calculateTotal(
price: Int,
quantity: Int
): Int {
val total = price * quantity
return total
}
Suppose you place a breakpoint on:
val total = price * quantity
When the debugger reaches that line, execution pauses.
You can inspect:
price = 100
quantity = 3
Then step over the calculation and inspect:
total = 300
Breakpoints help you understand exactly what your application is doing.
17. How to Add a Breakpoint in Android Studio
Follow these steps:
Step 1: Open a Kotlin source file.
Step 2: Find the line you want to inspect.
Step 3: Click the gutter next to the line number.
A breakpoint marker appears.
Step 4: Start the application using the Debug action.
Step 5: Perform the action that triggers the breakpoint.
Android Studio pauses execution when the breakpoint is reached.
You can now inspect variables and step through the code.
18. Debugger Controls Explained
Android Studio provides several debugger controls.
| Control | Purpose |
|---|---|
| Resume Program | Continue execution |
| Step Over | Execute the current line without stepping into called functions |
| Step Into | Enter a function call |
| Step Out | Finish the current function and return to its caller |
| Evaluate Expression | Evaluate an expression in the paused context |
| Stop | Stop the debugging session |
These controls help you investigate how your application executes.
19. Debugging Jetpack Compose Button Clicks
Let’s create a simple Compose screen.
@Composable
fun DebugExampleScreen() {
var counter by remember {
mutableIntStateOf(0)
}
Column(
modifier = Modifier
.fillMaxSize()
.padding(24.dp),
verticalArrangement = Arrangement.Center,
horizontalAlignment = Alignment.CenterHorizontally
) {
Text(
text = "Counter: $counter",
style = MaterialTheme.typography.headlineMedium
)
Spacer(
modifier = Modifier.height(20.dp)
)
Button(
onClick = {
counter++
Log.d(
"ComposeDebug",
"Counter updated: $counter"
)
}
) {
Text("Increase Counter")
}
}
}
Required imports
import android.util.Log
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.material3.Button
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableIntStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.unit.dp
Expected UI
+--------------------------------+
| |
| Counter: 0 |
| |
| [ Increase Counter ] |
| |
+--------------------------------+
When you click the button, the counter increases.
Logcat displays:
D/ComposeDebug: Counter updated: 1
D/ComposeDebug: Counter updated: 2
D/ComposeDebug: Counter updated: 3
Debugging exercise
Place a breakpoint on:
counter++
Run the application in Debug mode.
Click the button and inspect the value of counter.
This is a simple way to practice debugging Compose state.
20. Debugging Jetpack Compose State
Jetpack Compose uses state to update the UI.
For example:
var username by remember {
mutableStateOf("")
}
When the state changes, Compose may recompose the affected UI.
While debugging Compose applications, pay attention to:
- State values
- State updates
- Button click handlers
- ViewModel state
- Recomposition behavior
- Navigation events
Important
A composable function can execute multiple times due to recomposition.
Avoid assuming that every composable execution represents a new screen launch or user interaction.
For example, logging directly in a composable body can produce repeated messages:
@Composable
fun ExampleScreen() {
Log.d(
"ComposeDebug",
"Composable executed"
)
Text("Hello Compose")
}
This can be useful temporarily while investigating recomposition, but it should not be treated as a reliable screen-view event.
21. Debug vs Release Builds
Android projects commonly have Debug and Release build types.
Debug build
A Debug build is designed for development and testing.
It commonly supports:
- Debugger attachment
- Development logging
- Breakpoints
- Debug-specific configuration
Release build
A Release build is intended for distribution.
It may include:
- Code shrinking
- Optimization
- Obfuscation
- Release signing
- Production configuration
Example
You can restrict certain debug logs using:
if (BuildConfig.DEBUG) {
Log.d(
"MoiksDebug",
"Debug-only message"
)
}
Depending on your Android Gradle configuration, you may need to enable generated BuildConfig fields and import your module’s BuildConfig class.
Important security practice
Never log sensitive information such as:
- Passwords
- Authentication tokens
- Private user messages
- Payment details
- Personal identification information
Production logging should be minimal and privacy-conscious.
22. Common Android Debugging Mistakes
Mistake 1: Ignoring Logcat Errors
When an application crashes, review the exception and stack trace instead of randomly changing code.
Mistake 2: Logging Everything
Excessive logs make useful information harder to find.
Use clear, meaningful tags and messages.
Mistake 3: Logging Sensitive Data
Avoid printing passwords, tokens, or private user information.
Mistake 4: Not Using Breakpoints
Logs are useful, but breakpoints allow you to inspect the actual execution state.
Mistake 5: Assuming Compose Runs Only Once
Composable functions may recompose multiple times.
Understand state and recomposition before interpreting repeated logs.
Mistake 6: Ignoring Exceptions
Avoid silently swallowing exceptions.
For example, this makes debugging difficult:
try {
performOperation()
} catch (exception: Exception) {
// Ignored
}
Handle expected failures appropriately and log useful diagnostic information during development without exposing sensitive data.
Mistake 7: Debugging the Wrong Process
If Logcat doesn’t show your expected messages, verify that you’ve selected the correct device and application process.
23. Android Logcat Quick Reference
| Method | Use Case |
|---|---|
Log.v() | Detailed diagnostic messages |
Log.d() | Debugging application behavior |
Log.i() | Informational events |
Log.w() | Potential problems |
Log.e() | Errors and exceptions |
Log.wtf() | Severe unexpected conditions |
Useful Logcat filters
tag:MoiksDebug
level:ERROR
package:mine
Debugger shortcuts to remember
- Breakpoint: Click next to a line number.
- Debug: Run using the Debug action.
- Step Over: Execute the current line.
- Step Into: Enter a called function.
- Resume: Continue execution.
24. Practical Exercises
Try these exercises in Android Studio.
Exercise 1: Log Activity Lifecycle Events
Add logs inside:
onCreate()
onStart()
onResume()
onPause()
onStop()
onDestroy()
Run the application and observe the lifecycle messages.
Exercise 2: Debug a Compose Counter
Create a counter screen.
Log the counter value whenever the user clicks the button.
Exercise 3: Use a Breakpoint
Place a breakpoint inside a Kotlin function.
Inspect variable values before and after executing a calculation.
Exercise 4: Investigate an Exception
In a disposable practice project, deliberately trigger a NumberFormatException.
Inspect the stack trace in Logcat.
Then fix the problem using:
toIntOrNull()
Exercise 5: Filter Logcat
Create logs using the tag:
MoiksDebug
Filter Logcat to display only relevant messages.
What We’ve Learned
In this tutorial, we learned:
- What Android Logcat is
- How to open Logcat
- Android logging levels
- How to write Kotlin log messages
- How to filter Logcat
- How to investigate application crashes
- How to understand stack traces
- How to use breakpoints
- How to inspect variables
- How to debug Jetpack Compose state
- Debug vs Release builds
- Logging security best practices
- Common Android debugging mistakes
Understanding Logcat and debugging tools is an essential skill for every Android developer.
As you build more complex applications using Jetpack Compose, ViewModel, Coroutines, Room, and Retrofit, these tools will become even more useful.
What’s Next?
Now that we understand how to debug Android applications, we’ll explore another important topic:
Android Build Variants Explained: Debug, Release, Gradle & APK vs AAB
In the next tutorial, we’ll cover:
- What Android build variants are
- Debug and Release builds
- Android Gradle Plugin basics
- Build types
- Product flavors
- Version codes and version names
- APK vs Android App Bundle
- Signing configurations
- Build configuration best practices
- Preparing applications for Google Play
Happy Coding!
Continue learning modern Android development with Moiks Developer Blog.