Skip to content

@ContributesBinding in multi-module KMP project #118

@itsmattking

Description

@itsmattking

Hello,

I'm working on adding kotlin-inject-anvil to a new KMP project.

Similar to this issue, I'm trying to understand how I can have an interface in one module (say :domain) and an implementation in another (say :data). A parent module (say :parent) contains :domain and :data.

I want it so that if consumers include :parent that they automatically have injectable implementations of the items in it's :domain without seeing the implementations in :data.

:domain

interface MyDataSource {
  fun getThing(): String
}

:data (includes :domain dependency)

@ContributesBinding(AppScope::class)
@SingleIn(AppScope::class)
class MyDataSourceImpl @Inject constructor() : MyDataSource {
  override fun getThing(): String = "a string"
}

However when I try to create a component in :parent:

@MergeComponent(AppScope::class)
@SingleIn(AppScope::class)
abstract class CoreComponent {
    abstract val myDataSource: MyDataSource
}

it says that Cannot find an @Inject constructor or provider for MyDataSource, though I've defined it on MyDataSourceImpl via @ContributesBinding. I tried using @Provides the way typically done using plan kotlin-inject, but it's no difference.

Oddly enough though, this seems to only affect iosArm64 and iosSimulatorArm64 - I'm able to get it to work on JVM/Android with a bit of fiddling around.

So I guess my questions are:

  1. Am I just fundamentally misunderstanding how @ContributesBinding works or are there limitations around having it in different modules?
  2. Is my understanding of how to use kotlin-inject and kotlin-inject-anvil in KMP incorrect?

I've followed the documentation both here and here as well as looked at the samples provided in both this repo and kotlin-anvil.

Metadata

Metadata

Assignees

No one assigned

    Labels

    questionFurther information is requested

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions