Quick start: Kotlin Multiplatform¶
Herald for Kotlin Multiplatform lets you write events once, in shared Kotlin, and track them from shared view models. It runs on top of the native SDKs, so each app sets Herald up as a native app does, then gives it to the shared code.
1. Add the core to your shared module¶
Add herald-kmp-core, and export it so Swift can use it:
kotlin {
listOf(iosArm64(), iosSimulatorArm64()).forEach { target ->
target.binaries.framework {
baseName = "Shared"
export("io.github.mkhytarmkhoian:herald-kmp-core:1.1.0")
}
}
sourceSets {
commonMain.dependencies {
api("io.github.mkhytarmkhoian:herald-kmp-core:1.1.0")
}
}
}
2. Write your events in shared code¶
Events and view models are the same Kotlin as on Android:
package io.github.mkhytarmkhoian.herald.sample
import io.github.mkhytarmkhoian.herald.kmp.AnalyticsValue
import io.github.mkhytarmkhoian.herald.kmp.Event
import io.github.mkhytarmkhoian.herald.kmp.EventTrackerService
import io.github.mkhytarmkhoian.herald.kmp.ScreenViewEvent
import io.github.mkhytarmkhoian.herald.kmp.parameters
// Written once, in shared code. On Android these are Android Herald events; on iOS they reach
// herald-ios through the bridge.
public object CheckoutViewed : ScreenViewEvent {
override val name: String = "checkout"
}
public class CheckoutStarted(plan: String, seats: Int, price: Double) : Event {
override val name: String = "checkout_started"
override val parameters: Map<String, AnalyticsValue> = parameters {
put("plan", plan)
put("seats", seats)
put("price", price)
}
}
public class CheckoutViewModel(private val analytics: EventTrackerService) {
public suspend fun opened(): Unit = analytics.track(CheckoutViewed)
public suspend fun started(plan: String, seats: Int): Unit =
analytics.track(CheckoutStarted(plan, seats, price = seats * 4.99))
}
3. Set Herald up in each app¶
- Android: create Herald as in the Android quick start, and pass it to your shared view models.
- iOS: create Herald as in the iOS quick start, then connect it to
your shared code through
HeraldInterop, as the Kotlin Multiplatform page shows.
Each app's log then shows the events your shared code tracked.
Next¶
- Kotlin Multiplatform: the modules, and the iOS bridge in full.
- Platform notes: where shared Kotlin differs from Android.
- Set up your app: several services, error reporting and dependency injection.