SlideShare a Scribd company logo
1 of 2
Download to read offline
Koin 3.4
Cheat Sheet
For Android
Koin is a Kotlin framework to help you build any kind of Kotlin
application, from Android mobile to backend Ktor server
applications, including Kotlin Multiplatform and Compose.
Gradle Setup
Check the Koin setup page for the latest version:
setup.insert-koin.io
Koin Cloud_Inject is a platform for developers
that allows you to :
• Anticipate your application architecture
behaviour so you can squash issues before
they occur.
• Foresee trends in your development
roadmap so you can deploy new features
safer and faster
Register to join the closed beta
testing program here : register.kotzilla.io
Declaring your Components with Modules
Use the Koin DSL to describe your components
inside modules:
Koin DSL Keywords
In a Koin module, you can use the following keywords:
Lazy Modules & Background Loading
You may define modules with lazyModule , to load
them in the background later:
Retrieving dependencies with Koin API
To retrieve definitions in your Android classes with by inject()
or the by viewModel():
Start Koin
Start and configure your Koin application with the following
DSL:
In your Koin application use the lazyModules
function to load your lazy modules in the
background
Or directly with constructors:
class MyClassA()
class MyClassB(val classA : MyClassA)
val myModule = module {
single { MyClassA() }
single { MyClassB(get()) }
}
class MyApplication : Application(){
override fun onCreate() {
super.onCreate()
startKoin {
androidLogger()
androidContext(this@MyApplication)
modules(appModule)
}
}
}
val myLazyModule = lazyModule {
// definitions
}
startKoin {
// background loading
lazyModules(appModule)
}
KoinPlatform.getKoin().runOnKoinStarted { koin ->
// run code after start is completed
}
class MyActivity : AppCompatActivity() {
val presenter by inject<Presenter>()
val myViewModel by viewModel<MyViewModel>()
}
val myModule = module {
singleOf(::MyClassA)
singleOf(::MyClassB)
}
Function
DSL
Constructor
DSL
Description
includes - Includes other Koin modules
single singleOf Singleton instance definition
(unique instance)
factory factoryOf Factory instance definition
(instance recreated each time)
scope - Define a Scope for given type or
qualifier
scoped scopedOf Define a scoped instance (live
for the duration of the scope)
viewModel viewModelOf Android ViewModel instance
fragment fragmentOf Android Fragment instance
worker workerOf Android Worker for work
Manager
Start DSL Description
startKoin Start a Koin instance
androidLogger Use the Android logger for Koin
androidContext Use the Android Context to be
resolved in Koin
modules Load a list of Koin modules
Your new
application
architecture
superpowers
Koin Annotations
Cheat Sheet
For Android
The goal of the Koin Annotations project is to help
declare the Koin definition in a very fast and intuitive
way and generate all underlying Koin DSL for you.
KSP & Gradle Setup
Check the Koin setup page for latest version:
setup.insert-koin.io
Be sure to configure your project to use KSP
generated sources.
Declaring with a
Kotlin function (new in 1.2)
Use Koin Annotations on your top-level function to declare it
in Koin:
Koin Annotations
The following annotations can be used in classes:
Start Koin
Start and configure your Koin instance with the regular
Start DSL. To use an annotated module class, just
instantiate it and use the .module generated extension.
Don’t forget to use the right import for generated
sources:
Modules
Annotate a class with @Module and @ComponentScan, to
scan for annotated components in the given package:
Declaring your Components
Use Koin Annotations on your classes to declare it
in Koin. Annotated classes will be injected into their
first constructor:
You can inject the Android context easily: just use a
Context parameter in your constructor.
You can also declare components directly in a
module class:
Specify included modules directly on the @Module
annotation with the includes attribute:
Any interface is automatically detected
as a bound type:
Injected dependency can be nullable:
Lazy type and List types are detected (new in 1.2)
Injected parameter can declared just by tagging
your parameter with @InjectedParam
@Single
class MyClassA()
@Single
class MyClassB(val classA : MyClassA)
@Single
class MyClassB(val classA : MyClassA) : MyInterface
@Single
class MyClassB(val classA : MyClassA?)
// Detect Lazy
@Single
class MyClassB(val classA : Lazy<MyClassA>)
// Detect List
@Single
class MyClassB(val allInterfaces : List<MyInterface>)
@Single
class MyClass(@InjectedParam val id : String)
@Single
fun buildMyClassB(classA : MyClassA) = MyClassB(classA)
@Module
@ComponentScan(«com.my.package»)
class MyModule
@Module
class MyModule {
@Single
fun myClassA() = MyClassA()
@Single
fun myClassB(classA : MyClassA) = MyClassB(classA)
}
// Use Koin Generation
import org.koin.ksp.generated.*
startKoin {
androidLogger()
androidContext(this@MyApplication)
modules(MyModule().module)
}
@Module(includes = [Module1::class])
class MyModule
Annotation Description
@Module Declare class as a Koin module. Can
also use includes parameter, to specify
included modules
@ComponentScan Scan Koin definitions for given package
@Single Singleton instance definition (unique
instance)
@Factory Factory instance definition (instance
recreated each time)
@Scope Define a Scope for given type or qualifier
@Scoped Define a scoped instance (live for the
duration of the scope) for given type scope
@KoinViewModel Android ViewModel instance
@KoinWorker Android Worker WorkManager
www.kotzilla.io
@kotzilla_io

