SlideShare a Scribd company logo
DAGGER SO SHARP!
DEPENDENCY INJECTION IN ANDROID
TOPICS
 What is Dependency Injection
 What is Dagger
 Pros and Cons of Dagger
 Dagger alternatives
 Sample Implementation
 QA session
WHAT IS DEPENDENCY INJECTION?
 Dependency Injection (DI) is a design pattern used to implement Inversion of Control(IoC). It allows the
creation of dependent objects outside of a class and provides those objects to a class through different
ways. Using DI, we move the creation and binding of the dependent objects outside of the class that
depends on them.
 It's a design pattern(No need for a library to implement)
 We all use some form of DI
Car Engine
Dependency
Depends
DEPENDENCY INJECTION PATTERN
CLIENT
INJECTOR
SERVICE
Uses
HARD COUPLED
EXAMPLE
 BadCar is tightly coupled to
the fate of Engine and
Wheels
BENEFITS OF DI
LOOSE COUPLING EASY TO TEST CODE REUSABILITY
TYPES OF DI
Constructor
injection
Field
injection
Method
injection
POPULAR
LIBRARIES
 Dagger2
 Koin
 Kodein
 Roboguice
 Butterknife
 Android annotation
 Toothpick
WEAPONS,
MONEY AND
DRUGS
A SIDE BY SIDE
COMPARISON OF
DAGGER2, KOIN
AND KODEIN
COMPARISON BETWEEN DAGGER2, KOIN & KODEIN
Dagger 2 Koin Kodein
Written in Java Written in kotlin Written in Kotlin(Java support)
Generates code in build does not generate code(less
build time)
No code generation
Has proxy classes No proxy classes No proxy classes
Uses annotations No annotation No annotation
A lot of boilerplate Less boilerplate
DAGGER2
Car
Engine
Plugs Starter
Wheels
Tires Nuts
Driver
DAGGER2
 Uses Directed Acyclic Graph = DAGger
 There are no cyclic dependencies
 @Module and @Provides: define classes and methods which provide dependencies
 @Inject: request dependencies. Can be used on a constructor, a field, or a method
 @Component: enable selected modules and used for performing dependency injection
LET'S ROLL UP
OUR SLEEVES
AND WRITE
SOME CODE!
BENEFITS OF DAGGER2
 Provides compile-time verification of the whole dependency graph so app doesn't crash at runtime
 Very clear error messages
 Designed for low-end devices
CONS OF DAGGER2
 Too verbose
 Steep learning curve
INTERACTIVE QA
SESSION
ASK AWAY
THANK YOU THE END. THAT'S IT.
REFERENCES &
FURTHER
READING
 https://codinginflow.com/tutorials/android/dagger-2/part-1-introduction
 https://www.tutorialsteacher.com/ioc/dependency-injection
 https://android.jlelse.eu/fundamentals-of-dependency-injection-and-
popular-libraries-in-android-c17cf48b5253
 https://medium.com/@egek92/koin-di-comparison-of-android-dependency-
injection-frameworks-236a6909bc3e
 https://dagger.dev/users-guide
 https://medium.com/@charbgr/bye-bye-dagger-1494118dcd41
 https://www.journaldev.com/20654/android-mvp-dagger2-retrofit-rxjava
 https://android.jlelse.eu/7-steps-to-implement-dagger-2-in-android-
dabc16715a3a
 https://www.vogella.com/tutorials/Dagger/article.html
 https://blog.mindorks.com/introduction-to-dagger-2-using-dependency-
injection-in-android-part-2-b55857911bcd
 https://medium.com/@harivigneshjayapalan/dagger-2-for-android-
beginners-introduction-be6580cb3edb
 https://www.youtube.com/watch?v=ZZ_qek0hGkM
 https://medium.com/dvt-engineering/android-dagger-2-isnt-hard-
97671ac15b07
DAGGERING
NEVER STOP
I HAD TO ADD THIS

More Related Content

What's hot

ChromeとAndroidの過去・現在・未来
ChromeとAndroidの過去・現在・未来ChromeとAndroidの過去・現在・未来
ChromeとAndroidの過去・現在・未来
Shinobu Okano
 
Android testing part i
Android testing part iAndroid testing part i
Android testing part i
Kan-Han (John) Lu
 
C# Cookware - presented at CukeUp! 2014
C# Cookware - presented at CukeUp! 2014C# Cookware - presented at CukeUp! 2014
C# Cookware - presented at CukeUp! 2014
Manuel Pais
 
