Skip to main content

Installation Setup

This page walks you through adding Anchor DI to your Kotlin Multiplatform project step by step. Whether you're starting a new project or adding to an existing one, follow these steps to get Anchor DI running.


Prerequisites​

Before you begin, ensure your project meets these requirements:

RequirementVersion
Kotlin1.9+
KSP2.3+ (compatible with your Kotlin version)
Gradle8+

If you're new to KMP, create a project using the Kotlin Multiplatform Wizard or the Compose Multiplatform template.


The fastest way to set up Anchor DI. The convention plugin handles KSP wiring, dependencies, and multi-module configuration automatically.

In your shared module's build.gradle.kts:

plugins {
id("anchor-di-convention")
}

// Optional configuration (defaults shown):
anchorDi {
compose = true // adds anchor-di-compose + anchor-di-presentation
moduleId = project.name // override for multi-module projects
}

That's it. The plugin auto-applies KSP, adds anchor-di-api and anchor-di-core to commonMain, and wires the KSP processor to all active KMP targets.

BOM for Version Alignment

Use the BOM to align all Anchor DI module versions with a single declaration:

kotlin {
sourceSets {
commonMain.dependencies {
implementation(platform("io.github.12345debdut:anchor-di-bom:x.x.x"))
// Now you can omit versions on individual modules:
implementation("io.github.12345debdut:anchor-di-api")
implementation("io.github.12345debdut:anchor-di-core")
}
}
}

Option B: Manual Setup​

If you prefer full control over dependencies and KSP configuration, follow these steps.

Step 1: Add the KSP Plugin​

KSP (Kotlin Symbol Processing) is required for Anchor DI to generate code at compile time. Add the KSP plugin to your project.

In your project-level build.gradle.kts:

plugins {
id("com.google.devtools.ksp") version "2.3.5" apply false
}

The apply false means the plugin is available to subprojects but not applied at the project level. You'll apply it in the module that uses Anchor DI.

Step 2: Add Dependencies to Your Shared Module​

Add Anchor DI dependencies to the module that contains your shared code (typically the one with commonMain).

For KMP + Compose Multiplatform​

If you use Compose Multiplatform for UI (Android, iOS, Desktop, Web), add these dependencies:

// shared/build.gradle.kts (or your shared module)
plugins {
id("com.google.devtools.ksp")
}

repositories {
mavenCentral()
}

kotlin {
sourceSets {
commonMain.dependencies {
implementation(platform("io.github.12345debdut:anchor-di-bom:x.x.x"))
implementation("io.github.12345debdut:anchor-di-api")
implementation("io.github.12345debdut:anchor-di-core")
implementation("io.github.12345debdut:anchor-di-compose")
}
}
}

dependencies {
add("kspCommonMainMetadata", "io.github.12345debdut:anchor-di-ksp:x.x.x")
add("kspAndroid", "io.github.12345debdut:anchor-di-ksp:x.x.x")
add("kspIosArm64", "io.github.12345debdut:anchor-di-ksp:x.x.x")
add("kspIosSimulatorArm64", "io.github.12345debdut:anchor-di-ksp:x.x.x")
}

What each dependency does:

  • anchor-di-bom — Bill of Materials. Aligns all module versions so you only specify one version.
  • anchor-di-api — Annotations. No runtime dependency; your code references these.
  • anchor-di-core — Runtime container and Anchor object.
  • anchor-di-compose — anchorInject(), viewModelAnchor(), NavigationScopedContent, navigationScopedInject().
  • anchor-di-ksp — KSP processor. Must be added for each Kotlin target you use (kspCommonMainMetadata, kspAndroid, kspIosArm64, kspIosSimulatorArm64, etc.).

For KMP Without Compose​

If you use native UI (SwiftUI on iOS, Views on Android) or shared logic only, omit anchor-di-compose and add:

commonMain.dependencies {
implementation(platform("io.github.12345debdut:anchor-di-bom:x.x.x"))
implementation("io.github.12345debdut:anchor-di-api")
implementation("io.github.12345debdut:anchor-di-core")
implementation("io.github.12345debdut:anchor-di-presentation") // Optional: NavigationScopeRegistry
implementation("io.github.12345debdut:anchor-di-android") // Optional: ActivityScope (Android)
}

See KMP without Compose for details.


Step 3: Initialize at Startup​

You must call Anchor.init(*getAnchorContributors()) once at app startup. The function getAnchorContributors() is generated by KSP — it returns an array of ComponentBindingContributor implementations that register all your bindings.

Where to call it:

PlatformWhere
AndroidIn Application.onCreate() or before the first Composable
iOSIn your app entry point (e.g. main() or @main App init) before any UI
Desktop / JVMBefore creating any UI or resolving dependencies

Example (Android):

class MyApplication : Application() {
override fun onCreate() {
super.onCreate()
Anchor.init(*getAnchorContributors())
}
}

Example (Compose — commonMain):

@Composable
fun App() {
DisposableEffect(Unit) {
Anchor.init(*getAnchorContributors())
onDispose { }
}
AppContent()
}

Important for iOS: KSP generates code into target-specific directories that iosMain cannot see. You need an actual for getAnchorContributors() in both iosArm64Main and iosSimulatorArm64Main (not iosMain). See Platform-Specific Setup.


Step 4: Build​

Run a build so KSP can process your code and generate the contributor:

./gradlew build

Or use your IDE's build. After the first successful build, getAnchorContributors() will be available. If you see "Unresolved reference: getAnchorContributors", ensure KSP ran (check the build output) and that you've added at least one @Inject class or @Module.


Verifying the Setup​

  1. Add a simple @Inject class and a module (see Quick Example).
  2. Call Anchor.init(*getAnchorContributors()) at startup.
  3. Call Anchor.inject<YourType>() somewhere and run the app.

If you get "No binding found" or "Scoped binding requires a scope", check the Troubleshooting guide.


Next Steps​