More Related Content

Similar to Cheat-sheet_Koin_2023.pdf

Extending Appcelerator Titanium Mobile through Native Modules
Extending Appcelerator Titanium Mobile through Native ModulesExtending Appcelerator Titanium Mobile through Native Modules
Extending Appcelerator Titanium Mobile through Native Modulesomorandi
 
Object-oriented programming (OOP) with Complete understanding modules
Object-oriented programming (OOP) with Complete understanding modulesObject-oriented programming (OOP) with Complete understanding modules
Object-oriented programming (OOP) with Complete understanding modulesDurgesh Singh
 
React native bridge for iOS and android
 React native bridge for iOS and android React native bridge for iOS and android
React native bridge for iOS and androidKaty Slemon
 
NamEKuLDeePSInGhBCA6yfggyyjjfdgjkkh.pptx
NamEKuLDeePSInGhBCA6yfggyyjjfdgjkkh.pptxNamEKuLDeePSInGhBCA6yfggyyjjfdgjkkh.pptx
NamEKuLDeePSInGhBCA6yfggyyjjfdgjkkh.pptxPushpendrachaturvedi9
 
MPointInc_AndroidSDK_Documentation
MPointInc_AndroidSDK_DocumentationMPointInc_AndroidSDK_Documentation
MPointInc_AndroidSDK_DocumentationXin Shao
 
Flex Daily Solutions @ FITC 2008
Flex Daily Solutions @ FITC 2008Flex Daily Solutions @ FITC 2008
Flex Daily Solutions @ FITC 2008marcocasario
 
Unit Testing Using Mockito in Android (1).pdf
Unit Testing Using Mockito in Android (1).pdfUnit Testing Using Mockito in Android (1).pdf
Unit Testing Using Mockito in Android (1).pdfKaty Slemon
 
Net framework session03
Net framework session03Net framework session03
Net framework session03Vivek chan
 
React Basic and Advance || React Basic
React Basic and Advance   || React BasicReact Basic and Advance   || React Basic
React Basic and Advance || React Basicrafaqathussainc077
 
Overview & Downloading the Baseline using Global Configuration Managemen tand...
Overview & Downloading the Baseline using Global Configuration Managemen tand...Overview & Downloading the Baseline using Global Configuration Managemen tand...
Overview & Downloading the Baseline using Global Configuration Managemen tand...Bharat Malge
 
MuleSoft Surat Live Demonstration Virtual Meetup#3 - Building JWT OAuth 2.0 C...
MuleSoft Surat Live Demonstration Virtual Meetup#3 - Building JWT OAuth 2.0 C...MuleSoft Surat Live Demonstration Virtual Meetup#3 - Building JWT OAuth 2.0 C...
MuleSoft Surat Live Demonstration Virtual Meetup#3 - Building JWT OAuth 2.0 C...Jitendra Bafna
 
Swift 5.2 what are the new things that you need to know about
Swift 5.2   what are the new things that you need to know aboutSwift 5.2   what are the new things that you need to know about
Swift 5.2 what are the new things that you need to know aboutConcetto Labs
 