Let's Explore C# 6
Let's Explore C# 6Let's Explore C# 6
Let's Explore C# 6
Jaliya Udagedara
 
Debugging C# Applications
Debugging C# ApplicationsDebugging C# Applications
Debugging C# Applications
Jaliya Udagedara
 
Phonegap presentation
Phonegap presentationPhonegap presentation
Phonegap presentation
Atilla Özgür
 
Common Java problems when developing with Android
Common Java problems when developing with AndroidCommon Java problems when developing with Android
Common Java problems when developing with Android
Stephen Gilmore
 
12092019 JavaZone "The Battle of the IDEs"
12092019 JavaZone "The Battle of the IDEs"12092019 JavaZone "The Battle of the IDEs"
12092019 JavaZone "The Battle of the IDEs"
Ko Turk
 
Getting Started with IntelliJ IDEA as an Eclipse User
Getting Started with IntelliJ IDEA as an Eclipse UserGetting Started with IntelliJ IDEA as an Eclipse User
Getting Started with IntelliJ IDEA as an Eclipse User
ZeroTurnaround
 
Moved to https://slidr.io/azzazzel/what-s-not-new-in-modular-java
Moved to https://slidr.io/azzazzel/what-s-not-new-in-modular-javaMoved to https://slidr.io/azzazzel/what-s-not-new-in-modular-java
Moved to https://slidr.io/azzazzel/what-s-not-new-in-modular-java
Milen Dyankov
 
Flutter vs react native head to toe comparison [2021 edition]
Flutter vs react native  head to toe comparison [2021 edition]Flutter vs react native  head to toe comparison [2021 edition]
Flutter vs react native head to toe comparison [2021 edition]
Katy Slemon
 
Fight your technical debt with Jenkins, Jacoco and Sonar
Fight your technical debt with Jenkins, Jacoco and SonarFight your technical debt with Jenkins, Jacoco and Sonar
Fight your technical debt with Jenkins, Jacoco and Sonar
Mickael Istria
 
Current Testing Challenges Ireland
Current Testing Challenges IrelandCurrent Testing Challenges Ireland
Current Testing Challenges IrelandDavid O'Dowd
 
Android Study Jam - Info Session
Android Study Jam - Info SessionAndroid Study Jam - Info Session
Android Study Jam - Info Session
AITIKDANDAPAT
 
Moved to https://slidr.io/azzazzel/osgi-fundamentals
Moved to https://slidr.io/azzazzel/osgi-fundamentalsMoved to https://slidr.io/azzazzel/osgi-fundamentals
Moved to https://slidr.io/azzazzel/osgi-fundamentals
Milen Dyankov
 
Android reverse engineering: understanding third-party applications. OWASP EU...
Android reverse engineering: understanding third-party applications. OWASP EU...Android reverse engineering: understanding third-party applications. OWASP EU...
Android reverse engineering: understanding third-party applications. OWASP EU...
Internet Security Auditors
 
Lecture 19
Lecture 19Lecture 19
Lecture 19
Debasish Pratihari
 
Mobile development with React Native — one year in production
Mobile development with React Native — one year in productionMobile development with React Native — one year in production
Mobile development with React Native — one year in production
Avivi Academy
 
Getting started with react basics
Getting started with react basicsGetting started with react basics
Getting started with react basics
Knoldus Inc.
 

What's hot (20)

ChromeとAndroidの過去・現在・未来
ChromeとAndroidの過去・現在・未来ChromeとAndroidの過去・現在・未来
ChromeとAndroidの過去・現在・未来
 
Android testing part i
Android testing part iAndroid testing part i
Android testing part i
 
C# Cookware - presented at CukeUp! 2014
C# Cookware - presented at CukeUp! 2014C# Cookware - presented at CukeUp! 2014
C# Cookware - presented at CukeUp! 2014
 
Let's Explore C# 6
Let's Explore C# 6Let's Explore C# 6
Let's Explore C# 6
 
Debugging C# Applications
Debugging C# ApplicationsDebugging C# Applications
Debugging C# Applications
 
Phonegap presentation
Phonegap presentationPhonegap presentation
Phonegap presentation
 
Common Java problems when developing with Android
Common Java problems when developing with AndroidCommon Java problems when developing with Android
Common Java problems when developing with Android
 
