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

Invalid work of KoinViewModel annotation with KMM

Open Pschsch opened this issue 1 year ago • 4 comments

Describe the bug Koin Annotations use org.koin.androidx.viewmodel package for all platforms to generate viewModel definition

To Reproduce Steps to reproduce the behavior:

  1. Declare ViewModel class in commonMain source set
  2. Annotate it with KoinViewModel
  3. Launch compilation for target other, than Android
  4. See compilation error

Expected behavior It's a little difficult to say, what declaration should be in jvm, ios and other platforms. Maybe single? UPD: Exactly, factory

Koin project used and used version (please complete the following information): [e.g]: koin-annotations version 1.2.2

Additional moduleDefinition

import org.koin.core.module.Module
import org.koin.dsl.*
import org.koin.androidx.viewmodel.dsl.viewModel /*error*/
/*Pure JVM platform declaration*/
public val ru_example_mortgage_MortgageModule : Module = module {
	single() { ru.example.mortgage.businesslogic.MortgageServiceImpl() } bind(ru.example.mortgage.businesslogic.MortgageService::class)
	viewModel() { ru.example.mortgage.MortgageViewModel(get(),get(),get()) } 
}
public val ru.example.mortgage.MortgageModule.module : org.koin.core.module.Module get() = ru_example_mortgage_MortgageModule

Pschsch avatar Jun 28 '23 06:06 Pschsch

The @KoinViewModel annotation target the Android platform for now. This is why the annotation is in package org.koin.android.annotation. But then I agree, nothing prevents from using it in KMP project in common part for example.

arnaudgiuliani avatar Aug 04 '23 09:08 arnaudgiuliani

any update?

taetae98coding avatar Apr 17 '24 03:04 taetae98coding

I guess https://github.com/InsertKoinIO/koin-annotations/pull/138 should have fixed this but it does not help. It still imports org.koin.androidx.viewmodel.dsl.viewModel vs org.koin.compose.viewmodel.dsl.viewModel

KristapsKrumins avatar Jul 22 '24 18:07 KristapsKrumins

我猜#138应该已经修复了这个问题,但它没有帮助。它仍然导入 org.koin.androidx.viewmodel.dsl.viewModel 而不是 org.koin.compose.viewmodel.dsl.viewModel

Yes, No matter where I add ksp { arg("KOIN_USE_COMPOSE_VIEWMODEL", "true") }, it doesn't work!

master-lzh avatar Jul 27 '24 15:07 master-lzh