Migrating an Application from Angular 1 to Angular 2
Migrating an Application from Angular 1 to Angular 2 Migrating an Application from Angular 1 to Angular 2
Migrating an Application from Angular 1 to Angular 2 Ross Dederer
 
Android architecture
Android architecture Android architecture
Android architecture Trong-An Bui
 
Titanium Studio [Updated - 18/12/2011]
Titanium Studio [Updated - 18/12/2011]Titanium Studio [Updated - 18/12/2011]
Titanium Studio [Updated - 18/12/2011]Sentinel Solutions Ltd
 

Similar to Cheat-sheet_Koin_2023.pdf (20)

Extending Appcelerator Titanium Mobile through Native Modules
Extending Appcelerator Titanium Mobile through Native ModulesExtending Appcelerator Titanium Mobile through Native Modules
Extending Appcelerator Titanium Mobile through Native Modules
 
Object-oriented programming (OOP) with Complete understanding modules
Object-oriented programming (OOP) with Complete understanding modulesObject-oriented programming (OOP) with Complete understanding modules
Object-oriented programming (OOP) with Complete understanding modules
 
React native bridge for iOS and android
 React native bridge for iOS and android React native bridge for iOS and android
React native bridge for iOS and android
 
Hilt Annotations
Hilt AnnotationsHilt Annotations
Hilt Annotations
 
Meteor
MeteorMeteor
Meteor
 
NamEKuLDeePSInGhBCA6yfggyyjjfdgjkkh.pptx
NamEKuLDeePSInGhBCA6yfggyyjjfdgjkkh.pptxNamEKuLDeePSInGhBCA6yfggyyjjfdgjkkh.pptx
NamEKuLDeePSInGhBCA6yfggyyjjfdgjkkh.pptx
 
MPointInc_AndroidSDK_Documentation
MPointInc_AndroidSDK_DocumentationMPointInc_AndroidSDK_Documentation
MPointInc_AndroidSDK_Documentation
 
Spring Basics
Spring BasicsSpring Basics
Spring Basics
 
Flex Daily Solutions @ FITC 2008
Flex Daily Solutions @ FITC 2008Flex Daily Solutions @ FITC 2008
Flex Daily Solutions @ FITC 2008
 
Spring Framework
Spring FrameworkSpring Framework
Spring Framework
 
Unit Testing Using Mockito in Android (1).pdf
Unit Testing Using Mockito in Android (1).pdfUnit Testing Using Mockito in Android (1).pdf
Unit Testing Using Mockito in Android (1).pdf
 
Net framework session03
Net framework session03Net framework session03
Net framework session03
 
React Basic and Advance || React Basic
React Basic and Advance   || React BasicReact Basic and Advance   || React Basic
React Basic and Advance || React Basic
 
Overview & Downloading the Baseline using Global Configuration Managemen tand...
Overview & Downloading the Baseline using Global Configuration Managemen tand...Overview & Downloading the Baseline using Global Configuration Managemen tand...
Overview & Downloading the Baseline using Global Configuration Managemen tand...
 
MuleSoft Surat Live Demonstration Virtual Meetup#3 - Building JWT OAuth 2.0 C...
MuleSoft Surat Live Demonstration Virtual Meetup#3 - Building JWT OAuth 2.0 C...MuleSoft Surat Live Demonstration Virtual Meetup#3 - Building JWT OAuth 2.0 C...
MuleSoft Surat Live Demonstration Virtual Meetup#3 - Building JWT OAuth 2.0 C...
 
Swift 5.2 what are the new things that you need to know about
Swift 5.2   what are the new things that you need to know aboutSwift 5.2   what are the new things that you need to know about
Swift 5.2 what are the new things that you need to know about
 
Migrating an Application from Angular 1 to Angular 2
Migrating an Application from Angular 1 to Angular 2 Migrating an Application from Angular 1 to Angular 2
Migrating an Application from Angular 1 to Angular 2
 
Android architecture
Android architecture Android architecture
Android architecture
 
Titanium Studio [Updated - 18/12/2011]
Titanium Studio [Updated - 18/12/2011]Titanium Studio [Updated - 18/12/2011]
Titanium Studio [Updated - 18/12/2011]
 
csharp.docx
csharp.docxcsharp.docx
csharp.docx
 

