InsertKoinIO / koin-annotations

Koin Annotations - About Koin - a pragmatic lightweight dependency injection framework for Kotlin & Kotlin Multiplatform insert-koin.io
https://insert-koin.io
Apache License 2.0
123 stars 30 forks source link

Injecting Context/Application in multi module app doesn't work #94

Open sudansh opened 8 months ago

sudansh commented 8 months ago

Describe the bug Injecting Context/Application in single module app works but doesn't work in multi module app

To Reproduce Steps to reproduce the behavior:

  1. Create multi module app

This works in app module @Single fun prefsApp(context: Context) = Preferences(context)

@Single fun prefsFeature(context: Context) = Preferences(context)

Expected behavior prefsFeature should be able to inject context

Koin project used and used version (please complete the following information): koin-annotations-bom version 1.3.0

arnaudgiuliani commented 8 months ago

why this doesn't work. Can you provide mode details?

sudansh commented 8 months ago

Created a sample project with bug https://github.com/sudansh/koinBug/

Error while building e: [ksp] --> Missing Definition type 'android.content.Context' for 'app.sudansh.domain.PreferencesHelper'. Fix your configuration to define type 'Context'. e: Error occurred in KSP, check log for detail

sudansh commented 8 months ago

I saw this issue https://github.com/InsertKoinIO/koin-annotations/issues/49 and I believe this is not possible yet now to pass context with annotations.

arnaudgiuliani commented 8 months ago

it's possible with androidContext(...) line in your startKoin section. But verification seems to look for it

sudansh commented 8 months ago

Have already put androidContext() in startKoin but context isn't found in multi module app. It works if PreferenceHelper is in same module as startKoin

arnaudgiuliani commented 8 months ago

I don't see why your other module couldn't find your android Context if it's been injected since your startKoin section?! 🤔 Can you gives more details about how your are launching your other modules?

Junzz0 commented 7 months ago

koin-core: 3.5.0 koin-annotations: 1.3.0

https://insert-koin.io/docs/reference/koin-annotations/start/#compile-safety---check-your-koin-config-at-compile-time-since-130

If I turn on Compile Safety, the compilation will get this error, not set, the compilation is normal.

e: [ksp] --> Missing Definition type

Junzz0 commented 7 months ago

https://github.com/InsertKoinIO/hello-kmp/blob/annotations/shared/build.gradle.kts#L67

The recommended configuration was not used because an error would be reported.

dependencies {
    "kspCommonMainMetadata"(libs.findLibrary("koin-compiler").get())
    "kspAndroid"(libs.findLibrary("koin-compiler").get())
    "kspDesktop"(libs.findLibrary("koin-compiler").get())
}
arnaudgiuliani commented 7 months ago

yes, definitely need to update this project with the new compiler option. Also linked to this PR: https://github.com/InsertKoinIO/koin-annotations/pull/101

Adnan9011 commented 5 months ago

same issue when using context for create a database in the data module, context doesn't work :(

arnaudgiuliani commented 5 months ago

same issue when using context for create a database in the data module, context doesn't work :(

is it a KMP app? are you injecting on Android part?

Adnan9011 commented 5 months ago

same issue when using context for create a database in the data module, context doesn't work :(

is it a KMP app? are you injecting on Android part?

I have been using the Koin annotation in my Android project, but unfortunately, it doesn't work in multi-module setups. I then switched to using Koin without annotations.

I like the annotation feature, it just didn't work for me in this case (multi module). In my open-source project with a multi-module app, I have successfully used Koin (without annotations), which has worked well for me.

arnaudgiuliani commented 4 months ago

can you check with the latest 1.3.1? Some of you here were having config validation problem

Adnan9011 commented 4 months ago

Absolutely, I will keep you informed.