Troubleshooting
This page covers common issues you might encounter when using Anchor DI and how to fix them. If you run into something not listed here, consider opening an issue on GitHub.
"Scoped binding for X requires a scope"
What it means: A type is bound to a scope (e.g. ViewModelComponent) but was requested from the root container (no scope active). Anchor DI can't resolve scoped types from the root — they must be resolved inside the correct scope.
How to fix:
-
ViewModel-scoped types
Create the ViewModel withviewModelAnchor(), notviewModel { Anchor.inject<MyViewModel>() }. OnlyviewModelAnchor()runs resolution insideAnchor.withScope(ViewModelComponent::class). -
Navigation-scoped types
Resolve only insideNavigationScopedContent(navBackStackEntry)withnavigationScopedInject<T>(). Don't callAnchor.inject<T>()from the root for navigation-scoped types. -
Custom-scoped types
Resolve only insideAnchor.withScope(YourScope::class) { scope -> scope.get<X>() }or from a container returned byAnchor.scopedContainer(YourScope::class). -
Alternative
If the type doesn't need to be scoped, install its module inSingletonComponentinstead ofViewModelComponent(or the other scoped component).
"No binding found for X"
What it means: There is no binding for type X — no @Inject constructor, no @Provides, and no @Binds that provides X. The container doesn't know how to create or provide it.
How to fix:
-
Add a binding
- Use
@Injecton the constructor if you control the class. - Or add a
@Providesmethod in a module. - Or add a
@Bindsmethod to map an interface to an implementation.
- Use
-
Ensure the module is installed
The module must be@InstallIn(SingletonComponent::class)(or the appropriate component) and be part of the generated contributor. If you use multi-module, ensure the module is included ingetAnchorContributors(). -
Rebuild
KSP runs at compile time. Changes to annotations, modules, or dependencies require a full rebuild (e.g../gradlew clean buildor your IDE's rebuild).
getAnchorContributors() not found
What it means: KSP hasn't generated the contributor yet, or the generated code isn't visible to the source set you're compiling.
How to fix:
-
Rebuild
Run a full build so KSP can process your code and generategetAnchorContributors(). -
iOS: Use correct source sets
KSP generates code into target-specific directories thatiosMaincannot see. You need anactualforgetAnchorContributors()in bothiosArm64MainandiosSimulatorArm64Main(notiosMain). See Platform-Specific Setup. -
Check KSP configuration
Ensure you've addedanchor-di-kspfor each Kotlin target you use (kspCommonMainMetadata,kspAndroid,kspIosArm64, etc.).
Duplicate binding
What it means: Two modules (or a module and an @Inject class) provide the same type with the same qualifier. The container doesn't know which one to use.
How to fix:
- Add qualifiers to distinguish them:
@Named("id")or a custom qualifier (e.g.@ApiUrl,@WebUrl). - Or remove one of the bindings if it's redundant.
Circular dependency
What it means: A depends on B, B depends on C, C depends on A. The container can't resolve the cycle.
How to fix:
- Restructure — Break the cycle by introducing an interface, a new abstraction, or moving logic to a different layer.
- Use Lazy — Sometimes injecting
Lazy<T>can break a cycle (e.g. A depends onLazy<B>, B depends on A). Use sparingly; prefer restructuring.
iOS: Scope resolution fails on device or simulator
What it means: On Kotlin/Native (iOS), scope IDs might not match if the scope type isn't top-level or if there are reflection limitations.
How to fix:
- Ensure
getAnchorContributors()is in bothiosArm64MainandiosSimulatorArm64Main, notiosMain. - Use top-level
objectorclassfor scope markers (e.g.object ActivityScope). - Avoid nested scope types — they can have unstable qualified names on Kotlin/Native.
Gradle / Build issues
KSP version: Use KSP 2.3+ compatible with your Kotlin version. Check the KSP compatibility table.
Multi-module: Each module that has @Inject classes or @Module classes needs anchor-di-ksp. Use anchorDiModuleId (e.g. ksp { arg("anchorDiModuleId", "feature") }) so each module generates a unique contributor. Aggregate contributors in your app module's getAnchorContributors().
Still stuck?
- Check the documentation for detailed guides.
- Search existing issues on GitHub.
- Open a new issue with a minimal repro, error message, and environment (Kotlin version, KSP version, platform).