Recently uploaded

RE Capital's Visionary Leadership under Newman Leech
RE Capital's Visionary Leadership under Newman LeechRE Capital's Visionary Leadership under Newman Leech
RE Capital's Visionary Leadership under Newman LeechNewman George Leech
 
VIP Call Girls In Saharaganj ( Lucknow ) 🔝 8923113531 🔝 Cash Payment (COD) 👒
VIP Call Girls In Saharaganj ( Lucknow  ) 🔝 8923113531 🔝  Cash Payment (COD) 👒VIP Call Girls In Saharaganj ( Lucknow  ) 🔝 8923113531 🔝  Cash Payment (COD) 👒
VIP Call Girls In Saharaganj ( Lucknow ) 🔝 8923113531 🔝 Cash Payment (COD) 👒anilsa9823
 
Cash Payment 9602870969 Escort Service in Udaipur Call Girls
Cash Payment 9602870969 Escort Service in Udaipur Call GirlsCash Payment 9602870969 Escort Service in Udaipur Call Girls
Cash Payment 9602870969 Escort Service in Udaipur Call GirlsApsara Of India
 
GD Birla and his contribution in management
GD Birla and his contribution in managementGD Birla and his contribution in management
GD Birla and his contribution in managementchhavia330
 
Mysore Call Girls 8617370543 WhatsApp Number 24x7 Best Services
Mysore Call Girls 8617370543 WhatsApp Number 24x7 Best ServicesMysore Call Girls 8617370543 WhatsApp Number 24x7 Best Services
Mysore Call Girls 8617370543 WhatsApp Number 24x7 Best ServicesDipal Arora
 
Ensure the security of your HCL environment by applying the Zero Trust princi...
Ensure the security of your HCL environment by applying the Zero Trust princi...Ensure the security of your HCL environment by applying the Zero Trust princi...
Ensure the security of your HCL environment by applying the Zero Trust princi...Roland Driesen
 
Call Girls in Gomti Nagar - 7388211116 - With room Service
Call Girls in Gomti Nagar - 7388211116  - With room ServiceCall Girls in Gomti Nagar - 7388211116  - With room Service
Call Girls in Gomti Nagar - 7388211116 - With room Servicediscovermytutordmt
 
Yaroslav Rozhankivskyy: Три складові і три передумови максимальної продуктивн...
Yaroslav Rozhankivskyy: Три складові і три передумови максимальної продуктивн...Yaroslav Rozhankivskyy: Три складові і три передумови максимальної продуктивн...
Yaroslav Rozhankivskyy: Три складові і три передумови максимальної продуктивн...Lviv Startup Club
 
The CMO Survey - Highlights and Insights Report - Spring 2024
The CMO Survey - Highlights and Insights Report - Spring 2024The CMO Survey - Highlights and Insights Report - Spring 2024
The CMO Survey - Highlights and Insights Report - Spring 2024christinemoorman
 
VIP Kolkata Call Girl Howrah 👉 8250192130 Available With Room
VIP Kolkata Call Girl Howrah 👉 8250192130  Available With RoomVIP Kolkata Call Girl Howrah 👉 8250192130  Available With Room
VIP Kolkata Call Girl Howrah 👉 8250192130 Available With Roomdivyansh0kumar0
 
BEST ✨ Call Girls In Indirapuram Ghaziabad ✔️ 9871031762 ✔️ Escorts Service...
BEST ✨ Call Girls In  Indirapuram Ghaziabad  ✔️ 9871031762 ✔️ Escorts Service...BEST ✨ Call Girls In  Indirapuram Ghaziabad  ✔️ 9871031762 ✔️ Escorts Service...
BEST ✨ Call Girls In Indirapuram Ghaziabad ✔️ 9871031762 ✔️ Escorts Service...noida100girls
 
Creating Low-Code Loan Applications using the Trisotech Mortgage Feature Set
Creating Low-Code Loan Applications using the Trisotech Mortgage Feature SetCreating Low-Code Loan Applications using the Trisotech Mortgage Feature Set
Creating Low-Code Loan Applications using the Trisotech Mortgage Feature SetDenis Gagné
 
