first time using kotlin-inject, and it’s pretty co...
# kotlin-inject
p
first time using kotlin-inject, and it’s pretty cool! I had to learn one the hard way…. could maybe be useful feedback for docs or FRs:
@Provides
methods need to be wrapped in a
@Component
, else they don’t get picked up. e.g.
Copy code
//in file network.kt
@Provides
fun provideOkhttpClient(): OkHttpClient {
    return OkHttpClient.Builder().build()
}
this won’t work, it needs to be
Copy code
@ContributesTo(AppScope::class) //k-i-anvil annotation
interface NetworkComponent {
  @Provides fun okhttp(): OkHttpClient { ... }
}
FR: could be cool to allow
@Provides
on toplevel functions without a Component wrapper. Or have something in the docs for Dagger converts that says “use
@Component
like Dagger uses `@Module`”
e
I've been collecting doc improvement ideas here if you want to reference/add https://github.com/evant/kotlin-inject/issues/389
p
oh nice, will do
e
as for 'top-level' provides it needs to be findable by the component. there's not a mechanism in base kotlin-inject to make this possible but I'd imagine
@ContributesTo
in anvil might be able to be extended to make it work
p
yeah good point! what I’m currently writing in my issue comment, but i’ll put here as well – i sorta assumed that because
@Inject
could be applied to top-level functions, so could
@Provides
e
it does this by linking it to a type that's referenced in your component. which feels a little magical at the moment to be fair (https://github.com/evant/kotlin-inject/issues/252#issuecomment-2198786073 has some thought on that) The big no-no would be requiring the entire classpath to be scanned which a bare
@Provides
would require.