SlideShare a Scribd company logo
Getting Started with
Dagger 2
Fast dependency injection for Android applications
Why dependency injection?
Separation of concerns
Testability
Every app has some
kind of dependency
injection
Uncle Bob
“Depend upon Abstractions. Do
not depend upon concretions.”
A brief history of time
JSR-330
Guice
Dagger
Dagger 2
Maintained by Java Core Libraries team
at Google
Faster than Guice and Dagger
Easy to use and to debug
Mobile first
Dagger API
@Modules + @Provides
@Inject
@Component
Providing Dependencies
Requesting Dependencies
@Inject annotations required
Constructor, Field & Method
Constructor Injection
@Inject on a single constructor
Parameters are dependencies
They can be stored in private and
final fields
Constructor Injection
Constructor Injection
It’s useless now
Constructor Injection
Dependency Graph
EpisodeRepository DataModule#provideEpisodeRepository
RemoteEpisodeRepository
TraktvApi DataModule#provideTraktvApi
OmdbApi DataModule#provideOmdbApi
Constructor Injection
Field Injection
@Inject on fields for dependencies
Field can not be private or final
Inject happens after object is fully
instantiated
Object is usually responsible for injection
Method Injection
http://google.github.io/dagger/
Components
Bridge between modules and
injections
The injector
Components
Components
this notation is optional
Components
Components
How about some live
coding?
github.com/google/dagger
Questions?
Questions?
github.com/rodrigohenriques
Getting started with dagger 2

More Related Content

What's hot

Android Dagger2
Android Dagger2Android Dagger2
Android Dagger2
Monir Zzaman
 
Android Dagger 2
Android  Dagger 2Android  Dagger 2
Android Dagger 2
Sanket Shah
 
Infinum Android Talks #13 - Developing Android Apps Like Navy Seals by Ivan Kušt
Infinum Android Talks #13 - Developing Android Apps Like Navy Seals by Ivan KuštInfinum Android Talks #13 - Developing Android Apps Like Navy Seals by Ivan Kušt
Infinum Android Talks #13 - Developing Android Apps Like Navy Seals by Ivan Kušt
Infinum
 
Dagger 2 - Ciklum Speakers' Corner
Dagger 2 - Ciklum Speakers' CornerDagger 2 - Ciklum Speakers' Corner
Dagger 2 - Ciklum Speakers' Corner
Constantine Mars
 
Di with dagger2 in android
Di with dagger2 in androidDi with dagger2 in android
Di with dagger2 in android
Brian Kiptoo
 
Dependency injection using dagger2
Dependency injection using dagger2Dependency injection using dagger2
Dependency injection using dagger2
Javad Hashemi
 
Android Modularization
Android ModularizationAndroid Modularization
Android Modularization
Young-Hyuk Yoo
 