DEPED Work From Home WORKWEEK-PLAN.docx
DEPED Work From Home  WORKWEEK-PLAN.docxDEPED Work From Home  WORKWEEK-PLAN.docx
DEPED Work From Home WORKWEEK-PLAN.docxRodelinaLaud
 
MONA 98765-12871 CALL GIRLS IN LUDHIANA LUDHIANA CALL GIRL
MONA 98765-12871 CALL GIRLS IN LUDHIANA LUDHIANA CALL GIRLMONA 98765-12871 CALL GIRLS IN LUDHIANA LUDHIANA CALL GIRL
MONA 98765-12871 CALL GIRLS IN LUDHIANA LUDHIANA CALL GIRLSeo
 
Call Girls Pune Just Call 9907093804 Top Class Call Girl Service Available
Call Girls Pune Just Call 9907093804 Top Class Call Girl Service AvailableCall Girls Pune Just Call 9907093804 Top Class Call Girl Service Available
Call Girls Pune Just Call 9907093804 Top Class Call Girl Service AvailableDipal Arora
 
Mondelez State of Snacking and Future Trends 2023
Mondelez State of Snacking and Future Trends 2023Mondelez State of Snacking and Future Trends 2023
Mondelez State of Snacking and Future Trends 2023Neil Kimberley
 
Catalogue ONG NUOC PPR DE NHAT .pdf
Catalogue ONG NUOC PPR DE NHAT      .pdfCatalogue ONG NUOC PPR DE NHAT      .pdf
Catalogue ONG NUOC PPR DE NHAT .pdfOrient Homes
 
Eni 2024 1Q Results - 24.04.24 business.
Eni 2024 1Q Results - 24.04.24 business.Eni 2024 1Q Results - 24.04.24 business.
Eni 2024 1Q Results - 24.04.24 business.Eni
 

Recently uploaded (20)

RE Capital's Visionary Leadership under Newman Leech
RE Capital's Visionary Leadership under Newman LeechRE Capital's Visionary Leadership under Newman Leech
RE Capital's Visionary Leadership under Newman Leech
 
VIP Call Girls In Saharaganj ( Lucknow ) 🔝 8923113531 🔝 Cash Payment (COD) 👒
VIP Call Girls In Saharaganj ( Lucknow  ) 🔝 8923113531 🔝  Cash Payment (COD) 👒VIP Call Girls In Saharaganj ( Lucknow  ) 🔝 8923113531 🔝  Cash Payment (COD) 👒
VIP Call Girls In Saharaganj ( Lucknow ) 🔝 8923113531 🔝 Cash Payment (COD) 👒
 
KestrelPro Flyer Japan IT Week 2024 (English)
KestrelPro Flyer Japan IT Week 2024 (English)KestrelPro Flyer Japan IT Week 2024 (English)
KestrelPro Flyer Japan IT Week 2024 (English)
 
Cash Payment 9602870969 Escort Service in Udaipur Call Girls
Cash Payment 9602870969 Escort Service in Udaipur Call GirlsCash Payment 9602870969 Escort Service in Udaipur Call Girls
Cash Payment 9602870969 Escort Service in Udaipur Call Girls
 
GD Birla and his contribution in management
GD Birla and his contribution in managementGD Birla and his contribution in management
GD Birla and his contribution in management
 
Mysore Call Girls 8617370543 WhatsApp Number 24x7 Best Services
Mysore Call Girls 8617370543 WhatsApp Number 24x7 Best ServicesMysore Call Girls 8617370543 WhatsApp Number 24x7 Best Services
Mysore Call Girls 8617370543 WhatsApp Number 24x7 Best Services
 
Ensure the security of your HCL environment by applying the Zero Trust princi...
Ensure the security of your HCL environment by applying the Zero Trust princi...Ensure the security of your HCL environment by applying the Zero Trust princi...
Ensure the security of your HCL environment by applying the Zero Trust princi...
 
Call Girls in Gomti Nagar - 7388211116 - With room Service
Call Girls in Gomti Nagar - 7388211116  - With room ServiceCall Girls in Gomti Nagar - 7388211116  - With room Service
Call Girls in Gomti Nagar - 7388211116 - With room Service
 
