koin-annotations icon indicating copy to clipboard operation
koin-annotations copied to clipboard

Type is not declared in Koin module for view models

Open rlnt opened this issue 3 months ago • 0 comments

Describe the bug I am not sure if this is the right place since this problem originates from the IntelliJ/Android Studio Koin IDE plugin. The plugin says it's official, but the link to the issue tracker is down, so I am not sure where to report.

I currently have three main directories in my Compose Multiplatform project: data, network, and ui.

For each of these directories, I have a separate Koin module.

fun initKoin(config: KoinAppDeclaration? = null) {
    startKoin {
        modules(
            CommonDiModule().module
        )
        config?.invoke(this)
    }
}

// combined module for all submodules in common
@Module(includes = [DataDiModule::class, UiDiModule::class, NetworkModule::class])
class CommonDiModule

@Module
@ComponentScan("com.test.data")
class DataDiModule

@Module
@ComponentScan("com.test.ui")
class UiDiModule

@Module(createdAtStart = true)
class NetworkModule {
    @Single
    fun provideHttpClientEngine(): HttpClientEngine = Factories().createHttpClientEngine()
}

The dependency injection functions correctly, and my app runs fine, but the IDE plugin complains that my view models have no declared type in Koin modules.

val viewModel = koinViewModel<ProjectDetailsViewModel> { parametersOf(projectId) }
Image

All my view models are in the ui directory and are annotated with @KoinViewModel. It seems like the plugin doesn't detect this annotation correctly.

@KoinViewModel
class ProjectDetailsViewModel(
    @InjectedParam private val projectId: String,
    private val projectApiRepository: ProjectApiRepository
): ViewModel() {

When looking at the Koin Configuration tab in the IDE, it correctly detects my data and my network module. But the ui module isn't listed there, although the configurations for it are being generated, and the app works. Image

I assume the view model annotation is not detected, and the plugin shows a false warning.

To Reproduce Steps to reproduce the behavior:

  1. Specify a module with component scan
  2. Only place annotated view models in the component scan directory
  3. Make use of the view models somewhere with koinViewModel()
  4. See the warning displayed by the IDE plugin

Expected behavior Since the dependency injection works correctly, I expect it not to show a warning for these view models.

Koin project used and used version:

  • Kotlin: 2.2.0
  • KSP: 2.2.0-2.0.2
  • Koin Core: 4.1.0
  • Koin KSP Compiler: 2.1.0
  • Koin Annotations: 2.1.0
  • Koin Viewmodel: 4.1.0

rlnt avatar Sep 16 '25 07:09 rlnt