12092019 JavaZone "The Battle of the IDEs"
12092019 JavaZone "The Battle of the IDEs"12092019 JavaZone "The Battle of the IDEs"
12092019 JavaZone "The Battle of the IDEs"
 
Getting Started with IntelliJ IDEA as an Eclipse User
Getting Started with IntelliJ IDEA as an Eclipse UserGetting Started with IntelliJ IDEA as an Eclipse User
Getting Started with IntelliJ IDEA as an Eclipse User
 
Moved to https://slidr.io/azzazzel/what-s-not-new-in-modular-java
Moved to https://slidr.io/azzazzel/what-s-not-new-in-modular-javaMoved to https://slidr.io/azzazzel/what-s-not-new-in-modular-java
Moved to https://slidr.io/azzazzel/what-s-not-new-in-modular-java
 
Flutter vs react native head to toe comparison [2021 edition]
Flutter vs react native  head to toe comparison [2021 edition]Flutter vs react native  head to toe comparison [2021 edition]
Flutter vs react native head to toe comparison [2021 edition]
 
Ja coco ignite
Ja coco igniteJa coco ignite
Ja coco ignite
 
Fight your technical debt with Jenkins, Jacoco and Sonar
Fight your technical debt with Jenkins, Jacoco and SonarFight your technical debt with Jenkins, Jacoco and Sonar
Fight your technical debt with Jenkins, Jacoco and Sonar
 
Current Testing Challenges Ireland
Current Testing Challenges IrelandCurrent Testing Challenges Ireland
Current Testing Challenges Ireland
 
Android Study Jam - Info Session
Android Study Jam - Info SessionAndroid Study Jam - Info Session
Android Study Jam - Info Session
 
Moved to https://slidr.io/azzazzel/osgi-fundamentals
Moved to https://slidr.io/azzazzel/osgi-fundamentalsMoved to https://slidr.io/azzazzel/osgi-fundamentals
Moved to https://slidr.io/azzazzel/osgi-fundamentals
 
Android reverse engineering: understanding third-party applications. OWASP EU...
Android reverse engineering: understanding third-party applications. OWASP EU...Android reverse engineering: understanding third-party applications. OWASP EU...
Android reverse engineering: understanding third-party applications. OWASP EU...
 
Lecture 19
Lecture 19Lecture 19
Lecture 19
 
Mobile development with React Native — one year in production
Mobile development with React Native — one year in productionMobile development with React Native — one year in production
Mobile development with React Native — one year in production
 
Getting started with react basics
Getting started with react basicsGetting started with react basics
Getting started with react basics
 

Similar to Di with dagger2 in android

Ідіоматична ін'єкція залежностей на Kotlin без фреймворків - UA Mobile2019
Ідіоматична ін'єкція залежностей на Kotlin без фреймворків - UA Mobile2019Ідіоматична ін'єкція залежностей на Kotlin без фреймворків - UA Mobile2019
Ідіоматична ін'єкція залежностей на Kotlin без фреймворків - UA Mobile2019
UA Mobile
 
Dagger 2 - Ciklum Speakers' Corner
Dagger 2 - Ciklum Speakers' CornerDagger 2 - Ciklum Speakers' Corner
Dagger 2 - Ciklum Speakers' Corner
Constantine Mars
 
Android Test Driven Development
Android Test Driven DevelopmentAndroid Test Driven Development
Android Test Driven Development
Arif Huda
 
Dagger2 Intro
Dagger2 IntroDagger2 Intro
Dagger2 Intro
Young-Hyuk Yoo
 
What is the best approach to tdd
What is the best approach to tddWhat is the best approach to tdd
What is the best approach to tdd
Red Hat
 
Android MVVM TDD
Android MVVM TDDAndroid MVVM TDD
Android MVVM TDD
KyungHo Jung
 
King Tut Architecture
King Tut ArchitectureKing Tut Architecture
King Tut Architecture
Gary Pedretti
 
OpenDaylight Developer Experience 2.0
 OpenDaylight Developer Experience 2.0 OpenDaylight Developer Experience 2.0
OpenDaylight Developer Experience 2.0
Michael Vorburger
 
The Power of Dependency Injection with Dagger 2 and Kotlin
The Power of Dependency Injection with Dagger 2 and KotlinThe Power of Dependency Injection with Dagger 2 and Kotlin
The Power of Dependency Injection with Dagger 2 and Kotlin
Knoldus Inc.
 
