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

MPointInc_AndroidSDK_Documentation
MPointInc_AndroidSDK_DocumentationMPointInc_AndroidSDK_Documentation
MPointInc_AndroidSDK_Documentation
Xin Shao
 

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

The Abortion pills for sale in Qatar@Doha [+27737758557] []Deira Dubai Kuwait
The Abortion pills for sale in Qatar@Doha [+27737758557] []Deira Dubai KuwaitThe Abortion pills for sale in Qatar@Doha [+27737758557] []Deira Dubai Kuwait
The Abortion pills for sale in Qatar@Doha [+27737758557] []Deira Dubai Kuwait
daisycvs
 
FULL ENJOY Call Girls In Majnu Ka Tilla, Delhi Contact Us 8377877756
FULL ENJOY Call Girls In Majnu Ka Tilla, Delhi Contact Us 8377877756FULL ENJOY Call Girls In Majnu Ka Tilla, Delhi Contact Us 8377877756
FULL ENJOY Call Girls In Majnu Ka Tilla, Delhi Contact Us 8377877756
dollysharma2066
 
Chandigarh Escorts Service 📞8868886958📞 Just📲 Call Nihal Chandigarh Call Girl...
Chandigarh Escorts Service 📞8868886958📞 Just📲 Call Nihal Chandigarh Call Girl...Chandigarh Escorts Service 📞8868886958📞 Just📲 Call Nihal Chandigarh Call Girl...
Chandigarh Escorts Service 📞8868886958📞 Just📲 Call Nihal Chandigarh Call Girl...
Sheetaleventcompany
 
Call Girls Hebbal Just Call 👗 7737669865 👗 Top Class Call Girl Service Bangalore
Call Girls Hebbal Just Call 👗 7737669865 👗 Top Class Call Girl Service BangaloreCall Girls Hebbal Just Call 👗 7737669865 👗 Top Class Call Girl Service Bangalore
Call Girls Hebbal Just Call 👗 7737669865 👗 Top Class Call Girl Service Bangalore
amitlee9823
 
Russian Call Girls In Gurgaon ❤️8448577510 ⊹Best Escorts Service In 24/7 Delh...
Russian Call Girls In Gurgaon ❤️8448577510 ⊹Best Escorts Service In 24/7 Delh...Russian Call Girls In Gurgaon ❤️8448577510 ⊹Best Escorts Service In 24/7 Delh...
Russian Call Girls In Gurgaon ❤️8448577510 ⊹Best Escorts Service In 24/7 Delh...
lizamodels9
 
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
dollysharma2066
 
Russian Call Girls In Rajiv Chowk Gurgaon ❤️8448577510 ⊹Best Escorts Service ...
Russian Call Girls In Rajiv Chowk Gurgaon ❤️8448577510 ⊹Best Escorts Service ...Russian Call Girls In Rajiv Chowk Gurgaon ❤️8448577510 ⊹Best Escorts Service ...
Russian Call Girls In Rajiv Chowk Gurgaon ❤️8448577510 ⊹Best Escorts Service ...
lizamodels9
 
Call Girls Jp Nagar Just Call 👗 7737669865 👗 Top Class Call Girl Service Bang...
Call Girls Jp Nagar Just Call 👗 7737669865 👗 Top Class Call Girl Service Bang...Call Girls Jp Nagar Just Call 👗 7737669865 👗 Top Class Call Girl Service Bang...
Call Girls Jp Nagar Just Call 👗 7737669865 👗 Top Class Call Girl Service Bang...
amitlee9823
 

Recently uploaded (20)

The Abortion pills for sale in Qatar@Doha [+27737758557] []Deira Dubai Kuwait
The Abortion pills for sale in Qatar@Doha [+27737758557] []Deira Dubai KuwaitThe Abortion pills for sale in Qatar@Doha [+27737758557] []Deira Dubai Kuwait
The Abortion pills for sale in Qatar@Doha [+27737758557] []Deira Dubai Kuwait
 
Famous Olympic Siblings from the 21st Century
Famous Olympic Siblings from the 21st CenturyFamous Olympic Siblings from the 21st Century
Famous Olympic Siblings from the 21st Century
 
👉Chandigarh Call Girls 👉9878799926👉Just Call👉Chandigarh Call Girl In Chandiga...
👉Chandigarh Call Girls 👉9878799926👉Just Call👉Chandigarh Call Girl In Chandiga...👉Chandigarh Call Girls 👉9878799926👉Just Call👉Chandigarh Call Girl In Chandiga...
👉Chandigarh Call Girls 👉9878799926👉Just Call👉Chandigarh Call Girl In Chandiga...
 
FULL ENJOY Call Girls In Majnu Ka Tilla, Delhi Contact Us 8377877756
FULL ENJOY Call Girls In Majnu Ka Tilla, Delhi Contact Us 8377877756FULL ENJOY Call Girls In Majnu Ka Tilla, Delhi Contact Us 8377877756
FULL ENJOY Call Girls In Majnu Ka Tilla, Delhi Contact Us 8377877756
 
