Advanced KMP starter — modular observability (api / spi / impl), Metro DI, and Firebase (Android + iOS SPM). Not a minimal blank Compose app; see shared/README.md for architecture rationale.
Kotlin Multiplatform starter with modular observability (api / spi / impl), Metro DI composition in shared:di, and an iOS-only shared:umbrella framework. Targets Android, iOS, Desktop (JVM), JS, and Wasm.
For architecture decisions and provider options, see shared/README.md. For broader modularization context, see Android at scale @Square. For clone/setup/rename/Firebase, see docs/TEMPLATE.md. To contribute, see CONTRIBUTING.md.
Kotlin 2.4.0-RC is required today for iOS Firebase via Kotlin
swiftPMDependencies. Upgrade to stable 2.4.x ingradle/libs.versions.tomlwhen available.
- JDK 21
- Android Studio (latest stable)
- Xcode 16+ (for iOS)
- Firebase Android config:
androidApp/google-services.json(copy fromandroidApp/google-services.json.example; the real file is gitignored)
This project follows the new JetBrains KMP default structure: one shared UI library + per-platform entry-point modules.
| Module | Role |
|---|---|
sharedUI |
Compose Multiplatform UI library — consumed by androidApp, desktopApp, webApp |
androidApp |
Android application entry point (Firebase plugins applied here) |
desktopApp |
Desktop (JVM) Compose application |
webApp |
Browser (JS + Wasm) Compose application |
iosApp |
Xcode/SwiftUI application; links SharedUmbrella.framework |
shared:di |
Metro graphs and factories — use on Android/desktop/web |
shared:umbrella |
SharedUmbrella.framework + API exports — iOS only; the app modules must not depend on it |
platform-observability-firebase |
Firebase SPI adapters and bindings |
platform-observability-noop |
No-op observability for JVM / JS / Wasm |
shared/observability |
api, spi, impl feature slices |
shared/core |
Domain, foundation, logger modules |
build-logic |
Convention plugins |
- Android / JVM / JS / Wasm app modules depend on
sharedUI(Compose UI) +shared:di(Metro graphs); feature code usesobservability-apitypes viasharedUI'sapiexposure. - iOS:
iosApplinksSharedUmbrella; Swift callsIosAppDependencyGraphFactory(exported fromshared:dithrough umbrella). - Providers (
platform-observability-*) supply bindings only; they do not own the app graph.
Each app module (androidApp, desktopApp, webApp) has a Gradle guard that prevents accidental dependencies on :shared:umbrella.
This template uses Metro with aggregation (Anvil-style): implementations in feature/provider modules contribute into a single app scope graph in shared:di. The KMP convention plugin enables generateContributionHints so bindings merge across modules without listing every class on the graph.
Official docs: Quick Start · Aggregation · Bindings / multibindings
| Piece | Role in this repo |
|---|---|
@DependencyGraph(scope = …) |
Declares the app graph; Metro merges all contributions for that scope |
@ContributesBinding |
“This class is the one implementation of interface X” |
@ContributesIntoSet |
“This class is one of many implementations collected into Set<X>” |
@IntoSet / @ElementsIntoSet |
Same set contribution, but from a @Provides / @Binds in a @BindingContainer |
@ContributesTo + @BindingContainer |
“This module object provides things Metro cannot construct from a constructor” (@Provides) |
@Inject |
Optional on @Contributes* types (implicit inject is on by default); required when you have multiple constructors |
binding<Interface>() |
Use when the bound type is not the only supertype, or you need an explicit binding target |
Rule of thumb: prefer @ContributesBinding / @ContributesIntoSet on your own types; use @ContributesTo + @Provides (with @IntoSet when needed) only for third-party SDKs, factories, or conditional wiring.
The graph declares scope only — no bindingContainers list. Platform-specific graphs pick up whatever is on their compile classpath (Firebase on Android/iOS, noop on JVM/JS/Wasm).
@DependencyGraph(scope = AppScope::class)
interface AndroidDependencyGraph : BaseDependencyGraph {
@DependencyGraph.Factory
fun interface Factory {
fun create(): AndroidDependencyGraph
}
}Expose dependencies via graph interfaces (ObservabilityGraph, BaseDependencyGraph), then create the graph from the app:
val graph = AndroidDependencyGraphFactory.createGraph()
graph.analyticsLogger.track(myEvent)Use on classes you own: feature impl (DefaultAnalyticsLogger), platform adapters (FirebaseAnalyticsPlatform), or no-ops (NoOpAnalyticsLogger).
Metro injects constructor parameters automatically; @Inject is optional unless you need to be explicit.
@ContributesBinding(
scope = AppScope::class,
binding = binding<AnalyticsLogger>(),
)
class DefaultAnalyticsLogger(
private val platform: AnalyticsPlatform,
) : AnalyticsLogger { /* … */ }Platform adapter (SPI) — same pattern; Firebase SDK types come from a @Provides container (below):
@ContributesBinding(
scope = AppScope::class,
binding = binding<AnalyticsPlatform>(),
)
class FirebaseAnalyticsPlatform(
private val firebase: FirebaseAnalytics,
) : AnalyticsPlatform { /* … */ }Use binding<SomeInterface>() when the implementation has multiple supertypes or you bind to an interface that is not the only parent.
Use when there is no injectable constructor: third-party SDK entry points, object factories, or choosing between qualified bindings.
@ContributesTo(AppScope::class)
@BindingContainer
object FirebaseSdkBindings {
@Provides
fun provideFirebaseAnalytics(): FirebaseAnalytics = Firebase.analytics
}Debug vs release selection (qualifiers @Debug / @Release on contributed CrashPlatform types):
@ContributesTo(AppScope::class)
@BindingContainer
object FirebaseObservabilityBindings {
@Provides
fun provideCrashPlatform(
@Debug debugVersion: CrashPlatform,
@Release releaseVersion: CrashPlatform,
): CrashPlatform = if (isDebug) debugVersion else releaseVersion
}Kermit Logger is provided here; DefaultKMPLogger is a @ContributesBinding that receives it — see LoggerBindings.
Use when you need several implementations of the same type collected into an immutable Set<T> — feature plugins, screen registrations, validators, initializers. Prefer this over @ContributesBinding when “one winner” is the wrong model and each module should add an entry without a central registry.
Official docs: Aggregation — @ContributesIntoSet · Bindings — Multibindings
This is the registration style used for multi-feature screens (e.g. a PresenterRegistration / UiRegistration plugin per feature module):
- Define a small plugin interface in an api module.
- Each feature impl contributes one class with
@ContributesIntoSet(SomeScope::class). - The
@DependencyGraphfor that scope injectsSet<Plugin>into a@Providesand folds it (oftenfirstNotNullOfOrNull) into a single factory / facade the app consumes.
/** API module — one plugin per feature; return null if you don't handle the request. */
fun interface PresenterRegistration {
fun create(screen: Screen, navigator: Navigator): Presenter<*>?
}
/** Feature impl — contributes into AppScope Set<PresenterRegistration>. */
@ContributesIntoSet(AppScope::class)
class AuthPresenterRegistration(
private val repository: AuthRepository,
) : PresenterRegistration {
override fun create(screen: Screen, navigator: Navigator): Presenter<*>? =
when (screen) {
is SignInScreen -> SignInPresenter(repository, navigator)
else -> null
}
}
/** Another feature — same set, no central list to update. */
@ContributesIntoSet(AppScope::class)
class HomePresenterRegistration(
private val repository: HomeRepository,
) : PresenterRegistration { /* … */ }
/**
* Graph for that scope — Metro aggregates every @ContributesIntoSet contribution
* into registrations: Set<PresenterRegistration>, then you fold it once.
*/
@DependencyGraph(AppScope::class)
interface AppGraph {
val presenterFactory: Presenter.Factory
@Provides
fun providePresenterFactory(
registrations: Set<PresenterRegistration>,
): Presenter.Factory =
object : Presenter.Factory {
override fun create(
screen: Screen,
navigator: Navigator,
context: CircuitContext,
): Presenter<*>? =
registrations.firstNotNullOfOrNull { it.create(screen, navigator) }
}
}Scoped sets: contribute to different scopes when the plugins should not mix. Example: @ContributesIntoSet(AppScope::class) for signed-out screens, @ContributesIntoSet(UserScope::class) for signed-in screens, with a child @DependencyGraph(UserScope::class) that folds its own Set<PresenterRegistration>:
@DependencyGraph(UserScope::class)
interface UserGraph {
val presenterFactory: Presenter.Factory
@Provides
fun providePresenterFactory(
registrations: Set<PresenterRegistration>,
): Presenter.Factory =
object : Presenter.Factory {
override fun create(
screen: Screen,
navigator: Navigator,
context: CircuitContext,
): Presenter<*>? =
registrations.firstNotNullOfOrNull { it.create(screen, navigator) }
}
}
@ContributesIntoSet(UserScope::class)
class HomePlaceholderPresenterRegistration(
private val sessionHandle: AuthSessionHandle,
) : PresenterRegistration { /* only present after sign-in */ }Metro notes that matter for this pattern:
- One declared supertype → that type is the set element. Multiple supertypes →
binding = binding<PresenterRegistration>(). - Set contributions are additive and do not support
priority. Usereplaces = […]or graphexcludesto drop a contribution. - Empty multibindings are a compile error by default. If the set may be empty, declare
@Multibinds(allowEmpty = true)on the accessor. - Keep
generateContributionHints(andgenerateContributionHintsInFirwhen aggregating into Native/iOS graphs) on so feature modules’ set contributions are visible to the graph module.
@ContributesIntoSet is for your injectable classes (the registration pattern above). Use @Provides + @IntoSet (or @ElementsIntoSet) in a @ContributesTo @BindingContainer when the contribution is not a constructable type you own — SDK values, adapters without @Inject, or providing several elements at once:
@ContributesTo(AppScope::class)
@BindingContainer
object ExtraRegistrationBindings {
@Provides
@IntoSet
fun provideLegacyRegistration(): PresenterRegistration = LegacyPresenterRegistration()
@Provides
@ElementsIntoSet
fun providePlatformRegistrations(): Set<PresenterRegistration> =
setOf(PlatformARegistration(), PlatformBRegistration())
}Class-level @ContributesIntoSet and container @IntoSet / @ElementsIntoSet merge into the same Set<T> for that scope.
For keyed lookups use @ContributesIntoMap / @IntoMap with a map key (@StringKey, @ClassKey, …). Map contributions support priority for the same key; sets do not.
@SingleIn(AppScope::class)
@ContributesBinding(scope = AppScope::class, binding = binding<KMPLogger>())
class DefaultKMPLogger(private val logger: Logger) : KMPLogger- Define API in
shared/<feature>/api(interfaces the graph exposes). - Implement with
@ContributesBinding(AppScope::class)inimpl(or in aplatform-*module for platform code). Use@ContributesIntoSetwhen the feature needs many plugins of one type instead of a single binding. - Add
implementation(projects.shared.<feature>.impl)(or the platform module) to the matching source set inshared/di/build.gradle.kts. - Extend
BaseDependencyGraph/ObservabilityGraphwith newvalproperties — Metro will require bindings for them at compile time. For multibindings, exposeSet<T>(or declare@Multibinds(allowEmpty = true)if empty is allowed).
Do not duplicate the same binding with both @ContributesBinding and @Provides for the same type. Do not use @ContributesBinding and @ContributesIntoSet for the same “one vs many” intent on the same type without a clear design — pick one model.
For ready-to-use AI prompts that scaffold a full feature (api / spi / impl with data layer, use cases, presentation, and Metro wiring) for each provider option, see docs/prompts.
| Pattern | Example |
|---|---|
| Graph + factory | shared/di |
Feature facade @ContributesBinding |
shared/observability/impl |
Firebase SDK @Provides |
FirebaseSdkBindings |
Platform @ContributesBinding |
FirebaseAnalyticsPlatform |
| JVM/JS/Wasm no-ops | platform-observability-noop |
Multibindings (@ContributesIntoSet → Set<T> fold) |
Not shipped in this sample (no Circuit/plugin registry yet) — follow §4 when adding feature plugins / screen registrations |
- Clone the repo.
- Copy
androidApp/google-services.json.example→androidApp/google-services.jsonand replace placeholders with your Firebase Android app config. - Android:
./gradlew :androidApp:assembleDebug - Desktop:
./gradlew :desktopApp:run - iOS: See docs/TEMPLATE.md — Firebase iOS (SPM resolve + Xcode).
Optional: rename package with scripts/rename-package.sh (see TEMPLATE.md).
./gradlew :androidApp:assembleDebug./gradlew :desktopApp:runWasm (recommended):
./gradlew :webApp:wasmJsBrowserDevelopmentRunJS (older browsers):
./gradlew :webApp:jsBrowserDevelopmentRunOpen iosApp in Xcode and run, after resolving Kotlin frameworks (see docs/TEMPLATE.md).
CI is orchestrated with Fastlane lanes (build/test only, no store deploy). Install once with bundle install, then run the same checks as GitHub Actions:
bundle exec fastlane ci_android
bundle exec fastlane ci_desktop
bundle exec fastlane ci_web
bundle exec fastlane ci_ios # macOS only
bundle exec fastlane ci_all # all platforms; skips iOS off macOSSee CONTRIBUTING.md — Fastlane CI for the lane ↔ job mapping.
This project is licensed under the Apache License 2.0.
You may use, modify, and distribute this template — including in proprietary commercial applications — as long as you comply with the license (retain copyright notices, state changes where required, and include a copy of the license in distributions that ship the licensed code). No additional permission is required from the maintainer.
See LICENSE for the full legal text.