koin icon indicating copy to clipboard operation
koin copied to clipboard

Koin ViewModel annotation is generating a deprecated file after Koin 4.0

Open felipezad opened this issue 1 year ago • 3 comments

Describe the bug

The generated module with their respective DSL are being generated with a deprecated code.

IRL -> We work with projects that treats warning as errors in projects

To Reproduce Steps to reproduce the behavior:

  1. setup a project with the latest koin, koin annotations and ksp compiler.
  2. Create a viewmodel with Koin Annotations @KoinViewModel
  3. Inject into MainActivity
  4. Check the Module generated

Expected behavior The generated module would be generated with the right dsl import org.koin.core.module.dsl.viewModel instead of import org.koin.androidx.viewmodel.dsl.viewModel

image

Koin module and version:

koin = "4.0.0" koinKsp = "1.3.1" kspPlugin = "2.0.20-1.0.24"

koinAndroid = { group = "io.insert-koin", name = "koin-android", version.ref = "koin" } koinAndroidAnnotations = { group = "io.insert-koin", name = "koin-annotations", version.ref = "koinKsp" } koinKsp = { group = "io.insert-koin", name = "koin-ksp-compiler", version.ref = "koinKsp" }

Snippet or Sample project to help reproduce

Add a snippet or even a small sample project to hel reproduce your case.

Commit -> https://github.com/felipezad/SpotlessApplication/commit/5dff6f5cedc86e15c6a5c5f2f00b201e8bc99744

felipezad avatar Sep 19 '24 08:09 felipezad

Agree, I have the same issue

GazimSoliev avatar Sep 24 '24 23:09 GazimSoliev

Same issue here

marcelpallares avatar Sep 27 '24 16:09 marcelpallares

Same issue

Kotlin Jetpack Compose

using migration guide didn't fix the problem https://insert-koin.io/docs/setup/migrate

ksp = "2.0.20-1.0.24" koin-bom = "4.0.0" koin-ksp-annotations = "1.4.0-RC4"

koin-bom = { module = "io.insert-koin:koin-bom", version.ref = "koin-bom" } or koin-android = { group = "io.insert-koin", name = "koin-android", version.ref = "koin-bom" } gives same deprecated warning

koin-core-viewmodel = { group = "io.insert-koin", name = "koin-core-viewmodel", version.ref = "koin-bom" } koin-core-viewmodel-navigation = { group = "io.insert-koin", name = "koin-core-viewmodel-navigation", version.ref = "koin-bom" }

koin-annotations = { group = "io.insert-koin", name = "koin-annotations", version.ref = "koin-ksp-annotations" } koin-ksp = { group = "io.insert-koin", name = "koin-ksp-compiler", version.ref = "koin-ksp-annotations" }

image

aezakmi8 avatar Oct 03 '24 12:10 aezakmi8

Temporal Fix

package org.koin.androidx.viewmodel.dsl

import androidx.lifecycle.ViewModel
import org.koin.core.definition.Definition
import org.koin.core.definition.KoinDefinition
import org.koin.core.module.Module
import org.koin.core.module.dsl.viewModel as fixedViewModel
import org.koin.core.qualifier.Qualifier

inline fun <reified T : ViewModel> Module.viewModel(qualifier: Qualifier? = null, noinline definition: Definition<T>): KoinDefinition<T> =
    fixedViewModel(qualifier = qualifier, definition =  definition)

GazimSoliev avatar Nov 02 '24 12:11 GazimSoliev

can you raise the issue in https://github.com/InsertKoinIO/koin-annotations ?

arnaudgiuliani avatar Nov 15 '24 12:11 arnaudgiuliani

any trial on Koin Annotations 2.0-Beta1 ? This release contains the fix for that.

arnaudgiuliani avatar Nov 15 '24 12:11 arnaudgiuliani

It works, but only if I add ksp { arg("KOIN_USE_COMPOSE_VIEWMODEL", "true") } to each module's build.gradle.kts I have ViewModels in. I was unable to configure it as a general option using buildSrc.

Side note: It's not a KMM project, just an Android multi-module project structure.

So maybe there's still some room for improvement, I guess.

cmaier avatar Nov 17 '24 15:11 cmaier

Noted. Will report in on Koin Annotations project.

arnaudgiuliani avatar Jan 20 '25 18:01 arnaudgiuliani

I close it. Let's follow on koin annotations repo.

arnaudgiuliani avatar Jan 21 '25 14:01 arnaudgiuliani

didn't see a clear instruction outlined in here (and the IDE warning didn't indicate properly) but in case someone else is migrating and the instructions don't reveal themselves until you arrive here

remove imports

import org.koin.androidx.viewmodel.dsl.viewModel
import org.koin.androidx.viewmodel.dsl.viewModelOf

add imports

import org.koin.core.module.dsl.viewModel
import org.koin.core.module.dsl.viewModelOf

kassim avatar Jul 09 '25 23:07 kassim