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

Abortion pills in Jeddah ! +27737758557, cytotec pill riyadh. Saudi Arabia" A...
Abortion pills in Jeddah ! +27737758557, cytotec pill riyadh. Saudi Arabia" A...Abortion pills in Jeddah ! +27737758557, cytotec pill riyadh. Saudi Arabia" A...
Abortion pills in Jeddah ! +27737758557, cytotec pill riyadh. Saudi Arabia" A...bleessingsbender
 
Falcon Invoice Discounting: Aviate Your Cash Flow Challenges
Falcon Invoice Discounting: Aviate Your Cash Flow ChallengesFalcon Invoice Discounting: Aviate Your Cash Flow Challenges
Falcon Invoice Discounting: Aviate Your Cash Flow Challengeshemanthkumar470700
 
Unveiling Falcon Invoice Discounting: Leading the Way as India's Premier Bill...
Unveiling Falcon Invoice Discounting: Leading the Way as India's Premier Bill...Unveiling Falcon Invoice Discounting: Leading the Way as India's Premier Bill...
Unveiling Falcon Invoice Discounting: Leading the Way as India's Premier Bill...Falcon Invoice Discounting
 
Arti Languages Pre Seed Teaser Deck 2024.pdf
Arti Languages Pre Seed Teaser Deck 2024.pdfArti Languages Pre Seed Teaser Deck 2024.pdf
Arti Languages Pre Seed Teaser Deck 2024.pdfwill854175
 
10 Influential Leaders Defining the Future of Digital Banking in 2024.pdf
10 Influential Leaders Defining the Future of Digital Banking in 2024.pdf10 Influential Leaders Defining the Future of Digital Banking in 2024.pdf
10 Influential Leaders Defining the Future of Digital Banking in 2024.pdfciolook1
 
Ital Liptz - all about Itai Liptz. news.
Ital Liptz - all about Itai Liptz. news.Ital Liptz - all about Itai Liptz. news.
Ital Liptz - all about Itai Liptz. news.htj82vpw
 
A DAY IN THE LIFE OF A SALESPERSON .pptx
A DAY IN THE LIFE OF A SALESPERSON .pptxA DAY IN THE LIFE OF A SALESPERSON .pptx
A DAY IN THE LIFE OF A SALESPERSON .pptxseemajojo02
 
Falcon Invoice Discounting: Unlock Your Business Potential
Falcon Invoice Discounting: Unlock Your Business PotentialFalcon Invoice Discounting: Unlock Your Business Potential
Falcon Invoice Discounting: Unlock Your Business PotentialFalcon investment
 
PHX May 2024 Corporate Presentation Final
PHX May 2024 Corporate Presentation FinalPHX May 2024 Corporate Presentation Final
PHX May 2024 Corporate Presentation FinalPanhandleOilandGas
 
PALWAL CALL GIRL ❤ 82729*64427❤ CALL GIRLS IN PALWAL ESCORTS
PALWAL CALL GIRL ❤ 82729*64427❤ CALL GIRLS IN PALWAL ESCORTSPALWAL CALL GIRL ❤ 82729*64427❤ CALL GIRLS IN PALWAL ESCORTS
PALWAL CALL GIRL ❤ 82729*64427❤ CALL GIRLS IN PALWAL ESCORTSkajalroy875762
 
JIND CALL GIRL ❤ 8272964427❤ CALL GIRLS IN JIND ESCORTS SERVICE PROVIDE
JIND CALL GIRL ❤ 8272964427❤ CALL GIRLS IN JIND ESCORTS SERVICE PROVIDEJIND CALL GIRL ❤ 8272964427❤ CALL GIRLS IN JIND ESCORTS SERVICE PROVIDE
JIND CALL GIRL ❤ 8272964427❤ CALL GIRLS IN JIND ESCORTS SERVICE PROVIDEkajalroy875762
 
Thompson_Taylor_MBBS_PB1_2024-03 (1)- Project & Portfolio 2.pptx
Thompson_Taylor_MBBS_PB1_2024-03 (1)- Project & Portfolio 2.pptxThompson_Taylor_MBBS_PB1_2024-03 (1)- Project & Portfolio 2.pptx
Thompson_Taylor_MBBS_PB1_2024-03 (1)- Project & Portfolio 2.pptxtmthompson1
 
