ZacSweers/metro

A multiplatform, compile-time dependency injection framework for Kotlin

Kotlin

1,408

2,558 commits

updated Oct 12, 2026

See the code

README

πŸš‡ Metro

A compile-time dependency injection framework for Kotlin Multiplatform, powered by a Kotlin compiler plugin.

Maven Central Kotlin Build Status License


What is Metro?

Metro is a compile-time dependency injection framework that combines the best of Dagger, Anvil, and kotlin-inject into one cohesive solution.

Key Features:

  • βœ… Compile-time validation – Catch dependency graph errors during compilation, not at runtime
  • 🧩 FIR/IR code generation – No KAPT or KSP required, just a Kotlin compiler plugin
  • 🎯 Kotlin-first API – Inspired by kotlin-inject with top-level function injection and optional dependencies
  • πŸ—‘οΈ Dagger-esque runtime – Lean generated code with familiar patterns
  • βš’οΈ Anvil-style aggregation – @ContributesTo, @ContributesBinding, and more
  • 🌐 Multiplatform – Supports JVM, JS, Wasm, and Native targets
  • πŸ’‘ Helpful diagnostics – Detailed error messages with actionable suggestions
  • πŸ”— Advanced interop – Migrate incrementally from Dagger, kotlin-inject, or Guice
  • ⚑️ Fast - At build time, at runtime

Quick Start

1. Apply the Gradle plugin:

plugins {
  id("dev.zacsweers.metro") version "<version>"
}

2. Define a dependency graph:

@DependencyGraph
interface AppGraph {
  val repository: UserRepository

  @Provides
  fun provideApi(): Api = ApiImpl()
}

@Inject
class UserRepository(private val api: Api)

3. Create and use the graph:

val graph = createGraph<AppGraph>()
val repository = graph.repository

Documentation

πŸ“š zacsweers.github.io/metro

Topic
InstallationSetup and configuration
Dependency GraphsDefine and create graphs
ProvidesProvider functions and properties
Injection TypesConstructor, assisted, and member injection
ScopesScoping and lifecycle management
Coroutines SupportSuspend bindings and deferred initialization
AggregationAnvil-style contributions across modules
InteropDagger, kotlin-inject, and Guice compatibility
PerformanceBuild and runtime performance
CompatibilitySupported Kotlin versions
FAQFrequently asked questions
API DocsGenerated KDocs

Supported Platforms

Metro supports JVM, Android, JS, Wasm, and Native targets. The compiler plugin works with all Kotlin Multiplatform project types.

See the multiplatform docs for full details.


License

Copyright (C) 2025 Zac Sweers

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

   https://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
compiler-plugin
dependency-injection
di
jakarta-inject
javax-inject
jsr-330
kotlin

ZacSweers/metro

A multiplatform, compile-time dependency injection framework for Kotlin

Kotlin

1,408

2,558 commits

updated Oct 12, 2026

See the code

README

πŸš‡ Metro

A compile-time dependency injection framework for Kotlin Multiplatform, powered by a Kotlin compiler plugin.

Maven Central Kotlin Build Status License


What is Metro?

Metro is a compile-time dependency injection framework that combines the best of Dagger, Anvil, and kotlin-inject into one cohesive solution.

Key Features:

  • βœ… Compile-time validation – Catch dependency graph errors during compilation, not at runtime
  • 🧩 FIR/IR code generation – No KAPT or KSP required, just a Kotlin compiler plugin
  • 🎯 Kotlin-first API – Inspired by kotlin-inject with top-level function injection and optional dependencies
  • πŸ—‘οΈ Dagger-esque runtime – Lean generated code with familiar patterns
  • βš’οΈ Anvil-style aggregation – @ContributesTo, @ContributesBinding, and more
  • 🌐 Multiplatform – Supports JVM, JS, Wasm, and Native targets
  • πŸ’‘ Helpful diagnostics – Detailed error messages with actionable suggestions
  • πŸ”— Advanced interop – Migrate incrementally from Dagger, kotlin-inject, or Guice
  • ⚑️ Fast - At build time, at runtime

Quick Start

1. Apply the Gradle plugin:

plugins {
  id("dev.zacsweers.metro") version "<version>"
}

2. Define a dependency graph:

@DependencyGraph
interface AppGraph {
  val repository: UserRepository

  @Provides
  fun provideApi(): Api = ApiImpl()
}

@Inject
class UserRepository(private val api: Api)

3. Create and use the graph:

val graph = createGraph<AppGraph>()
val repository = graph.repository

Documentation

πŸ“š zacsweers.github.io/metro

Topic
InstallationSetup and configuration
Dependency GraphsDefine and create graphs
ProvidesProvider functions and properties
Injection TypesConstructor, assisted, and member injection
ScopesScoping and lifecycle management
Coroutines SupportSuspend bindings and deferred initialization
AggregationAnvil-style contributions across modules
InteropDagger, kotlin-inject, and Guice compatibility
PerformanceBuild and runtime performance
CompatibilitySupported Kotlin versions
FAQFrequently asked questions
API DocsGenerated KDocs

Supported Platforms

Metro supports JVM, Android, JS, Wasm, and Native targets. The compiler plugin works with all Kotlin Multiplatform project types.

See the multiplatform docs for full details.


License

Copyright (C) 2025 Zac Sweers

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

   https://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
compiler-plugin
dependency-injection
di
jakarta-inject
javax-inject
jsr-330
kotlin

Significant stargazers

Jake Wharton

68,756 followers Β· starred Sep 2025

Oleg Nenashev

807 followers Β· starred Aug 2025

Bartek Pacia

557 followers Β· starred Jan 2026

Manoel Aranda Neto

232 followers Β· starred Apr 2026