https://kotlinlang.org logo
#koin
Title
# koin
d

Daniele Segato

03/29/2024, 3:37 PM
Learning Koin, how do I inject parameters in modules with koin-annotation? take this very basic example…
Copy code
@Module()
@ComponentScan("com.example.api")
class ApiModule

@Factory
class Api(
    @InjectedParam
    private val client: HttpClient,
)

// Application onCreate

startKoin {
  modules(
    ApiModule().module, // how do I inject the httpClient?
  )
}
how do I inject the http client in there?
the generated by ksp code
Copy code
public val com_example_api_ApiModule : Module = module {
	factory() { params -> com.example.api.Api(params.get()) } 
}
public val com.example.api.ApiModule.module : org.koin.core.module.Module get() = com_example_api_ApiModule
If it makes more sense…. Pretend this is the structure
Copy code
// HttpClientModule
@Factory
class HttpClientFactory(@InjectedParam config: HttpClientConfig)

@Module
class HttpClientModule {

  @Single
  fun httpClient(factory: HttpClientFactory): HttpClient = factory.create()
}

// ApiModule
@Factory
class ApiImpl(@InjectedParam httpClient: HttpClient)

// RepositoryModule
@Single
class RepositoryImpl(@InjectedParam api: Api)

class MyActivity: AppCompatActivity() {
  val repository by inject<Repository>()
}
basically in this whole mess the only thing I would need to inject from outside is “HttpClientConfig” but how do I inject it? possibly only once
a

Alexandru Caraus

03/29/2024, 4:06 PM
Remove the injected param annotation
Take first example, add a function annotated with @Single which will create http client
d

Daniele Segato

03/29/2024, 5:09 PM
Thanks If i Do that I get an error from the ksp compiler (I’ve enabled
KOIN_CONFIG_CHECK
). If I disable that check it works - I think this is a bug
8 Views