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?
Daniele Segato
03/29/2024, 3:42 PM
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
Daniele Segato
03/29/2024, 3:56 PM
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
Alexandru Caraus
03/29/2024, 4:08 PM
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