Skip to content

KMP example apps #31

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 8 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions examples/ExampleApp/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
*.iml
.gradle
.idea
.DS_Store
build
captures
.externalNativeBuild
.cxx
local.properties
xcuserdata
93 changes: 93 additions & 0 deletions examples/ExampleApp/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
# Bugsnag Kotlin Multiplatform - ExampleApp

This repository demonstrates how to integrate [Bugsnag KMP](https://github.com/bugsnag/bugsnag-kotlin-multiplatform) into a Kotlin Multiplatform (KMP) application across **Android**, **iOS**, and **JavaScript Web** platforms.

## Project Structure
```plaintext
examples/ExampleApp/
├── androidApp/ # Android app with Jetpack Compose UI
├── iosExampleApp/ # iOS app with SwiftUI
├── webApp/ # Web app using Kotlin/JS and HTML
├── shared/ # Shared KMP module with Bugsnag integration
└── build.gradle.kts # Root build configuration
```
---

## Android App

### Build & Run

```bash
cd examples/ExampleApp
./gradlew clean build
Open in Android Studio
Set your Bugsnag API Key at `androidApp/src/main/java/com/example/exampleapps/android/MainActivity.kt`
```

### Features
Notify Bugsnag with a manual error

Trigger a fatal crash

Set custom metadata

Leave a breadcrumb

Simulate an ANR

## iOS App

### Build & Run

```bash
open iosExampleApp/iosExampleApp.xcworkspace
Select an ios sim or device
Set your Bugsnag API Key at `iosApp/iOSApp.swift`
Run the app
```

### Features

Manual Bugsnag notification

Use shared KMP code

Trigger crash via button

## Web App

### Build & Run

```bash
cd examples/ExampleApp
./gradlew :webApp:jsBrowserDevelopmentRun
open http://localhost:8080
```

### Features
Display UI with HTML/JS

Buttons call shared Bugsnag methods:

Manual notify

Trigger crash

Set metadata

Leave breadcrumb

## Shared KMP Module
The shared module contains the Bugsnag integration code used across all platforms. It includes:
- Bugsnag initialization
- Manual error reporting
- Custom metadata handling
- Breadcrumbs management

## Troubleshooting

Can't find shared module in iOS project?
``` bash
run ./gradlew syncFramework
link the generated framework in Xcode
```
48 changes: 48 additions & 0 deletions examples/ExampleApp/androidApp/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
plugins {
alias(libs.plugins.androidApplication)
alias(libs.plugins.kotlinAndroid)
alias(libs.plugins.compose.compiler)
}

android {
namespace = "com.example.bugsnag.kmp.android"
compileSdk = 35
defaultConfig {
applicationId = "com.example.bugsnag.kmp.android"
minSdk = 24
targetSdk = 35
versionCode = 1
versionName = "1.0"
}
buildFeatures {
compose = true
}
packaging {
resources {
excludes += "/META-INF/{AL2.0,LGPL2.1}"
}
}
buildTypes {
getByName("release") {
isMinifyEnabled = false
}
}
compileOptions {
sourceCompatibility = JavaVersion.VERSION_1_8
targetCompatibility = JavaVersion.VERSION_1_8
}
kotlinOptions {
jvmTarget = "1.8"
}
}

dependencies {
implementation(projects.shared)
implementation(libs.compose.ui)
implementation(libs.compose.ui.tooling.preview)
implementation(libs.compose.material3)
implementation(libs.androidx.activity.compose)
debugImplementation(libs.compose.ui.tooling)
implementation(project(":shared"))
implementation(libs.bugsnag.kmp)
}
20 changes: 20 additions & 0 deletions examples/ExampleApp/androidApp/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android">

<application
android:allowBackup="false"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<meta-data
android:name="com.bugsnag.android.API_KEY"
android:value="your-api-key-here" />
</application>
</manifest>
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
package com.example.bugsnag.kmp.android

import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.layout.*
import androidx.compose.material3.*
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.unit.dp
import com.bugsnag.kmp.Configuration
import com.example.ExampleApps.SharedBugsnag

class MainActivity : ComponentActivity() {

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)

val config = Configuration(applicationContext)
SharedBugsnag().startBugsnag(config)

setContent {
MyApplicationTheme {
Surface(
modifier = Modifier.fillMaxSize(),
color = MaterialTheme.colorScheme.background
) {
MainScreen()
}
}
}
}
}