Yaroslav Rozhankivskyy: Три складові і три передумови максимальної продуктивн...
Yaroslav Rozhankivskyy: Три складові і три передумови максимальної продуктивн...Yaroslav Rozhankivskyy: Три складові і три передумови максимальної продуктивн...
Yaroslav Rozhankivskyy: Три складові і три передумови максимальної продуктивн...
 
The CMO Survey - Highlights and Insights Report - Spring 2024
The CMO Survey - Highlights and Insights Report - Spring 2024The CMO Survey - Highlights and Insights Report - Spring 2024
The CMO Survey - Highlights and Insights Report - Spring 2024
 
VIP Kolkata Call Girl Howrah 👉 8250192130 Available With Room
VIP Kolkata Call Girl Howrah 👉 8250192130  Available With RoomVIP Kolkata Call Girl Howrah 👉 8250192130  Available With Room
VIP Kolkata Call Girl Howrah 👉 8250192130 Available With Room
 
BEST ✨ Call Girls In Indirapuram Ghaziabad ✔️ 9871031762 ✔️ Escorts Service...
BEST ✨ Call Girls In  Indirapuram Ghaziabad  ✔️ 9871031762 ✔️ Escorts Service...BEST ✨ Call Girls In  Indirapuram Ghaziabad  ✔️ 9871031762 ✔️ Escorts Service...
BEST ✨ Call Girls In Indirapuram Ghaziabad ✔️ 9871031762 ✔️ Escorts Service...
 
Creating Low-Code Loan Applications using the Trisotech Mortgage Feature Set
Creating Low-Code Loan Applications using the Trisotech Mortgage Feature SetCreating Low-Code Loan Applications using the Trisotech Mortgage Feature Set
Creating Low-Code Loan Applications using the Trisotech Mortgage Feature Set
 
DEPED Work From Home WORKWEEK-PLAN.docx
DEPED Work From Home  WORKWEEK-PLAN.docxDEPED Work From Home  WORKWEEK-PLAN.docx
DEPED Work From Home WORKWEEK-PLAN.docx
 
MONA 98765-12871 CALL GIRLS IN LUDHIANA LUDHIANA CALL GIRL
MONA 98765-12871 CALL GIRLS IN LUDHIANA LUDHIANA CALL GIRLMONA 98765-12871 CALL GIRLS IN LUDHIANA LUDHIANA CALL GIRL
MONA 98765-12871 CALL GIRLS IN LUDHIANA LUDHIANA CALL GIRL
 
Call Girls Pune Just Call 9907093804 Top Class Call Girl Service Available
Call Girls Pune Just Call 9907093804 Top Class Call Girl Service AvailableCall Girls Pune Just Call 9907093804 Top Class Call Girl Service Available
Call Girls Pune Just Call 9907093804 Top Class Call Girl Service Available
 
Mondelez State of Snacking and Future Trends 2023
Mondelez State of Snacking and Future Trends 2023Mondelez State of Snacking and Future Trends 2023
Mondelez State of Snacking and Future Trends 2023
 
Forklift Operations: Safety through Cartoons
Forklift Operations: Safety through CartoonsForklift Operations: Safety through Cartoons
Forklift Operations: Safety through Cartoons
 
Catalogue ONG NUOC PPR DE NHAT .pdf
Catalogue ONG NUOC PPR DE NHAT      .pdfCatalogue ONG NUOC PPR DE NHAT      .pdf
Catalogue ONG NUOC PPR DE NHAT .pdf
 
Eni 2024 1Q Results - 24.04.24 business.
Eni 2024 1Q Results - 24.04.24 business.Eni 2024 1Q Results - 24.04.24 business.
Eni 2024 1Q Results - 24.04.24 business.
 

