InsertKoinIO / koin

Koin - a pragmatic lightweight dependency injection framework for Kotlin & Kotlin Multiplatform
https://insert-koin.io
Apache License 2.0
9.09k stars 719 forks source link

Can't inject gradle api implementation dependency by Koin #1937

Open AlexSuvorov2k opened 3 months ago

AlexSuvorov2k commented 3 months ago

I have a multimodule application with analytics feature. Since i want to use not only firebase analytics, i need to split the module into api and implementation. Also i will disable some analytics providers by BuildConfig setup. My project dependencies will look something like this:

  1. app
  2. ...
  3. feature:analytics-api
  4. feature:analytics-firebase
  5. feature:analytics-mock

How to get target implementation in application class?

Initializing a Koin in the application class:

startKoin {
            androidContext(this@ClientApp)
            val moduleList = (appModules + activityModules + featureModule).toMutableList()
            modules(moduleList.toList())
        }

My api class:

interface ApplicationAnalytics {
    ....
    fun setUserIdentifier(context: Context, clientId: String)
    fun setUserProperty(context: Context, bundle: Bundle)
    fun pushEvent(context: Context, event: Event)
}

My impl class:

class FirebaseAnalyticsImpl : ApplicationAnalytics {

    ....

    override fun pushEvent(context: Context, event: Event) {
        pushFirebaseEvent(context, event)
    }

Module designation and potential problem area:

private val analyticsModule = module {
    /*if(BuildConfig.WITH_ANALYTICS) */
    single {
           How to get target implementation????? 
        ApplicationAnalytics
    }
}

private val mockAnalyticsModule = module {
    single {
        MockAnalytics()
    }
}

val featureModule = listOf(analyticsModule).ifEmpty {
    listOf(mockAnalyticsModule)
}

Gradle dependency scheme: app:

implementation(project(":core:data"))
implementation(project(":core:domain"))
implementation(project(":core:ui"))
implementation project(':feature:analytics-api')
implementation(project(":shared"))
......

feature-analytics:

implementation(project(":core:data"))
implementation(project(":shared"))

feature-analytics-firebase:

implementation(project(":feature:analytics-api"))
implementation(project(":shared"))

The idea for the implementation is taken from this diagram

arnaudgiuliani commented 2 months ago

seems to be a gradle visibility problem here?