Meningkatkan SOC dan Reusabillity Kode dengan Duo DI - Sidiq Permana (CIO Nus...
Meningkatkan SOC dan Reusabillity Kode dengan Duo DI - Sidiq Permana (CIO Nus...Meningkatkan SOC dan Reusabillity Kode dengan Duo DI - Sidiq Permana (CIO Nus...
Meningkatkan SOC dan Reusabillity Kode dengan Duo DI - Sidiq Permana (CIO Nus...
DicodingEvent
 
An Introduction to Dependency Inversion Principle
An Introduction to Dependency Inversion PrincipleAn Introduction to Dependency Inversion Principle
An Introduction to Dependency Inversion Principle
Dunith Dhanushka
 
Dagger2
Dagger2Dagger2
Dagger2
Anjan Debnath
 
Fastlane on Android 介紹
Fastlane on Android 介紹Fastlane on Android 介紹
Fastlane on Android 介紹
Kros Huang
 
Dependency Inversion Principle
Dependency Inversion PrincipleDependency Inversion Principle
Dependency Inversion Principle
Marco Mangia Musardo
 
Creating modern java web applications based on struts2 and angularjs
Creating modern java web applications based on struts2 and angularjsCreating modern java web applications based on struts2 and angularjs
Creating modern java web applications based on struts2 and angularjs
Johannes Geppert
 
Memulai Karir menjadi iOS Developer - Gilang ramadhan (Academy Content Writer...
Memulai Karir menjadi iOS Developer - Gilang ramadhan (Academy Content Writer...Memulai Karir menjadi iOS Developer - Gilang ramadhan (Academy Content Writer...
Memulai Karir menjadi iOS Developer - Gilang ramadhan (Academy Content Writer...
DicodingEvent
 
Angular 2... so can I use it now??
Angular 2... so can I use it now??Angular 2... so can I use it now??
Angular 2... so can I use it now??
Laurent Duveau
 
Dependency Inversion Principle
Dependency Inversion PrincipleDependency Inversion Principle
Dependency Inversion Principle
Shahriar Hyder
 
React Nativeの光と闇
React Nativeの光と闇React Nativeの光と闇
React Nativeの光と闇
Yukiya Nakagawa
 
Adventures with Angular 2
Adventures with Angular 2Adventures with Angular 2
Adventures with Angular 2
Dragos Ionita
 
Angular 2
Angular 2Angular 2
Angular2 - getting-ready
Angular2 - getting-ready Angular2 - getting-ready
Angular2 - getting-ready
Nir Kaufman
 

What's hot (20)

Android Dagger2
Android Dagger2Android Dagger2
Android Dagger2
 
Android Dagger 2
Android  Dagger 2Android  Dagger 2
Android Dagger 2
 
Infinum Android Talks #13 - Developing Android Apps Like Navy Seals by Ivan Kušt
Infinum Android Talks #13 - Developing Android Apps Like Navy Seals by Ivan KuštInfinum Android Talks #13 - Developing Android Apps Like Navy Seals by Ivan Kušt
Infinum Android Talks #13 - Developing Android Apps Like Navy Seals by Ivan Kušt
 
Dagger 2 - Ciklum Speakers' Corner
Dagger 2 - Ciklum Speakers' CornerDagger 2 - Ciklum Speakers' Corner
Dagger 2 - Ciklum Speakers' Corner
 
Di with dagger2 in android
Di with dagger2 in androidDi with dagger2 in android
Di with dagger2 in android
 
Dependency injection using dagger2
Dependency injection using dagger2Dependency injection using dagger2
Dependency injection using dagger2
 
Android Modularization
Android ModularizationAndroid Modularization
Android Modularization
 
Meningkatkan SOC dan Reusabillity Kode dengan Duo DI - Sidiq Permana (CIO Nus...
Meningkatkan SOC dan Reusabillity Kode dengan Duo DI - Sidiq Permana (CIO Nus...Meningkatkan SOC dan Reusabillity Kode dengan Duo DI - Sidiq Permana (CIO Nus...
Meningkatkan SOC dan Reusabillity Kode dengan Duo DI - Sidiq Permana (CIO Nus...
 
An Introduction to Dependency Inversion Principle
An Introduction to Dependency Inversion PrincipleAn Introduction to Dependency Inversion Principle
An Introduction to Dependency Inversion Principle
 
Dagger2
Dagger2Dagger2
Dagger2
 
Fastlane on Android 介紹
Fastlane on Android 介紹Fastlane on Android 介紹
Fastlane on Android 介紹
 
Dependency Inversion Principle
Dependency Inversion PrincipleDependency Inversion Principle
Dependency Inversion Principle
 
Creating modern java web applications based on struts2 and angularjs
Creating modern java web applications based on struts2 and angularjsCreating modern java web applications based on struts2 and angularjs
Creating modern java web applications based on struts2 and angularjs
 
Memulai Karir menjadi iOS Developer - Gilang ramadhan (Academy Content Writer...
Memulai Karir menjadi iOS Developer - Gilang ramadhan (Academy Content Writer...Memulai Karir menjadi iOS Developer - Gilang ramadhan (Academy Content Writer...
Memulai Karir menjadi iOS Developer - Gilang ramadhan (Academy Content Writer...
 
Angular 2... so can I use it now??
Angular 2... so can I use it now??Angular 2... so can I use it now??
Angular 2... so can I use it now??
 
Dependency Inversion Principle
Dependency Inversion PrincipleDependency Inversion Principle
Dependency Inversion Principle
 
React Nativeの光と闇
React Nativeの光と闇React Nativeの光と闇
React Nativeの光と闇
 
Adventures with Angular 2
Adventures with Angular 2Adventures with Angular 2
Adventures with Angular 2
 
Angular 2
Angular 2Angular 2
Angular 2
 
Angular2 - getting-ready
Angular2 - getting-ready Angular2 - getting-ready
Angular2 - getting-ready
 

Similar to Getting started with dagger 2

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.
 
Dagger for android
Dagger for androidDagger for android
Dagger for android
Kan-Han (John) Lu
 
Writing testable Android apps
Writing testable Android appsWriting testable Android apps
Writing testable Android apps
Tomáš Kypta
 
Introduction to Dagger 2 by Ratanak
Introduction to Dagger 2 by RatanakIntroduction to Dagger 2 by Ratanak
Introduction to Dagger 2 by Ratanak
ratanak pek
 
[Android] DI in multimodule application
[Android] DI in multimodule application[Android] DI in multimodule application
[Android] DI in multimodule application
Oleg Mazhukin
 
Introduction to dependency injection in Scala (Play)
Introduction to dependency injection in Scala (Play)Introduction to dependency injection in Scala (Play)
Introduction to dependency injection in Scala (Play)
Knoldus Inc.
 
An Implementation Tour to AngularJS
An Implementation Tour to AngularJSAn Implementation Tour to AngularJS
An Implementation Tour to AngularJS
rahulmonikasharma
 
C# Dependency Injection Tutorial | C# Dependency Injection Example | C# Tutor...
C# Dependency Injection Tutorial | C# Dependency Injection Example | C# Tutor...C# Dependency Injection Tutorial | C# Dependency Injection Example | C# Tutor...
C# Dependency Injection Tutorial | C# Dependency Injection Example | C# Tutor...
Simplilearn
 
Angular2 with type script
Angular2 with type scriptAngular2 with type script
Angular2 with type script
Ravi Mone
 
Ідіоматична ін'єкція залежностей на Kotlin без фреймворків - UA Mobile2019
Ідіоматична ін'єкція залежностей на Kotlin без фреймворків - UA Mobile2019Ідіоматична ін'єкція залежностей на Kotlin без фреймворків - UA Mobile2019
Ідіоматична ін'єкція залежностей на Kotlin без фреймворків - UA Mobile2019
UA Mobile
 
How To Dependency Inject a Kitten: An Introduction to Dagger 2
How To Dependency Inject a Kitten: An Introduction to Dagger 2How To Dependency Inject a Kitten: An Introduction to Dagger 2
How To Dependency Inject a Kitten: An Introduction to Dagger 2
Todd Burgess
 
Dependency injection and dagger2 in android paramvir singh
Dependency injection and dagger2 in android   paramvir singhDependency injection and dagger2 in android   paramvir singh
Dependency injection and dagger2 in android paramvir singh
Paramvir Singh
 
Android architecture
Android architecture Android architecture
Android architecture
Trong-An Bui
 
Introduction to Spring Framework
Introduction to Spring FrameworkIntroduction to Spring Framework
Introduction to Spring Framework
ASG
 
Rapid Prototyping with TurboGears2
Rapid Prototyping with TurboGears2Rapid Prototyping with TurboGears2
Rapid Prototyping with TurboGears2
Alessandro Molina
 
The 2014 Decision Makers Guide to Java Web Frameworks
The 2014 Decision Makers Guide to Java Web FrameworksThe 2014 Decision Makers Guide to Java Web Frameworks
The 2014 Decision Makers Guide to Java Web Frameworks
Kunal Ashar
 
Angular 2: core concepts
Angular 2: core conceptsAngular 2: core concepts
Angular 2: core concepts
Codemotion
 
IRJET- DocLock Application for Secure Document Sharing
IRJET- DocLock Application for Secure Document SharingIRJET- DocLock Application for Secure Document Sharing
IRJET- DocLock Application for Secure Document Sharing
IRJET Journal
 
Cut your Dependencies with Dependency Injection - .NET User Group Osnabrueck
Cut your Dependencies with Dependency Injection - .NET User Group OsnabrueckCut your Dependencies with Dependency Injection - .NET User Group Osnabrueck
Cut your Dependencies with Dependency Injection - .NET User Group Osnabrueck
Theo Jungeblut
 
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
 

Similar to Getting started with dagger 2 (20)

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
 
Dagger for android
Dagger for androidDagger for android
Dagger for android
 
Writing testable Android apps
Writing testable Android appsWriting testable Android apps
Writing testable Android apps
 
Introduction to Dagger 2 by Ratanak
Introduction to Dagger 2 by RatanakIntroduction to Dagger 2 by Ratanak
Introduction to Dagger 2 by Ratanak
 
[Android] DI in multimodule application
[Android] DI in multimodule application[Android] DI in multimodule application
[Android] DI in multimodule application
 
Introduction to dependency injection in Scala (Play)
Introduction to dependency injection in Scala (Play)Introduction to dependency injection in Scala (Play)
Introduction to dependency injection in Scala (Play)
 
An Implementation Tour to AngularJS
An Implementation Tour to AngularJSAn Implementation Tour to AngularJS
An Implementation Tour to AngularJS
 
C# Dependency Injection Tutorial | C# Dependency Injection Example | C# Tutor...
C# Dependency Injection Tutorial | C# Dependency Injection Example | C# Tutor...C# Dependency Injection Tutorial | C# Dependency Injection Example | C# Tutor...
C# Dependency Injection Tutorial | C# Dependency Injection Example | C# Tutor...
 
Angular2 with type script
Angular2 with type scriptAngular2 with type script
Angular2 with type script
 
Ідіоматична ін'єкція залежностей на Kotlin без фреймворків - UA Mobile2019
Ідіоматична ін'єкція залежностей на Kotlin без фреймворків - UA Mobile2019Ідіоматична ін'єкція залежностей на Kotlin без фреймворків - UA Mobile2019
Ідіоматична ін'єкція залежностей на Kotlin без фреймворків - UA Mobile2019
 
How To Dependency Inject a Kitten: An Introduction to Dagger 2
How To Dependency Inject a Kitten: An Introduction to Dagger 2How To Dependency Inject a Kitten: An Introduction to Dagger 2
How To Dependency Inject a Kitten: An Introduction to Dagger 2
 
Dependency injection and dagger2 in android paramvir singh
Dependency injection and dagger2 in android   paramvir singhDependency injection and dagger2 in android   paramvir singh
Dependency injection and dagger2 in android paramvir singh
 
Android architecture
Android architecture Android architecture
Android architecture
 
Introduction to Spring Framework
Introduction to Spring FrameworkIntroduction to Spring Framework
Introduction to Spring Framework
 
Rapid Prototyping with TurboGears2
Rapid Prototyping with TurboGears2Rapid Prototyping with TurboGears2
Rapid Prototyping with TurboGears2
 
The 2014 Decision Makers Guide to Java Web Frameworks
The 2014 Decision Makers Guide to Java Web FrameworksThe 2014 Decision Makers Guide to Java Web Frameworks
The 2014 Decision Makers Guide to Java Web Frameworks
 
Angular 2: core concepts
Angular 2: core conceptsAngular 2: core concepts
Angular 2: core concepts
 
IRJET- DocLock Application for Secure Document Sharing
IRJET- DocLock Application for Secure Document SharingIRJET- DocLock Application for Secure Document Sharing
IRJET- DocLock Application for Secure Document Sharing
 
Cut your Dependencies with Dependency Injection - .NET User Group Osnabrueck
Cut your Dependencies with Dependency Injection - .NET User Group OsnabrueckCut your Dependencies with Dependency Injection - .NET User Group Osnabrueck
Cut your Dependencies with Dependency Injection - .NET User Group Osnabrueck
 
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
 

Getting started with dagger 2