Lundin Gold - Q1 2024 Conference Call Presentation (Revised)
Lundin Gold - Q1 2024 Conference Call Presentation (Revised)Lundin Gold - Q1 2024 Conference Call Presentation (Revised)
Lundin Gold - Q1 2024 Conference Call Presentation (Revised)Adnet Communications
 
QSM Chap 10 Service Culture in Tourism and Hospitality Industry.pptx
QSM Chap 10 Service Culture in Tourism and Hospitality Industry.pptxQSM Chap 10 Service Culture in Tourism and Hospitality Industry.pptx
QSM Chap 10 Service Culture in Tourism and Hospitality Industry.pptxDitasDelaCruz
 
2024 May - Clearbit Integration with Hubspot - Greenville HUG.pptx
2024 May - Clearbit Integration with Hubspot  - Greenville HUG.pptx2024 May - Clearbit Integration with Hubspot  - Greenville HUG.pptx
2024 May - Clearbit Integration with Hubspot - Greenville HUG.pptxBoundify
 
Goal Presentation_NEW EMPLOYEE_NETAPS FOUNDATION.pptx
Goal Presentation_NEW EMPLOYEE_NETAPS FOUNDATION.pptxGoal Presentation_NEW EMPLOYEE_NETAPS FOUNDATION.pptx
Goal Presentation_NEW EMPLOYEE_NETAPS FOUNDATION.pptxNetapsFoundationAdmi
 
Understanding Financial Accounting 3rd Canadian Edition by Christopher D. Bur...
Understanding Financial Accounting 3rd Canadian Edition by Christopher D. Bur...Understanding Financial Accounting 3rd Canadian Edition by Christopher D. Bur...
Understanding Financial Accounting 3rd Canadian Edition by Christopher D. Bur...ssuserf63bd7
 
Progress Report - Oracle's OCI Analyst Summit 2024
Progress Report - Oracle's OCI Analyst Summit 2024Progress Report - Oracle's OCI Analyst Summit 2024
Progress Report - Oracle's OCI Analyst Summit 2024Holger Mueller
 

Recently uploaded (20)

WheelTug Short Pitch Deck 2024 | Byond Insights
WheelTug Short Pitch Deck 2024 | Byond InsightsWheelTug Short Pitch Deck 2024 | Byond Insights
WheelTug Short Pitch Deck 2024 | Byond Insights
 
Abortion pills in Jeddah ! +27737758557, cytotec pill riyadh. Saudi Arabia" A...
Abortion pills in Jeddah ! +27737758557, cytotec pill riyadh. Saudi Arabia" A...Abortion pills in Jeddah ! +27737758557, cytotec pill riyadh. Saudi Arabia" A...
Abortion pills in Jeddah ! +27737758557, cytotec pill riyadh. Saudi Arabia" A...
 
Falcon Invoice Discounting: Aviate Your Cash Flow Challenges
Falcon Invoice Discounting: Aviate Your Cash Flow ChallengesFalcon Invoice Discounting: Aviate Your Cash Flow Challenges
Falcon Invoice Discounting: Aviate Your Cash Flow Challenges
 
Unveiling Falcon Invoice Discounting: Leading the Way as India's Premier Bill...
Unveiling Falcon Invoice Discounting: Leading the Way as India's Premier Bill...Unveiling Falcon Invoice Discounting: Leading the Way as India's Premier Bill...
Unveiling Falcon Invoice Discounting: Leading the Way as India's Premier Bill...
 
Arti Languages Pre Seed Teaser Deck 2024.pdf
Arti Languages Pre Seed Teaser Deck 2024.pdfArti Languages Pre Seed Teaser Deck 2024.pdf
Arti Languages Pre Seed Teaser Deck 2024.pdf
 
10 Influential Leaders Defining the Future of Digital Banking in 2024.pdf
10 Influential Leaders Defining the Future of Digital Banking in 2024.pdf10 Influential Leaders Defining the Future of Digital Banking in 2024.pdf
10 Influential Leaders Defining the Future of Digital Banking in 2024.pdf
 
Ital Liptz - all about Itai Liptz. news.
Ital Liptz - all about Itai Liptz. news.Ital Liptz - all about Itai Liptz. news.
Ital Liptz - all about Itai Liptz. news.
 
