Repository navigation
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
@ContributesBindingannotations unless derived bindings are enabled. Note that@ContributesIntoSetand@ContributesIntoMapare always prohibited on generic classes. - [IR] Generate fewer classes for
@Includesdependencies. - [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>>andProvider<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@Providesdeclarations that are already@PublishedApi internalgetting a warning to add@PublishedApi. The warning still applies when a containing class isinternalwithout@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
@Injectclasses skipping declared and inherited member injection whengenerateContributionProvidersis enabled. - [FIR/IR] Fix dependency graphs with a user-declared
companion objectfailing to compile or to call their generated creator, such asAppGraph.create(). User-declaredfactory(...)overloads in these companions now work. A user-declared no-argfactory()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
internalcontributions from friend compilations (such asmaincontributions used by tests). - [IR] Fix an
Unknown filecompiler crash when a contribution's@Originreferences 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
Providertypes). 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") }
- Converted provider accessors and constructor parameters reuse their wrappers (i.e. different types of
- [IR] Fix JVM compilation failures when nested
@GraphExtensionimplementations would produce class file names longer than 255 bytes. - [IR] Fix
@GraphPrivate @Bindsdeclarations 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
@Bindsalias whose@GraphPrivatesource is unscoped. Also fix a crash when that source is a@Bindsalias of an unscoped binding that an ancestor graph builds too. - [IR] Fix eager reads of included dependency accessors when graph extensions request
ProviderorLazyvalues. - [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
ClassFormatErrorwhen a contributed@Bindsextension 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
nullwhen 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 aProvider. - [IR] Fix suspend
@Providesfunctions requested as aSuspendProviderretaining the whole graph. Their generated provider lambdas now capture only what the provider's factory would hold. Provider lambdas that wrap an@Includesaccessor also stop retaining the graph and capture only the included instance. - [IR/IC] Fix consumer graphs retaining stale scopes on Kotlin <
2.4.0after 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>>throwingIllegalStateException: Codegen error? Duplicates in the provider listwhen two contributions have equal providers, such as two@Binds @IntoSetaliases of bound instances with equal values. - [Runtime] Fix scoped providers that recursively invoke themselves accepting two equal but distinct instances.
DoubleCheckcompares 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.4and2026.3stable. - 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
- Update Metro bootstrap to v1.5.2 by @renovate[bot] in #3050
- Update dependency com.android.tools:r8 to v9.5.29 by @renovate[bot] in #3049
- Clean up old tested dev versions by @ZacSweers in #3052
- Update tested IDE versions by @ZacSweers in #3053
- Update IDE version mappings by @ZacSweers in #3055
- Update dependency com.github.ajalt.mordant:mordant-core to v3.1.1 by @renovate[bot] in #3054
- Update IDE version mappings by @ZacSweers in #3064
- Reconcile default params in dagger interop by @aglose in #3059
- Optimize CI and cache reuse by @ZacSweers in #3058
- Update dependency io.insert-koin.compiler.plugin to v1.2.3 by @renovate[bot] in #3057
- Update dependency zensical to v0.1.0 by @renovate[bot] in #3065
- Update dependency Markdown to v3.11.3 by @renovate[bot] in #3071
- Update dependency androidx.test.uiautomator:uiautomator to v2.4.1 by @renovate[bot] in #3060
- Fix dependabot issues by @ZacSweers in #3056
- Update plugin dokka to v2.3.0 by @renovate[bot] in #3072
- Fix missing member injection with generateContributionProviders by @ZacSweers in #3070
- Fix graphs creating new map provider instances each call by @falqadi in #3068
- Fix eager reads of included dependency accessors by @falqadi in #3069
- Fix
@GraphPrivate @Bindsdeclarations leaking into graph extensions by @ZacSweers in #3073 - feat: add github workflow to cleanup old versiond docs sites on-deman by @hossain-khan in #3074
- Update plugin intellijPlatform to v2.20.0 by @renovate[bot] in #3061
- Update IDE version mappings by @ZacSweers in #3075
- Reduce provider allocations by @falqadi in #3067
- More provider field/reuse cleanups by @ZacSweers in #3076
- Update dependency click to v8.5.1 by @renovate[bot] in #3078
- Update Gradle to v9.9.0 by @renovate[bot] in #3077
- Update IDE version mappings by @ZacSweers in #3080
- Update dependency mkdocs-material to v9.8.1 by @renovate[bot] in #3083
- Fix calling declaration for dynamic graphs by @ZacSweers in #3084
- Set the Kotlin compiler version for JS box tests by @ZacSweers in #3086
- Factor in receivers in deduping property accessors by @ZacSweers in #3085
- Update tested IDE versions by @ZacSweers in #3087
- Fix graph creators in user-declared companion objects by @ZacSweers in #3088
- Fix the JS box skiko stub on Kotlin 2.5 test frameworks by @ZacSweers in #3090
- Update dependency com.autonomousapps:gradle-testkit-support to v0.31 by @renovate[bot] in #3091
- Update dependency pymdown-extensions to v13 by @renovate[bot] in #3089
- Update dependency com.autonomousapps:graph-support to v0.14.0 by @renovate[bot] in #3092
- Add companion-mode compiler infrastructure by @ZacSweers in #2973
- Generate hidden companion-block helpers in IR by @ZacSweers in #2974
- Generate graph creators in companion blocks by @ZacSweers in #2975
- Recognize companion-block providers in IDEA by @ZacSweers in #2913
- Update IDE version mappings by @ZacSweers in #3093
- Update dependency mkdocs-git-revision-date-localized-plugin to v1.7.0 by @renovate[bot] in #3094
- Add repro for too-long name lengths for graph extensions by @falqadi in #3095
- Update dependency tornado to v6.6.1 by @renovate[bot] in #3096
- Update plugin intellijPlatform to v2.20.1 by @renovate[bot] in #3097
- Update IDE version mappings by @ZacSweers in #3099
- Update dependency zensical to v0.1.1 by @renovate[bot] in #3100
- Update Gradle to v9.9.1 by @renovate[bot] in #3101
- Update wire to v7.2.0 by @renovate[bot] in #3102
- Graph lifecycle API by @ZacSweers in #3011
- Update IDE version mappings by @ZacSweers in #3103
- Compare instances by identity in DoubleCheck's reentrant check by @ZacSweers in #3107
- Stop generating an unused UUID tag for each allocated name by @ZacSweers in #3110
- Tidy up map multibinding symbol selection and graph node override loop by @ZacSweers in #3108
- Update dependency pymdown-extensions to v13.1 by @renovate[bot] in #3114
- Fix a compiler crash for map multibindings whose values are maps by @ZacSweers in #3109
- Stop double-counting dependency references of deferred types by @ZacSweers in #3112
- Tidy up runtime map factories by @ZacSweers in #3104
- Fix two diagnostic edge cases in IrBindingGraph by @ZacSweers in #3113
- Keep memoize thread-safe in IDE mode by @ZacSweers in #3105
- Remove the duplicate provider check from SetFactory by @ZacSweers in #3106
- Companion block edge cases by @ZacSweers in #3115
- Batch graph lifecycle warnings that share a source element by @ZacSweers in #3111
- Test 2.5.0-RC by @ZacSweers in #3117
- Bump 2.5.20 dev support by @ZacSweers in #3118
- Update IDE version mappings by @ZacSweers in #3120
- Update wire to v7.2.1 by @renovate[bot] in #3121
- Update dependency com.android.tools:r8 to v9.5.32 by @renovate[bot] in #3122
- Handle absent file entries by @ZacSweers in #3123
- Fix the FIR duplicate check for multibinds annotations by @ZacSweers in #3126
- Stop the inline provider warning for
@PublishedApi internalproviders by @ZacSweers in #3127 - Fix suspend providers retaining the graph through their provider lambda by @ZacSweers in #3128
- Derived bindings by @ZacSweers in #3125
- Update dependency org.robolectric:robolectric to v4.18 by @renovate[bot] in #3129
- Fix an IC lookup on older kotlin versions by @ZacSweers in #3131
- Update ksp monorepo to v2.3.14 by @renovate[bot] in #3130
- Fix anonymous suspend parameter defaults by @ZacSweers in #3132
- Fix generic class binding contribution with derived bindings by @kevinguitar in #3133
- Update IDE version mappings by @ZacSweers in #3134
New Contributors
Full Changelog: 1.5.2...1.6.0