Angular TS(typescript)
Angular TS(typescript)Angular TS(typescript)
Angular TS(typescript)
Ivan Stepić
 
Hierarchy Viewer Internals
Hierarchy Viewer InternalsHierarchy Viewer Internals
Hierarchy Viewer Internals
Kyungmin Lee
 
DI with Dagger2
DI with Dagger2DI with Dagger2
DI with Dagger2
Eugen Martynov
 
Angular from Zero to Mastery - Training (Intermediate)
Angular from Zero to Mastery - Training (Intermediate)Angular from Zero to Mastery - Training (Intermediate)
Angular from Zero to Mastery - Training (Intermediate)
Smail LOUNES
 
[Android] DI in multimodule application
[Android] DI in multimodule application[Android] DI in multimodule application
[Android] DI in multimodule application
Oleg Mazhukin
 
Play with Testing on Android - Gilang Ramadhan (Academy Content Writer at Dic...
Play with Testing on Android - Gilang Ramadhan (Academy Content Writer at Dic...Play with Testing on Android - Gilang Ramadhan (Academy Content Writer at Dic...
Play with Testing on Android - Gilang Ramadhan (Academy Content Writer at Dic...
DicodingEvent
 
Angular2 + Ng-Lightning + Lightning Design System = Great Apps
Angular2 + Ng-Lightning + Lightning Design System = Great AppsAngular2 + Ng-Lightning + Lightning Design System = Great Apps
Angular2 + Ng-Lightning + Lightning Design System = Great Apps
Emily Hurn
 
Curious Coders Java Web Frameworks Comparison
Curious Coders Java Web Frameworks ComparisonCurious Coders Java Web Frameworks Comparison
Curious Coders Java Web Frameworks ComparisonHamed Hatami
 
Ng talk
Ng talkNg talk
Angular Js
Angular JsAngular Js
Angular Js
Knoldus Inc.
 

Similar to Di with dagger2 in android (20)

Ідіоматична ін'єкція залежностей на Kotlin без фреймворків - UA Mobile2019
Ідіоматична ін'єкція залежностей на Kotlin без фреймворків - UA Mobile2019Ідіоматична ін'єкція залежностей на Kotlin без фреймворків - UA Mobile2019
Ідіоматична ін'єкція залежностей на Kotlin без фреймворків - UA Mobile2019
 
Dagger 2 - Ciklum Speakers' Corner
Dagger 2 - Ciklum Speakers' CornerDagger 2 - Ciklum Speakers' Corner
Dagger 2 - Ciklum Speakers' Corner
 
Android Test Driven Development
Android Test Driven DevelopmentAndroid Test Driven Development
Android Test Driven Development
 
Dagger2 Intro
Dagger2 IntroDagger2 Intro
Dagger2 Intro
 
What is the best approach to tdd
What is the best approach to tddWhat is the best approach to tdd
What is the best approach to tdd
 
Android MVVM TDD
Android MVVM TDDAndroid MVVM TDD
Android MVVM TDD
 
King Tut Architecture
King Tut ArchitectureKing Tut Architecture
King Tut Architecture
 
OpenDaylight Developer Experience 2.0
 OpenDaylight Developer Experience 2.0 OpenDaylight Developer Experience 2.0
OpenDaylight Developer Experience 2.0
 
The Power of Dependency Injection with Dagger 2 and Kotlin
The Power of Dependency Injection with Dagger 2 and KotlinThe Power of Dependency Injection with Dagger 2 and Kotlin
The Power of Dependency Injection with Dagger 2 and Kotlin
 
Angular TS(typescript)
Angular TS(typescript)Angular TS(typescript)
Angular TS(typescript)
 
Hierarchy Viewer Internals
Hierarchy Viewer InternalsHierarchy Viewer Internals
Hierarchy Viewer Internals
 
DI with Dagger2
DI with Dagger2DI with Dagger2
DI with Dagger2
 
Angular from Zero to Mastery - Training (Intermediate)
Angular from Zero to Mastery - Training (Intermediate)Angular from Zero to Mastery - Training (Intermediate)
Angular from Zero to Mastery - Training (Intermediate)
 
[Android] DI in multimodule application
[Android] DI in multimodule application[Android] DI in multimodule application
[Android] DI in multimodule application
 
Play with Testing on Android - Gilang Ramadhan (Academy Content Writer at Dic...
Play with Testing on Android - Gilang Ramadhan (Academy Content Writer at Dic...Play with Testing on Android - Gilang Ramadhan (Academy Content Writer at Dic...
Play with Testing on Android - Gilang Ramadhan (Academy Content Writer at Dic...
 
Angular2 + Ng-Lightning + Lightning Design System = Great Apps
Angular2 + Ng-Lightning + Lightning Design System = Great AppsAngular2 + Ng-Lightning + Lightning Design System = Great Apps
Angular2 + Ng-Lightning + Lightning Design System = Great Apps
 
Curious Coders Java Web Frameworks Comparison
Curious Coders Java Web Frameworks ComparisonCurious Coders Java Web Frameworks Comparison
Curious Coders Java Web Frameworks Comparison
 
Dependency injection
Dependency injectionDependency injection
Dependency injection
 
Ng talk
Ng talkNg talk
Ng talk
 
Angular Js
Angular JsAngular Js
Angular Js
 

Di with dagger2 in android

  • 1. DAGGER SO SHARP! DEPENDENCY INJECTION IN ANDROID
  • 2. TOPICS  What is Dependency Injection  What is Dagger  Pros and Cons of Dagger  Dagger alternatives  Sample Implementation  QA session
  • 3. WHAT IS DEPENDENCY INJECTION?  Dependency Injection (DI) is a design pattern used to implement Inversion of Control(IoC). It allows the creation of dependent objects outside of a class and provides those objects to a class through different ways. Using DI, we move the creation and binding of the dependent objects outside of the class that depends on them.  It's a design pattern(No need for a library to implement)  We all use some form of DI Car Engine Dependency Depends
  • 5. HARD COUPLED EXAMPLE  BadCar is tightly coupled to the fate of Engine and Wheels
  • 6. BENEFITS OF DI LOOSE COUPLING EASY TO TEST CODE REUSABILITY
  • 8.
  • 9. POPULAR LIBRARIES  Dagger2  Koin  Kodein  Roboguice  Butterknife  Android annotation  Toothpick
  • 10. WEAPONS, MONEY AND DRUGS A SIDE BY SIDE COMPARISON OF DAGGER2, KOIN AND KODEIN
  • 11. COMPARISON BETWEEN DAGGER2, KOIN & KODEIN Dagger 2 Koin Kodein Written in Java Written in kotlin Written in Kotlin(Java support) Generates code in build does not generate code(less build time) No code generation Has proxy classes No proxy classes No proxy classes Uses annotations No annotation No annotation A lot of boilerplate Less boilerplate
  • 13. DAGGER2  Uses Directed Acyclic Graph = DAGger  There are no cyclic dependencies  @Module and @Provides: define classes and methods which provide dependencies  @Inject: request dependencies. Can be used on a constructor, a field, or a method  @Component: enable selected modules and used for performing dependency injection
  • 14. LET'S ROLL UP OUR SLEEVES AND WRITE SOME CODE!
  • 15. BENEFITS OF DAGGER2  Provides compile-time verification of the whole dependency graph so app doesn't crash at runtime  Very clear error messages  Designed for low-end devices
  • 16. CONS OF DAGGER2  Too verbose  Steep learning curve
  • 18. THANK YOU THE END. THAT'S IT.
  • 19. REFERENCES & FURTHER READING  https://codinginflow.com/tutorials/android/dagger-2/part-1-introduction  https://www.tutorialsteacher.com/ioc/dependency-injection  https://android.jlelse.eu/fundamentals-of-dependency-injection-and- popular-libraries-in-android-c17cf48b5253  https://medium.com/@egek92/koin-di-comparison-of-android-dependency- injection-frameworks-236a6909bc3e  https://dagger.dev/users-guide  https://medium.com/@charbgr/bye-bye-dagger-1494118dcd41  https://www.journaldev.com/20654/android-mvp-dagger2-retrofit-rxjava  https://android.jlelse.eu/7-steps-to-implement-dagger-2-in-android- dabc16715a3a  https://www.vogella.com/tutorials/Dagger/article.html  https://blog.mindorks.com/introduction-to-dagger-2-using-dependency- injection-in-android-part-2-b55857911bcd  https://medium.com/@harivigneshjayapalan/dagger-2-for-android- beginners-introduction-be6580cb3edb  https://www.youtube.com/watch?v=ZZ_qek0hGkM  https://medium.com/dvt-engineering/android-dagger-2-isnt-hard- 97671ac15b07