Skip to content

bugsnag/bugsnag-kotlin-multiplatform

Repository files navigation

SmartBear BugSnag logo

Error monitoring & exception reporter for Kotlin with Multiplatform support

Detect crashes in your applications using Kotlin Multiplatform: collecting diagnostic information and immediately notifying your development team, helping you to understand and resolve issues as fast as possible.

Beta SDK

This SDK is still in beta but builds upon on our existing bugsnag-android, bugsnag-cocoa and bugsnag-js SDKs. As such it can be considered stable and safe for use in production, but possibly missing features in the Kotlin common layer that are available in the platform-specific SDKs. We are working to add more features to the common layer, but if you need a specific feature, please open an issue.

Installation

Add bugsnag-kmp to your build.gradle.kts file:

dependencies {
    implementation("com.bugsnag:bugsnag-kmp:+")
}

In your Xcode project add bugsnag-cocoa as a Swift Package Manager (recommended) or other project dependency. See our online docs for instructions: https://docs.bugsnag.com/platforms/ios/#installation.

Basic configuration

The Bugsnag client should be initialized as early as possible in your application lifecycle. On Android this is typically in the onCreate method of your Application class, while on iOS this is typically in the didFinishLaunchingWithOptions method of your AppDelegate class, or in the init method of your App class. Since we have Kotlin Multiplatform support, you can share most of your configuration code:

// BugsnagStartup.kt
import com.bugsnag.kmp.Bugsnag
import com.bugsnag.kmp.Configuration

fun startBugsnag(configuration: Configuration) {
   // common configuration here
   configuration.addMetadata("App", "multiplatform", true)
   Bugsnag.start(configuration)
}

Bugsnag.start should still be called in a platform-specific location:

 // don't accidentally import 'com.bugsnag.android.Configuration' if you want common configuration
import com.bugsnag.kmp.Configuration

class MyApp : Application() {
    override fun onCreate() {
        super.onCreate()
        startBugsnag(Configuration(this, "your-api-key"))
    }
}
@main
struct iOSApp: App {
   init() {
      BugsnagStartupKt.startBugsnag(configuration: Bugsnag_kmpConfiguration(apiKey: "your-api-key"))
   }
}

Enabled error types

The Bugsnag client can be configured to ignore certain types of errors. By default, all error types are enabled. You can enable or disable error types by calling the setEnabledErrorTypes method on the Configuration object. In the bugsnag-kmp each platform-specific error type can be configured independently as part of the common source set:

import com.bugsnag.kmp.Configuration
// bugsnag-kmp includes a convenience extension function to set enabled error types in a lambda
import com.bugsnag.kmp.setEnabledErrorTypes

configuration.setEnabledErrorTypes {
   iosOoms = false
}

Showing full stacktraces

To see fully symbolicated Kotlin and native stacktraces in your dashboard, we recommend using the bugsnag-cli tool. This will find and upload the symbol files for each platform in your Kotlin Multiplatform project:

# upload the symbol files for the current build
bugsnag-cli upload android-aab app --api-key=your-api-key
bugsnag-cli upload xcode-archive iosApp --api-key=your-api-key 

If your API-key is defined in the AndroidManifest.xml and Info.plist files, you can omit the --api-key argument. The CLI will automatically find the API key in your project files. These steps are typically added to your CI/CD pipeline, so that the symbol files are uploaded automatically after each build.

Support

Contributing

All contributors are welcome!

License

The BugSnag Kotlin SDK is free software released under the MIT License. See the LICENSE for details.