(Anamika) VIP Call Girls Napur Call Now 8617697112 Napur Escorts 24x7
(Anamika) VIP Call Girls Napur Call Now 8617697112 Napur Escorts 24x7(Anamika) VIP Call Girls Napur Call Now 8617697112 Napur Escorts 24x7
(Anamika) VIP Call Girls Napur Call Now 8617697112 Napur Escorts 24x7
 
Call Girls Service In Old Town Dubai ((0551707352)) Old Town Dubai Call Girl ...
Call Girls Service In Old Town Dubai ((0551707352)) Old Town Dubai Call Girl ...Call Girls Service In Old Town Dubai ((0551707352)) Old Town Dubai Call Girl ...
Call Girls Service In Old Town Dubai ((0551707352)) Old Town Dubai Call Girl ...
 
Chandigarh Escorts Service 📞8868886958📞 Just📲 Call Nihal Chandigarh Call Girl...
Chandigarh Escorts Service 📞8868886958📞 Just📲 Call Nihal Chandigarh Call Girl...Chandigarh Escorts Service 📞8868886958📞 Just📲 Call Nihal Chandigarh Call Girl...
Chandigarh Escorts Service 📞8868886958📞 Just📲 Call Nihal Chandigarh Call Girl...
 
Call Girls Hebbal Just Call 👗 7737669865 👗 Top Class Call Girl Service Bangalore
Call Girls Hebbal Just Call 👗 7737669865 👗 Top Class Call Girl Service BangaloreCall Girls Hebbal Just Call 👗 7737669865 👗 Top Class Call Girl Service Bangalore
Call Girls Hebbal Just Call 👗 7737669865 👗 Top Class Call Girl Service Bangalore
 
BAGALUR CALL GIRL IN 98274*61493 ❤CALL GIRLS IN ESCORT SERVICE❤CALL GIRL
BAGALUR CALL GIRL IN 98274*61493 ❤CALL GIRLS IN ESCORT SERVICE❤CALL GIRLBAGALUR CALL GIRL IN 98274*61493 ❤CALL GIRLS IN ESCORT SERVICE❤CALL GIRL
BAGALUR CALL GIRL IN 98274*61493 ❤CALL GIRLS IN ESCORT SERVICE❤CALL GIRL
 
Russian Call Girls In Gurgaon ❤️8448577510 ⊹Best Escorts Service In 24/7 Delh...
Russian Call Girls In Gurgaon ❤️8448577510 ⊹Best Escorts Service In 24/7 Delh...Russian Call Girls In Gurgaon ❤️8448577510 ⊹Best Escorts Service In 24/7 Delh...
Russian Call Girls In Gurgaon ❤️8448577510 ⊹Best Escorts Service In 24/7 Delh...
 
Cheap Rate Call Girls In Noida Sector 62 Metro 959961乂3876
Cheap Rate Call Girls In Noida Sector 62 Metro 959961乂3876Cheap Rate Call Girls In Noida Sector 62 Metro 959961乂3876
Cheap Rate Call Girls In Noida Sector 62 Metro 959961乂3876
 
Falcon Invoice Discounting: Empowering Your Business Growth
Falcon Invoice Discounting: Empowering Your Business GrowthFalcon Invoice Discounting: Empowering Your Business Growth
Falcon Invoice Discounting: Empowering Your Business Growth
 
It will be International Nurses' Day on 12 May
It will be International Nurses' Day on 12 MayIt will be International Nurses' Day on 12 May
It will be International Nurses' Day on 12 May
 
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
 
Phases of Negotiation .pptx
 Phases of Negotiation .pptx Phases of Negotiation .pptx
Phases of Negotiation .pptx
 
Marel Q1 2024 Investor Presentation from May 8, 2024
Marel Q1 2024 Investor Presentation from May 8, 2024Marel Q1 2024 Investor Presentation from May 8, 2024
Marel Q1 2024 Investor Presentation from May 8, 2024
 
Russian Call Girls In Rajiv Chowk Gurgaon ❤️8448577510 ⊹Best Escorts Service ...
Russian Call Girls In Rajiv Chowk Gurgaon ❤️8448577510 ⊹Best Escorts Service ...Russian Call Girls In Rajiv Chowk Gurgaon ❤️8448577510 ⊹Best Escorts Service ...
Russian Call Girls In Rajiv Chowk Gurgaon ❤️8448577510 ⊹Best Escorts Service ...
 
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
 
Call Girls Jp Nagar Just Call 👗 7737669865 👗 Top Class Call Girl Service Bang...
Call Girls Jp Nagar Just Call 👗 7737669865 👗 Top Class Call Girl Service Bang...Call Girls Jp Nagar Just Call 👗 7737669865 👗 Top Class Call Girl Service Bang...
Call Girls Jp Nagar Just Call 👗 7737669865 👗 Top Class Call Girl Service Bang...
 
How to Get Started in Social Media for Art League City
How to Get Started in Social Media for Art League CityHow to Get Started in Social Media for Art League City
How to Get Started in Social Media for Art League City
 

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