@Composable
fun MainScreen() {
val bugsnag = SharedBugsnag()

Column(
modifier = Modifier
.fillMaxSize()
.padding(16.dp),
verticalArrangement = Arrangement.spacedBy(12.dp)
) {
Text("Bugsnag KMP - Android example app")

Button(
onClick = { bugsnag.manualNotify("Manual notify from Android") },
modifier = Modifier.fillMaxWidth()
) {
Text("Notify Bugsnag")
}

Button(
onClick = { bugsnag.triggerFatalCrash("Crash from Android button") },
modifier = Modifier.fillMaxWidth()
) {
Text("Trigger Fatal Crash")
}

Button(
onClick = { bugsnag.setMetadata("device", "sentFromAndroid") },
modifier = Modifier.fillMaxWidth()
) {
Text("Set Metadata")
}

Button(
onClick = { bugsnag.leaveBreadcrumbs("User tapped breadcrumb button") },
modifier = Modifier.fillMaxWidth()
) {
Text("Leave Breadcrumb")
}

Button(
onClick = {
// Manual ANR trigger: keep platform-specific code where needed
android.os.Handler(android.os.Looper.getMainLooper()).post {
Thread.sleep(10_000)
}
},
modifier = Modifier.fillMaxWidth()
) {
Text("Trigger ANR")
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package com.example.bugsnag.kmp.android

import androidx.compose.foundation.isSystemInDarkTheme
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Shapes
import androidx.compose.material3.Typography
import androidx.compose.material3.darkColorScheme
import androidx.compose.material3.lightColorScheme
import androidx.compose.runtime.Composable
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.text.font.FontFamily
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp

@Composable
fun MyApplicationTheme(
darkTheme: Boolean = isSystemInDarkTheme(),
content: @Composable () -> Unit
) {
val colors = if (darkTheme) {
darkColorScheme(
primary = Color(0xFFBB86FC),
secondary = Color(0xFF03DAC5),
tertiary = Color(0xFF3700B3)
)
} else {
lightColorScheme(
primary = Color(0xFF6200EE),
secondary = Color(0xFF03DAC5),
tertiary = Color(0xFF3700B3)
)
}
val typography = Typography(
bodyMedium = TextStyle(
fontFamily = FontFamily.Default,
fontWeight = FontWeight.Normal,
fontSize = 16.sp
)
)
val shapes = Shapes(
small = RoundedCornerShape(4.dp),
medium = RoundedCornerShape(4.dp),
large = RoundedCornerShape(0.dp)
)

MaterialTheme(
colorScheme = colors,
typography = typography,
shapes = shapes,
content = content
)
}
3 changes: 3 additions & 0 deletions examples/ExampleApp/androidApp/src/main/res/values/styles.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<resources>
<style name="AppTheme" parent="android:Theme.Material.NoActionBar"/>
</resources>
8 changes: 8 additions & 0 deletions examples/ExampleApp/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
plugins {
//trick: for the same plugin versions in all sub-modules
alias(libs.plugins.androidApplication).apply(false)
alias(libs.plugins.androidLibrary).apply(false)
alias(libs.plugins.kotlinAndroid).apply(false)
alias(libs.plugins.kotlinMultiplatform).apply(false)
alias(libs.plugins.compose.compiler).apply(false)
}
13 changes: 13 additions & 0 deletions examples/ExampleApp/gradle.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#Gradle
org.gradle.jvmargs=-Xmx2048M -Dfile.encoding=UTF-8 -Dkotlin.daemon.jvm.options\="-Xmx2048M"
org.gradle.caching=true
org.gradle.configuration-cache=true

#Kotlin
kotlin.code.style=official

#Android
android.useAndroidX=true
android.nonTransitiveRClass=true

xcodeproj=~/examples/ExampleApp/iosApp
25 changes: 25 additions & 0 deletions examples/ExampleApp/gradle/libs.versions.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
[versions]
agp = "8.10.0"
kotlin = "2.1.0"
compose = "1.5.4"
compose-material3 = "1.1.2"
androidx-activityCompose = "1.8.0"
kmp = "+"

[libraries]
kotlin-test = { module = "org.jetbrains.kotlin:kotlin-test", version.ref = "kotlin" }
androidx-activity-compose = { module = "androidx.activity:activity-compose", version.ref = "androidx-activityCompose" }
compose-ui = { module = "androidx.compose.ui:ui", version.ref = "compose" }
compose-ui-tooling = { module = "androidx.compose.ui:ui-tooling", version.ref = "compose" }
compose-ui-tooling-preview = { module = "androidx.compose.ui:ui-tooling-preview", version.ref = "compose" }
compose-foundation = { module = "androidx.compose.foundation:foundation", version.ref = "compose" }
compose-material3 = { module = "androidx.compose.material3:material3", version.ref = "compose-material3" }
bugsnag-kmp = { module = "com.bugsnag:bugsnag-kmp", version.ref = "kmp" }

[plugins]
androidApplication = { id = "com.android.application", version.ref = "agp" }
androidLibrary = { id = "com.android.library", version.ref = "agp" }
kotlinAndroid = { id = "org.jetbrains.kotlin.android", version.ref = "kotlin" }
kotlinMultiplatform = { id = "org.jetbrains.kotlin.multiplatform", version.ref = "kotlin" }
kotlinCocoapods = { id = "org.jetbrains.kotlin.native.cocoapods", version.ref = "kotlin" }
compose-compiler = { id = "org.jetbrains.kotlin.plugin.compose", version.ref = "kotlin" }
Binary file not shown.
6 changes: 6 additions & 0 deletions examples/ExampleApp/gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#Thu Jun 05 16:09:23 BST 2025
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-8.11.1-bin.zip
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
Loading