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:
| Requirement | Version |
|---|---|
| Kotlin | 1.9+ |
| KSP | 2.3+ (compatible with your Kotlin version) |
| Gradle | 8+ |
If you're new to KMP, create a project using the Kotlin Multiplatform Wizard or the Compose Multiplatform template.
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("io.github.12345debdut:anchor-di-api:x.x.x")
implementation("io.github.12345debdut:anchor-di-core:x.x.x")
implementation("io.github.12345debdut:anchor-di-compose:x.x.x")
}
}
}
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-api— Annotations. No runtime dependency; your code references these.anchor-di-core— Runtime container andAnchorobject.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("io.github.12345debdut:anchor-di-api:x.x.x")
implementation("io.github.12345debdut:anchor-di-core:x.x.x")
implementation("io.github.12345debdut:anchor-di-presentation:x.x.x") // Optional: NavigationScopeRegistry
implementation("io.github.12345debdut:anchor-di-android:x.x.x") // 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:
| Platform | Where |
|---|---|
| Android | In Application.onCreate() or before the first Composable |
| iOS | In your app entry point (e.g. main() or @main App init) before any UI |
| Desktop / JVM | Before 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
- Add a simple
@Injectclass and a module (see Quick Example). - Call
Anchor.init(*getAnchorContributors())at startup. - 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
- KMP + Compose — Details for Compose Multiplatform projects
- KMP without Compose — Details for projects using native UI
- Multi-module Setup — When your project has multiple modules with DI
- Platform-Specific Setup — iOS KSP setup, Android, and more