koin icon indicating copy to clipboard operation
koin copied to clipboard

No value found error when confirmed created before injection

Open BigBallard opened this issue 4 months ago • 0 comments

Describe the bug I am working on a multiplatform project primarily focusing on Android at the moment. In commonMain, I have an interface IHttpClientFactory that has an implementation defined in androidMain. A function getHttpClientFactory is marked as expected with an actual definition in androidMain.

In my module, I define a singleton for IHttpClientFactory, calling the getHttpClientFactory fun. Another singleton for an IInventoryManager (impl HttpInventoryManager which takes an IHttpClientFactory as a paraneter).

When the HttpInventoryManager is created by koin, and exception occurs stating that there is no value for IHttpClientFactory.

To Reproduce Steps to reproduce the behavior:

  1. Have or create an KMP project with Android as a target
  2. Create an interface in commonMain with an expect function to get an instance of interface
  3. Implement interface in androidMain and create actual of function to get instance
  4. Define module in commonMain and addsingle for interface, calling the expect function to provide instance
  5. Define class in commonMain with constructor param of above interface, add to module as single
  6. Use koinInject on step 5 interface somewhere in Android app
  7. Run application

Expected behavior The implementation instance that is created for koin should be provided as a value for IHttpClientFactory.

Koin module and version:

  • koin-compose: 4.1.0

Snippet or Sample project to help reproduce

COMMON MAIN Inventory Module

val inventoryModule = module {
    single<IHttpClientFactory>(createdAtStart = true) {
        getHttpClientFactory()
    }
    single<IInventoryManager> {params-> HttpInventoryManager(params.get()) }
}

Http Client Factory

interface IHttpClientFactory {
    fun createClient(): HttpClient
}

expect fun getHttpClientFactory(): IHttpClientFactory

Injection

class HttpInventoryManager(private val httpClientFactory: IHttpClientFactory): IInventoryManager

ANDROID MAIN Http Client Factory

class OkHttpClientFactory(): IHttpClientFactory {
    override fun createClient(): HttpClient {
        return HttpClient(OkHttp)
    }
}

actual fun getHttpClientFactory(): IHttpClientFactory = OkHttpClientFactory()

Root of stack:

org.koin.core.error.InstanceCreationException: Could not create instance for '[Factory: 'com.bigballard.tool.inventory.InventoryItemViewModel']'
...
Caused by: org.koin.core.error.InstanceCreationException: Could not create instance for '[Singleton: 'com.bigballard.tool.inventory.IInventoryManager']'
...
Caused by: org.koin.core.error.DefinitionParameterException: No value found for type 'com.bigballard.tool.http.IHttpClientFactory'
...

BigBallard avatar Aug 15 '25 14:08 BigBallard