|
| 1 | +--- |
| 2 | +title: "Sentry Kotlin Multiplatform in Compose Multiplatform" |
| 3 | +description: "Learn how to integrate Sentry's Kotlin Multiplatform SDK into your Compose Multiplatform project for cross-platform error tracking." |
| 4 | +sdk: sentry.kotlin.multiplatform |
| 5 | +categories: |
| 6 | + - kotlin |
| 7 | + - multiplatform |
| 8 | +sidebar_order: 15 |
| 9 | +--- |
| 10 | + |
| 11 | +# Sentry Kotlin Multiplatform in Compose Multiplatform |
| 12 | + |
| 13 | +<Alert level="warning" title="Experimental Support"> |
| 14 | +While Sentry's Kotlin Multiplatform SDK works in Compose Multiplatform projects, it isn't natively supported or fully tested for CMP. Some features may behave unexpectedly or provide incomplete data. Use with caution in production environments. |
| 15 | +</Alert> |
| 16 | + |
| 17 | +## Overview |
| 18 | + |
| 19 | +**Compose Multiplatform** is JetBrains' declarative UI framework that allows you to build native user interfaces for desktop, web, and mobile platforms using a single Kotlin codebase. It extends Jetpack Compose beyond Android to create truly multiplatform applications. |
| 20 | + |
| 21 | +**Sentry's Kotlin Multiplatform SDK** provides error tracking and performance monitoring capabilities across multiple Kotlin targets. While the SDK functions in Compose Multiplatform projects using the same APIs as pure KMP setups, it's important to note: |
| 22 | + |
| 23 | +- ✅ **Supported**: The KMP SDK works in CMP projects |
| 24 | +- ⚠️ **Not CMP-native**: No specific CMP optimizations or testing |
| 25 | +- 🔍 **Untested edge cases**: Some platform-specific behaviors may be incomplete |
| 26 | + |
| 27 | +## Prerequisites |
| 28 | + |
| 29 | +Before integrating Sentry into your Compose Multiplatform project, ensure you have: |
| 30 | + |
| 31 | +### Development Environment |
| 32 | +- **Kotlin 1.9.0+** and compatible JVM (Java 11+) |
| 33 | +- **Gradle 7.6+** with Kotlin DSL support |
| 34 | +- **Compose Multiplatform plugin** configured in your project |
| 35 | + |
| 36 | +### Project Setup |
| 37 | +- A working Compose Multiplatform project targeting your desired platforms: |
| 38 | + - **JVM/Desktop**: Windows, macOS, Linux |
| 39 | + - **Android**: Mobile applications |
| 40 | + - **iOS**: iPhone/iPad applications (experimental) |
| 41 | + |
| 42 | +### Sentry Configuration |
| 43 | +- A [Sentry project](https://sentry.io) with your **DSN** (Data Source Name) |
| 44 | +- Basic understanding of Sentry error tracking concepts |
| 45 | + |
| 46 | +## Installation |
| 47 | + |
| 48 | +### 1. Add Sentry Dependency |
| 49 | + |
| 50 | +Add the Sentry Kotlin Multiplatform SDK to your `commonMain` source set in `build.gradle.kts`: |
| 51 | + |
| 52 | +```kotlin {filename:build.gradle.kts} |
| 53 | +kotlin { |
| 54 | + sourceSets { |
| 55 | + commonMain.dependencies { |
| 56 | + implementation("io.sentry:sentry-kotlin-multiplatform:0.7.1") |
| 57 | + } |
| 58 | + } |
| 59 | +} |
| 60 | +``` |
| 61 | + |
| 62 | +### 2. Platform-Specific Considerations |
| 63 | + |
| 64 | +The Sentry KMP SDK automatically includes platform-specific artifacts. No additional dependencies are required for basic functionality across JVM, Android, and iOS targets. |
| 65 | + |
| 66 | +<Alert level="info"> |
| 67 | +Platform-specific features (like native crash reporting) may not be fully available or tested in CMP contexts. |
| 68 | +</Alert> |
| 69 | + |
| 70 | +## Basic Usage in CMP |
| 71 | + |
| 72 | +### 1. Initialize Sentry |
| 73 | + |
| 74 | +Initialize Sentry in your main application entry point, typically in your `main()` function or `App` composable: |
| 75 | + |
| 76 | +```kotlin {filename:App.kt} |
| 77 | +import io.sentry.kotlin.multiplatform.Sentry |
| 78 | +import androidx.compose.runtime.Composable |
| 79 | + |
| 80 | +@Composable |
| 81 | +fun App() { |
| 82 | + // Initialize Sentry when your app starts |
| 83 | + Sentry.init { options -> |
| 84 | + options.dsn = "___PUBLIC_DSN___" |
| 85 | + options.debug = true // Enable for development |
| 86 | + options.environment = "development" |
| 87 | + } |
| 88 | + |
| 89 | + // Your Compose UI content |
| 90 | + MainContent() |
| 91 | +} |
| 92 | + |
| 93 | +@Composable |
| 94 | +fun MainContent() { |
| 95 | + // Your app's UI components |
| 96 | +} |
| 97 | +``` |
| 98 | + |
| 99 | +### 2. Capture Errors and Exceptions |
| 100 | + |
| 101 | +Use Sentry to capture exceptions and errors throughout your application: |
| 102 | + |
| 103 | +```kotlin {filename:ErrorHandling.kt} |
| 104 | +import io.sentry.kotlin.multiplatform.Sentry |
| 105 | + |
| 106 | +@Composable |
| 107 | +fun ExampleScreen() { |
| 108 | + Button( |
| 109 | + onClick = { |
| 110 | + try { |
| 111 | + // Risky operation that might fail |
| 112 | + performRiskyOperation() |
| 113 | + } catch (e: Exception) { |
| 114 | + // Capture the exception with Sentry |
| 115 | + Sentry.captureException(e) |
| 116 | + // Handle the error in your UI |
| 117 | + } |
| 118 | + } |
| 119 | + ) { |
| 120 | + Text("Perform Action") |
| 121 | + } |
| 122 | +} |
| 123 | + |
| 124 | +fun performRiskyOperation() { |
| 125 | + // Example operation that might throw |
| 126 | + throw RuntimeException("Something went wrong!") |
| 127 | +} |
| 128 | +``` |
| 129 | + |
| 130 | +### 3. Add Breadcrumbs for Context |
| 131 | + |
| 132 | +Breadcrumbs help trace user actions leading up to an error: |
| 133 | + |
| 134 | +```kotlin {filename:BreadcrumbExample.kt} |
| 135 | +import io.sentry.kotlin.multiplatform.Sentry |
| 136 | +import io.sentry.kotlin.multiplatform.protocol.Breadcrumb |
| 137 | + |
| 138 | +@Composable |
| 139 | +fun NavigationExample() { |
| 140 | + LaunchedEffect(Unit) { |
| 141 | + // Add breadcrumb when screen loads |
| 142 | + Sentry.addBreadcrumb( |
| 143 | + Breadcrumb().apply { |
| 144 | + message = "User navigated to main screen" |
| 145 | + category = "navigation" |
| 146 | + level = SentryLevel.INFO |
| 147 | + } |
| 148 | + ) |
| 149 | + } |
| 150 | + |
| 151 | + Button( |
| 152 | + onClick = { |
| 153 | + // Add breadcrumb for user actions |
| 154 | + Sentry.addBreadcrumb("User clicked primary button", "ui.click") |
| 155 | + handleButtonClick() |
| 156 | + } |
| 157 | + ) { |
| 158 | + Text("Click Me") |
| 159 | + } |
| 160 | +} |
| 161 | +``` |
| 162 | + |
| 163 | +## Feature Support |
| 164 | + |
| 165 | +The following Sentry KMP features **should** work in Compose Multiplatform projects, though with varying levels of reliability: |
| 166 | + |
| 167 | +### ✅ Core Features (Expected to Work) |
| 168 | +- **Error Tracking**: Exception capture and reporting |
| 169 | +- **Breadcrumbs**: Manual breadcrumb creation and context tracking |
| 170 | +- **Custom Tags**: Adding metadata to events |
| 171 | +- **User Context**: Setting user information |
| 172 | +- **Environment Configuration**: Debug modes, environment settings |
| 173 | + |
| 174 | +### ⚠️ Advanced Features (Partially Tested) |
| 175 | +- **Performance Monitoring**: Transaction tracking (basic support) |
| 176 | +- **Attachments**: File and screenshot attachments (platform-dependent) |
| 177 | +- **Release Health**: Session tracking (may have gaps) |
| 178 | + |
| 179 | +### ❓ Platform-Specific Features (Unknown Status) |
| 180 | +- **Native Crash Reporting**: OS-level crash detection |
| 181 | +- **Automatic Error Boundary**: UI framework integration |
| 182 | +- **Platform-Specific Context**: Device/OS information |
| 183 | + |
| 184 | +## Known Limitations & Caveats |
| 185 | + |
| 186 | +<Alert level="warning" title="Important Considerations"> |
| 187 | +Keep these limitations in mind when using Sentry KMP in Compose Multiplatform: |
| 188 | +</Alert> |
| 189 | + |
| 190 | +### Untested CMP Behaviors |
| 191 | +- **Compose Navigation**: Screen transition tracking may be incomplete |
| 192 | +- **State Management**: Compose state-related errors might not be captured optimally |
| 193 | +- **Hot Reload**: Development-time behavior with Compose hot reload is unknown |
| 194 | + |
| 195 | +### Data Discrepancies |
| 196 | +- **Performance Metrics**: Frame rates and UI performance data may be inaccurate |
| 197 | +- **Platform Context**: Device information might be missing or incorrect on some targets |
| 198 | +- **Session Tracking**: App lifecycle events may not align with Compose lifecycle |
| 199 | + |
| 200 | +### Missing Platform Hooks |
| 201 | +- **Desktop Platforms**: Limited system integration compared to mobile SDKs |
| 202 | +- **iOS Integration**: Native iOS features may not work as expected |
| 203 | +- **WebAssembly**: If targeting web, functionality will be significantly limited |
| 204 | + |
| 205 | +## Troubleshooting Tips |
| 206 | + |
| 207 | +### Common Setup Issues |
| 208 | + |
| 209 | +**Problem**: Events not appearing in Sentry |
| 210 | +```kotlin |
| 211 | +// ❌ Incorrect: DSN not set or invalid |
| 212 | +Sentry.init { } // Missing DSN |
| 213 | + |
| 214 | +// ✅ Correct: Always set your DSN |
| 215 | +Sentry.init { options -> |
| 216 | + options.dsn = "___PUBLIC_DSN___" |
| 217 | + options.debug = true // Helps identify issues |
| 218 | +} |
| 219 | +``` |
| 220 | + |
| 221 | +**Problem**: Exceptions not being captured |
| 222 | +```kotlin |
| 223 | +// ❌ Incorrect: Exception swallowed silently |
| 224 | +try { |
| 225 | + riskyOperation() |
| 226 | +} catch (e: Exception) { |
| 227 | + // Silent failure |
| 228 | +} |
| 229 | + |
| 230 | +// ✅ Correct: Always capture exceptions |
| 231 | +try { |
| 232 | + riskyOperation() |
| 233 | +} catch (e: Exception) { |
| 234 | + Sentry.captureException(e) |
| 235 | + handleError(e) |
| 236 | +} |
| 237 | +``` |
| 238 | + |
| 239 | +### Verification Steps |
| 240 | + |
| 241 | +1. **Check Sentry Dashboard**: Look for events in your Sentry project dashboard |
| 242 | +2. **Enable Debug Mode**: Set `options.debug = true` to see SDK logs |
| 243 | +3. **Test Exception Capture**: Manually trigger an exception to verify capture |
| 244 | +4. **Review Platform Logs**: Check console output for Sentry-related messages |
| 245 | + |
| 246 | +### Debug Configuration |
| 247 | + |
| 248 | +```kotlin {filename:DebugConfig.kt} |
| 249 | +Sentry.init { options -> |
| 250 | + options.dsn = "___PUBLIC_DSN___" |
| 251 | + options.debug = true |
| 252 | + options.beforeSend = { event -> |
| 253 | + println("Sending event: ${event.eventId}") |
| 254 | + event // Return event to send, or null to drop |
| 255 | + } |
| 256 | +} |
| 257 | +``` |
| 258 | + |
| 259 | +## Next Steps & Updates |
| 260 | + |
| 261 | +### Stay Updated |
| 262 | +- **Watch Official Docs**: Monitor [Sentry's Kotlin documentation](https://docs.sentry.io/platforms/kotlin/) for CMP-specific updates |
| 263 | +- **GitHub Repository**: Follow the [sentry-kotlin-multiplatform repository](https://github.com/getsentry/sentry-kotlin-multiplatform) for issues and improvements |
| 264 | +- **Community Feedback**: Report CMP-specific issues to help improve support |
| 265 | + |
| 266 | +### Recommended Practices |
| 267 | +- **Start Small**: Begin with basic error tracking before adding advanced features |
| 268 | +- **Test Thoroughly**: Verify functionality on all your target platforms |
| 269 | +- **Monitor Performance**: Watch for any performance impacts on your Compose UI |
| 270 | +- **Fallback Plans**: Have alternative error handling for critical app functions |
| 271 | + |
| 272 | +### Future Considerations |
| 273 | +Native Compose Multiplatform support is not currently on Sentry's official roadmap, but community feedback and adoption may influence future development priorities. |
| 274 | + |
| 275 | +<Alert level="info" title="Contributing"> |
| 276 | +If you encounter CMP-specific issues or have suggestions for better integration, consider contributing to the [Sentry Kotlin Multiplatform project](https://github.com/getsentry/sentry-kotlin-multiplatform) or opening feature requests. |
| 277 | +</Alert> |
0 commit comments