Skip to content

1.6.0

Latest

Choose a tag to compare

@ZacSweers ZacSweers released this 16 Nov 15:46
· 77 commits to main since this release

New

[MEEP-3124] Derived bindings

@Provides and @Binds functions can now have type parameters. One generic provider supplies a binding for every type it fits, so you don't need a separate provider for each type.

@BindingContainer
object Serializers {
  // Provides KSerializer<List<User>>, KSerializer<List<List<String>>>, and so on.
  @Provides
  fun <T> list(element: KSerializer<T>): KSerializer<List<T>> = ListSerializer(element)

  // Reified type parameters work too. This provides a KSerializer for any serializable type.
  @Provides inline fun <reified T> forType(): KSerializer<T> = serializer()
}

When a graph needs a type that has no binding of its own, Metro finds a generic provider whose return type matches and infers its type arguments. Here, a request for KSerializer<List<User>> calls list(forType<User>()). A binding that you declare for the exact type always wins over a generic one.

This is experimental and off by default. Turn it on with metro { enableDerivedBindings.set(true) }.

See the derived bindings docs for more info. Please share feedback on the MEEP!

[MEEP-2929] Graph lifecycle callbacks

This release introduces experimental support for graph lifecycle callbacks. Graph lifecycle callbacks allow you to define functions that are called when a graph is initialized or closed. This can be useful for performing cleanup or other tasks when a graph is no longer needed.

A graph that implements CloseableGraph can annotate functions with @OnGraphInit and @OnGraphClose. Metro invokes them at the corresponding lifecycle events, injects their parameters, and generates close() implementations.

With suspend provider support, callbacks can suspend and implement SuspendCloseableGraph instead.

See graph lifecycle for more info. Please share feedback on the MEEP!

Experimental companion blocks support

This release adds experimental support for companion block @Provides functions and properties on Kotlin 2.5.0-Beta1 and later. See companion-block configuration.

Note this feature is experimental in both Metro and Kotlin itself.

Enhancements

  • [FIR] Report an error on generic classes annotated with @ContributesBinding annotations unless derived bindings are enabled. Note that @ContributesIntoSet and @ContributesIntoMap are always prohibited on generic classes.
  • [IR] Generate fewer classes for @Includes dependencies.
  • [IR] Generate fewer provider fields in graphs with dependency cycles. Previously, bindings used by a cycle-breaking binding were accidentally counted twice and always resulted in a shared provider field.
  • [Runtime] Map multibinding providers such as Provider<Map<K, V>> and Provider<Map<K, Lazy<V>>> now only copy their result map once per call.

Fixes

  • [FIR] Fix Metro's FIR extensions using unsynchronized lazy values in IDE mode, where the IDE analyzes code on several threads at once.
  • [FIR] Stop a single-threaded compilation from turning off thread-safe lazy values for other compilations running in the same Kotlin daemon.
  • [FIR] Fix inline @Provides declarations that are already @PublishedApi internal getting a warning to add @PublishedApi. The warning still applies when a containing class is internal without @PublishedApi.
  • [FIR] Fix the duplicate check for multibinds annotations, which read the assisted annotation's slot. A declaration with two multibinds annotations, such as Metro's and Dagger's @Multibinds, now fails with "Multiple multibinds annotations found" during FIR analysis. It previously failed later in IR.
  • [FIR/IR] Fix contributed @Inject classes skipping declared and inherited member injection when generateContributionProviders is enabled.
  • [FIR/IR] Fix dependency graphs with a user-declared companion object failing to compile or to call their generated creator, such as AppGraph.create(). User-declared factory(...) overloads in these companions now work. A user-declared no-arg factory() that clashes with the generated one now reports an error.
  • [IR] Fix anonymous objects with suspend members in injected constructor defaults causing compiler crashes or incorrect JVM bytecode.
  • [IR] Fix dynamic graphs missing internal contributions from friend compilations (such as main contributions used by tests).
  • [IR] Fix an Unknown file compiler crash when a contribution's @Origin references a compiled class.
  • [IR] Better reuse provider instances across repeated graph accessor and unscoped consumer requests.
    • Converted provider accessors and constructor parameters reuse their wrappers (i.e. different types of Provider types). Canonical providers used only to initialize one cached conversion can be inlined.
    • Repeated requests for multibinding providers reuse their providers. A Provider<Map<K, Provider<V>>> returns the same map of entry providers on each invocation, like Dagger.
    • Unscoped values still create a fresh instance on each provider invocation.
    • Note that this results in more fields to allow reuse, in exchange for less allocations at runtime. If you have any issues, please file an issue with details. you can force these to just be getters instead via temporary compiler option in the meantime via metro { compilerOptions.put("force-provider-getters", "true") }
  • [IR] Fix JVM compilation failures when nested @GraphExtension implementations would produce class file names longer than 255 bytes.
  • [IR] Fix @GraphPrivate @Binds declarations leaking into graph extensions. Child graphs now resolve their own bindings or report a missing binding when the parent's alias is private.
  • [IR] Fix a compiler crash when a graph extension reads a @Binds alias whose @GraphPrivate source is unscoped. Also fix a crash when that source is a @Binds alias of an unscoped binding that an ancestor graph builds too.
  • [IR] Fix eager reads of included dependency accessors when graph extensions request Provider or Lazy values.
  • [IR] Fix missing graph references in getter-only shards when accessing included dependencies.
  • [IR] Fix graphs creating new providers every time a Map<K, Provider<V>> is requested. The graph now stores one map of entry providers and returns it on every read.
  • [IR] Fix a ClassFormatError when a contributed @Binds extension property or function shares a name with a graph accessor.
  • [IR] Fix an internal compiler error replacing a missing-binding error when another binding in the graph has an error type.
  • [IR] Fix the invalid assisted injection error naming the injection site as null when the request has no declaration to name.
  • [IR] Fix a compiler crash for map multibindings with two or more entries whose values are maps, such as Map<String, Map<String, Int>>, when the map is requested through a Provider.
  • [IR] Fix suspend @Provides functions requested as a SuspendProvider retaining the whole graph. Their generated provider lambdas now capture only what the provider's factory would hold. Provider lambdas that wrap an @Includes accessor also stop retaining the graph and capture only the included instance.
  • [IR/IC] Fix consumer graphs retaining stale scopes on Kotlin <2.4.0 after an injected class in another module gains or loses a scope annotation.
  • [IR/interop] Fix an issue with Dagger-processed providers/constructors with default parameters with their generated factory. Previously, Metro would treat the source function's parameter as optional if it had a default value and not reconcile that correctly with Dagger's generated factory (which would not propagate that default since Dagger uses a different mechanism for optional bindings).
  • [Runtime] Fix Provider<Set<T>> throwing IllegalStateException: Codegen error? Duplicates in the provider list when two contributions have equal providers, such as two @Binds @IntoSet aliases of bound instances with equal values.
  • [Runtime] Fix scoped providers that recursively invoke themselves accepting two equal but distinct instances. DoubleCheck compares the instances by identity, like Dagger, and throws if they differ.

Changes

  • [compat] Support Kotlin 2.5.20-dev-4223.
  • Test Kotlin 2.5.0-RC.
  • Test IntelliJ IDEA 2026.2.4 and 2026.3 stable.
  • Test Android Studio Rabbit 2 stable and Patch 1.
  • Test Android Studio Rabbit 3 canaries (Canary 3).

Contributors

Special thanks to the following contributors for contributing to this release!

Consider sponsoring Metro's development

What's Changed

New Contributors

Full Changelog: 1.5.2...1.6.0