Skip to content

Instantly share code, notes, and snippets.

View arnaudgiuliani's full-sized avatar

Arnaud Giuliani arnaudgiuliani

View GitHub Profile
// DSL — now with compile-time auto-wiring
val appModule = module {
    single<Database>()
    single<UserRepository>()
    viewModel<MainViewModel>()
}
// Or Annotations — same power
@Singleton
class UserRepository(private val database: Database)
@arnaudgiuliani
arnaudgiuliani / koin_classic.kt
Created February 4, 2026 07:42
Koin App Module - First DSL
val appModule = module {
    single { Database() }
    single { UserRepository(get()) }
    viewModel { MainViewModel(get()) }
}
Koin
Overall Statistics Across All Sessions:
MainActivityViewModel: Avg = 0.166 ms, Min = 0.146 ms, Max = 0.198 ms, Standard Error ± 0.002 ms
ForYouViewModel: Avg = 2.052 ms, Min = 0.223 ms, Max = 9.042 ms, Standard Error ± 0.302 ms
AppStartup: Avg = 1416.360 ms, Min = 1204.000 ms, Max = 1746.000 ms, Standard Error ± 37.072 ms
--
Hilt
// generated sources
import org.koin.ksp.generated.*
fun main(){
startKoin {
modules(CoffeeAppModule().module)
}
}
@Module
@ComponentScan("org.koin.sample.coffee")
class CoffeeAppModule
@Single
class ElectricHeater : Heater
@Single
class Thermosiphon(private val heater: Heater) : Pump
@Single
class CoffeeMaker(private val pump: Pump, private val heater: Heater)
val dataModule = module {
// data components definitions
}
val featureModule1 = module {
includes(dataModule)
// feature1 definitions
}
val featureModule2 = module {
includes(dataModule)
// feature2 definitions
class ClassA(val b : ClassB)
class ClassB()
module {
singleOf(::ClassA)
singleOf(::ClassB)
}
class MyAdapter(val presenter : MyPresenter)
module {
scope<MyActivity> {
// get MyPresenter instance from current scope
scoped { MyAdapter(get()) }
scoped { MyPresenter() }
}
}
class MyActivity : AppCompatActivity, AndroidScopeComponent {
// use Activity Retained Scope
override val scope : Scope by activityRetainedScope()
val presenter : MyPresenter by inject()
}