Cheat-sheet_Koin_2023.pdf

  • 1. Koin 3.4 Cheat Sheet For Android Koin is a Kotlin framework to help you build any kind of Kotlin application, from Android mobile to backend Ktor server applications, including Kotlin Multiplatform and Compose. Gradle Setup Check the Koin setup page for the latest version: setup.insert-koin.io Koin Cloud_Inject is a platform for developers that allows you to : • Anticipate your application architecture behaviour so you can squash issues before they occur. • Foresee trends in your development roadmap so you can deploy new features safer and faster Register to join the closed beta testing program here : register.kotzilla.io Declaring your Components with Modules Use the Koin DSL to describe your components inside modules: Koin DSL Keywords In a Koin module, you can use the following keywords: Lazy Modules & Background Loading You may define modules with lazyModule , to load them in the background later: Retrieving dependencies with Koin API To retrieve definitions in your Android classes with by inject() or the by viewModel(): Start Koin Start and configure your Koin application with the following DSL: In your Koin application use the lazyModules function to load your lazy modules in the background Or directly with constructors: class MyClassA() class MyClassB(val classA : MyClassA) val myModule = module { single { MyClassA() } single { MyClassB(get()) } } class MyApplication : Application(){ override fun onCreate() { super.onCreate() startKoin { androidLogger() androidContext(this@MyApplication) modules(appModule) } } } val myLazyModule = lazyModule { // definitions } startKoin { // background loading lazyModules(appModule) } KoinPlatform.getKoin().runOnKoinStarted { koin -> // run code after start is completed } class MyActivity : AppCompatActivity() { val presenter by inject<Presenter>() val myViewModel by viewModel<MyViewModel>() } val myModule = module { singleOf(::MyClassA) singleOf(::MyClassB) } Function DSL Constructor DSL Description includes - Includes other Koin modules single singleOf Singleton instance definition (unique instance) factory factoryOf Factory instance definition (instance recreated each time) scope - Define a Scope for given type or qualifier scoped scopedOf Define a scoped instance (live for the duration of the scope) viewModel viewModelOf Android ViewModel instance fragment fragmentOf Android Fragment instance worker workerOf Android Worker for work Manager Start DSL Description startKoin Start a Koin instance androidLogger Use the Android logger for Koin androidContext Use the Android Context to be resolved in Koin modules Load a list of Koin modules Your new application architecture superpowers
  • 2. Koin Annotations Cheat Sheet For Android The goal of the Koin Annotations project is to help declare the Koin definition in a very fast and intuitive way and generate all underlying Koin DSL for you. KSP & Gradle Setup Check the Koin setup page for latest version: setup.insert-koin.io Be sure to configure your project to use KSP generated sources. Declaring with a Kotlin function (new in 1.2) Use Koin Annotations on your top-level function to declare it in Koin: Koin Annotations The following annotations can be used in classes: Start Koin Start and configure your Koin instance with the regular Start DSL. To use an annotated module class, just instantiate it and use the .module generated extension. Don’t forget to use the right import for generated sources: Modules Annotate a class with @Module and @ComponentScan, to scan for annotated components in the given package: Declaring your Components Use Koin Annotations on your classes to declare it in Koin. Annotated classes will be injected into their first constructor: You can inject the Android context easily: just use a Context parameter in your constructor. You can also declare components directly in a module class: Specify included modules directly on the @Module annotation with the includes attribute: Any interface is automatically detected as a bound type: Injected dependency can be nullable: Lazy type and List types are detected (new in 1.2) Injected parameter can declared just by tagging your parameter with @InjectedParam @Single class MyClassA() @Single class MyClassB(val classA : MyClassA) @Single class MyClassB(val classA : MyClassA) : MyInterface @Single class MyClassB(val classA : MyClassA?) // Detect Lazy @Single class MyClassB(val classA : Lazy<MyClassA>) // Detect List @Single class MyClassB(val allInterfaces : List<MyInterface>) @Single class MyClass(@InjectedParam val id : String) @Single fun buildMyClassB(classA : MyClassA) = MyClassB(classA) @Module @ComponentScan(«com.my.package») class MyModule @Module class MyModule { @Single fun myClassA() = MyClassA() @Single fun myClassB(classA : MyClassA) = MyClassB(classA) } // Use Koin Generation import org.koin.ksp.generated.* startKoin { androidLogger() androidContext(this@MyApplication) modules(MyModule().module) } @Module(includes = [Module1::class]) class MyModule Annotation Description @Module Declare class as a Koin module. Can also use includes parameter, to specify included modules @ComponentScan Scan Koin definitions for given package @Single Singleton instance definition (unique instance) @Factory Factory instance definition (instance recreated each time) @Scope Define a Scope for given type or qualifier @Scoped Define a scoped instance (live for the duration of the scope) for given type scope @KoinViewModel Android ViewModel instance @KoinWorker Android Worker WorkManager www.kotzilla.io @kotzilla_io