A DAY IN THE LIFE OF A SALESPERSON .pptx
A DAY IN THE LIFE OF A SALESPERSON .pptxA DAY IN THE LIFE OF A SALESPERSON .pptx
A DAY IN THE LIFE OF A SALESPERSON .pptx
 
Falcon Invoice Discounting: Unlock Your Business Potential
Falcon Invoice Discounting: Unlock Your Business PotentialFalcon Invoice Discounting: Unlock Your Business Potential
Falcon Invoice Discounting: Unlock Your Business Potential
 
PHX May 2024 Corporate Presentation Final
PHX May 2024 Corporate Presentation FinalPHX May 2024 Corporate Presentation Final
PHX May 2024 Corporate Presentation Final
 
PALWAL CALL GIRL ❤ 82729*64427❤ CALL GIRLS IN PALWAL ESCORTS
PALWAL CALL GIRL ❤ 82729*64427❤ CALL GIRLS IN PALWAL ESCORTSPALWAL CALL GIRL ❤ 82729*64427❤ CALL GIRLS IN PALWAL ESCORTS
PALWAL CALL GIRL ❤ 82729*64427❤ CALL GIRLS IN PALWAL ESCORTS
 
JIND CALL GIRL ❤ 8272964427❤ CALL GIRLS IN JIND ESCORTS SERVICE PROVIDE
JIND CALL GIRL ❤ 8272964427❤ CALL GIRLS IN JIND ESCORTS SERVICE PROVIDEJIND CALL GIRL ❤ 8272964427❤ CALL GIRLS IN JIND ESCORTS SERVICE PROVIDE
JIND CALL GIRL ❤ 8272964427❤ CALL GIRLS IN JIND ESCORTS SERVICE PROVIDE
 
Thompson_Taylor_MBBS_PB1_2024-03 (1)- Project & Portfolio 2.pptx
Thompson_Taylor_MBBS_PB1_2024-03 (1)- Project & Portfolio 2.pptxThompson_Taylor_MBBS_PB1_2024-03 (1)- Project & Portfolio 2.pptx
Thompson_Taylor_MBBS_PB1_2024-03 (1)- Project & Portfolio 2.pptx
 
Lundin Gold - Q1 2024 Conference Call Presentation (Revised)
Lundin Gold - Q1 2024 Conference Call Presentation (Revised)Lundin Gold - Q1 2024 Conference Call Presentation (Revised)
Lundin Gold - Q1 2024 Conference Call Presentation (Revised)
 
QSM Chap 10 Service Culture in Tourism and Hospitality Industry.pptx
QSM Chap 10 Service Culture in Tourism and Hospitality Industry.pptxQSM Chap 10 Service Culture in Tourism and Hospitality Industry.pptx
QSM Chap 10 Service Culture in Tourism and Hospitality Industry.pptx
 
2024 May - Clearbit Integration with Hubspot - Greenville HUG.pptx
2024 May - Clearbit Integration with Hubspot  - Greenville HUG.pptx2024 May - Clearbit Integration with Hubspot  - Greenville HUG.pptx
2024 May - Clearbit Integration with Hubspot - Greenville HUG.pptx
 
Goal Presentation_NEW EMPLOYEE_NETAPS FOUNDATION.pptx
Goal Presentation_NEW EMPLOYEE_NETAPS FOUNDATION.pptxGoal Presentation_NEW EMPLOYEE_NETAPS FOUNDATION.pptx
Goal Presentation_NEW EMPLOYEE_NETAPS FOUNDATION.pptx
 
Understanding Financial Accounting 3rd Canadian Edition by Christopher D. Bur...
Understanding Financial Accounting 3rd Canadian Edition by Christopher D. Bur...Understanding Financial Accounting 3rd Canadian Edition by Christopher D. Bur...
Understanding Financial Accounting 3rd Canadian Edition by Christopher D. Bur...
 
HomeRoots Pitch Deck | Investor Insights | April 2024
HomeRoots Pitch Deck | Investor Insights | April 2024HomeRoots Pitch Deck | Investor Insights | April 2024
HomeRoots Pitch Deck | Investor Insights | April 2024
 
Progress Report - Oracle's OCI Analyst Summit 2024
Progress Report - Oracle's OCI Analyst Summit 2024Progress Report - Oracle's OCI Analyst Summit 2024
Progress Report - Oracle's OCI Analyst Summit 2024
 

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