{"id":1448,"date":"2025-01-23T11:13:51","date_gmt":"2025-01-23T10:13:51","guid":{"rendered":"https:\/\/www.thomaskeller.biz\/blog\/?p=1448"},"modified":"2025-01-23T11:13:51","modified_gmt":"2025-01-23T10:13:51","slug":"bridging-koin-and-dagger2","status":"publish","type":"post","link":"https:\/\/www.thomaskeller.biz\/blog\/2025\/01\/23\/bridging-koin-and-dagger2\/","title":{"rendered":"Bridging Koin and Dagger2"},"content":{"rendered":"\n<p>In my current project we are slowly migrating from <a href=\"https:\/\/dagger.dev\/\">Dagger2<\/a> (non-Hilt) into the <a href=\"https:\/\/insert-koin.io\/\" data-type=\"link\" data-id=\"https:\/\/insert-koin.io\/\">Koin world<\/a>, module by module, and figured we need to bridge a couple of new things into the old world. But how exactly would one do that?<\/p>\n\n\n\n<p>My solution was to let Dagger create the Koin graph as part of it&#8217;s own graph, like so:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import org.koin.core.module.Module as KoinModule\n\n@Module\nclass KoinCompatModule {\n    @Provides\n    @Singleton\n    @Suppress(\"SpreadOperator\")\n    fun provideKoinApplication(koinModules: Set&lt;KoinModule>): KoinApplication =\n        startKoin {\n            modules(*koinModules.toTypedArray())\n        }\n}<\/code><\/pre>\n\n\n\n<p>Now, each new Gradle module just has to provide a compatible Dagger2 module alongside the Koin module for injection:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import org.koin.core.module.Module as KoinModule\n\nfun coreDomainModule(): KoinModule =\n    module {\n        single&lt;ClockProvider> { ClockProviderImpl() }\n        single&lt;DispatcherProvider> { DispatcherProviderImpl() }\n    }\n\n@Module\nclass CoreDomainModule {\n    @Provides\n    @IntoSet\n    internal fun provideCoreDomainModule(): KoinModule = coreDomainModule()\n\n    @Provides\n    internal fun provideClockProvider(app: KoinApplication): ClockProvider =\n        app.koin.get()\n\n    @Provides\n    internal fun provideDispatcherProvider(app: KoinApplication): DispatcherProvider =\n        app.koin.get()\n}<\/code><\/pre>\n\n\n\n<p>In Dagger dependencies are configured unscoped, so that the underlying KoinApplication keeps the ownership \/ track of instance lifetime. Finally, to have new applications sharing the same modules not depend on Dagger2, we define them as <code>compileOnly<\/code>:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>dependencies {\n    compileOnly(libs.dagger2.core)\n    ksp(libs.dagger2.compiler)\n}<\/code><\/pre>\n\n\n\n<p>Then all what is missing is to add <code>KoinCompatModule<\/code> and <code>CoreDomainModule<\/code> to the specific Dagger component you want to have them in. Obviously all this works best with singleton \/ unscoped dependencies, i.e. when it comes to activity or even viewmodel-scoped dependencies this will probably not work so easily, but it&#8217;s a start \ud83d\ude42<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In my current project we are slowly migrating from Dagger2 (non-Hilt) into the Koin world, module by module, and figured we need to bridge a couple of new things into the old world. But how exactly would one do that? My solution was to let Dagger create the Koin graph as part of it&#8217;s own &hellip; <a href=\"https:\/\/www.thomaskeller.biz\/blog\/2025\/01\/23\/bridging-koin-and-dagger2\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Bridging Koin and Dagger2<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[34,33,35],"class_list":["post-1448","post","type-post","status-publish","format-standard","hentry","category-uncategorized","tag-android","tag-dagger2","tag-koin"],"_links":{"self":[{"href":"https:\/\/www.thomaskeller.biz\/blog\/wp-json\/wp\/v2\/posts\/1448","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.thomaskeller.biz\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.thomaskeller.biz\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.thomaskeller.biz\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.thomaskeller.biz\/blog\/wp-json\/wp\/v2\/comments?post=1448"}],"version-history":[{"count":1,"href":"https:\/\/www.thomaskeller.biz\/blog\/wp-json\/wp\/v2\/posts\/1448\/revisions"}],"predecessor-version":[{"id":1449,"href":"https:\/\/www.thomaskeller.biz\/blog\/wp-json\/wp\/v2\/posts\/1448\/revisions\/1449"}],"wp:attachment":[{"href":"https:\/\/www.thomaskeller.biz\/blog\/wp-json\/wp\/v2\/media?parent=1448"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.thomaskeller.biz\/blog\/wp-json\/wp\/v2\/categories?post=1448"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.thomaskeller.biz\/blog\/wp-json\/wp\/v2\/tags?post=1448"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}