SlideShare a Scribd company logo
Dependency Injection with Dagger2
M.Javad Hashemi
Dependency Injection
● A style of creation in which an object are created by an external entity or technique
whereby one object supplies the dependencies of another object.
● IoC ( Inversion of Control)
○ The modules of top levels shouldn’t depend on modules of the lower levels. The modules of all
levels should depend on abstractions.
○ The abstractions shouldn’t depend on details. The details should depend on abstractions.
Dependency Injection
Benefits of DI ?
● Test
● Maintenance
● Reusable code
Dependency Injection
class CoffeeMaker {
private final Heater heater;
private final Pump pump;
public CoffeeMaker() {
heater = new ElectricHeater();
pump = new Thermpsiphon(heater);
}
Coffee makeCaffee(){ /* ... */ }
}
class CoffeeMain {
public static void main(String[] args){
Coffee coffee = new CoffeeMaker().makeCaffee();
}
}
Dependency Injection
class CoffeeMaker {
private final Heater heater;
private final Pump pump;
public CoffeeMaker(Heater heater, Pump pump) {
this.heater = heater;
this.pump = pump;
}
Coffee makeCaffee(){ /* ... */ }
}
class CoffeeMain {
public static void main(String[] args){
Heater heater = new ElectricHeater();
Pump pump = new Thermosiphon(heater)
Coffee coffee = new CoffeeMaker(heater, pump).makeCaffee();
}
}
Dagger2
● Generate all the dependency injection boilerplate codes based on
annotation processing
● Base elements:
○ @Inject
○ @Module/@Provide
○ @Component
○ @Scope
○ @Named/@Qualifier
@Inject
Request dependency
● Constructor Injection
● Field Injection
● Method Injection
the @Inject annotation will tell the "Dagger" which
dependency needed to be passed to the dependant
class.
public class Starks{
/**
* Explaining different usage
* of Inject annotations of dagger
**/
//Feild injection
@Inject
Allies allies;
//Constructor injection
@Inject
public Starks(){
//do something..
}
//Method injection
@Inject
private void prepareForWar(){
//do something..
}
}
COMPILE TIME CHECK !!!
@Module/@Provide
@Module: Classes with methods “provide
dependencies”
@Provide:  methods inside @Module, which
“tell Dagger how we want to build and present
a dependency“
@Module
class DatabaseModule {
@Provides
fun provideRoomDatabase(application: App):
AppDatabase
{
return Room
.databaseBuilder(application,
AppDatabase::class.java,
Constants.DATABASE_NAME)
.build()
}
@Provides
fun provideDebitDao(appDatabase: AppDatabase):
DebitDao
{
return appDatabase.getDebitDao()
}
}
@Component
bridge between @Inject and @Module.
@Component(
modules = [
AndroidSupportInjectionModule::class,
DatabaseModule::class,
]
)
interface AppComponent : AndroidInjector<App> {
@Component.Builder
abstract class Builder :
AndroidInjector.Builder<App>()
}
class App : Application() {
override fun onCreate() {
super.onCreate()
DaggerAppComponent.builder().create(this)
}
}
@Component
@Scope
● When we want to share some
variables across fragments but not
across activities, then we need to
have the variable that lives in activity scope.
● It’s main job is to ensure a variable is only
created once and could be reused within a given
scope.
@Scope
@kotlin.annotation.Retention(AnnotationRetention.RUNTIME)
annotation class FragmentScope {
}
@Scope
@kotlin.annotation.Retention(AnnotationRetention.RUN
TIME)
annotation class ActivityScope {
}
@Named/@Qualifier
● We can’t provide two methods which returned
the same Object
● So, how can do that ?
@Module
class CatModule {
@Provides
fun provideGarfield(): Cat =
Cat("Garfield")
@Provides
fun provideHelloKitty(): Cat =
Cat("Hello Kitty")
}
error: [Dagger/DuplicateBindings]
packagename.something.something.Cat is bound
multiple times:
@Named/@Qualifier
● The @Named annotation is good for identifying
which provider to be used when we are trying
to inject the dependency of the same type.
@Module
class CatModule {
@Provides
@Named("Garfield")
fun provideGarfield(): Cat =
Cat("Garfield")
@Provides
@Named("HelloKitty")
fun provideHelloKitty(): Cat =
Cat("Hello Kitty")
}
class QualifierActivity : AppCompatActivity() {
@Inject
@Named("Garfield")
lateinit var garfield: Cat
@Inject
@Named("HelloKitty")
lateinit var helloKitty: Cat
override fun onCreate(savedInstanceState:
Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_qualifier)
DaggerCatComponent.builder().build().inject(this
)
garfieldTextView.text = "injected:
${garfield.name}"
helloKittyTextView.text = "injected:
${helloKitty.name}"
}
}
@Named/@Qualifier
● The @Qualifier is the same as @Named, but it
is defined with custom annotation
@Module
class CatModule {
@Provides
@NamedClone("Red Apple")
fun provideRedApple(): Apple = Apple("red")
@Provides
@NamedClone("Green Apple")
fun provideGreenApple(): Apple = Apple("green")
}
@Qualifier
@MustBeDocumented
@Retention(AnnotationRetention.RUNTIME)
annotation class NamedClone(val value:
String = "") {
}
@Inject
@field:NamedClone("Red Apple")
lateinit var redApple: Apple
@Inject
@field:NamedClone("Green Apple")
lateinit var greenApple: Apple
Is it the end?
Not Yet !!!
We Implement all dagger parts but
something missing. Did you find it?
Android-Dagger
● Android Framework Components, like activity, fragment,... have no constructor injection
support
● It is done by 4 steps:
Android-Dagger
1- AndroidInjectionModule
An internal class from dagger 2.10 which provides our activities and fragments. That is why we
have used it in Component modules:
@Component(
modules = [
AndroidInjectionModule::class,
DatabaseModule::class,
]
)
interface AppComponent : AndroidInjector<App> {
@Component.Builder
abstract class Builder :
AndroidInjector.Builder<App>()
}
Android-Dagger
2- ActivityBuilder
It’s an module which map all of activities to help dagger knows our activities in compile time.
Let’s imagine we have 2 activities : Main and Detail
@Module
public abstract class ActivityBuilder {
@Binds
@IntoMap
@ActivityKey(MainActivity.class)
abstract AndroidInjector.Factory<? extends Activity> bindMainActivity(MainActivityComponent.Builder builder);
@Binds
@IntoMap
@ActivityKey(DetailActivity.class)
abstract AndroidInjector.Factory<? extends Activity> bindDetailActivity(DetailActivityComponent.Builder builder);
}
Android-Dagger
3- SubComponents
We can think apps in three layers while using Dagger.
● Application Component (Must be one)
● Activity Component
● Fragment Component
So. each Activity (also each Fragment) must have its own component. These are defined as
SubComponents
Android-Dagger
3- SubComponents
We have 2 Activities, so needs to create 2 subcomponents, and each activities have own
specific modules, for example for mainActivity:
@Subcomponent(modules = MainActivityModule.class)
public interface MainActivityComponent extends
AndroidInjector<MainActivity>{
@Subcomponent.Builder
abstract class Builder extends
AndroidInjector.Builder<MainActivity>{}
}
@Module
public class MainActivityModule
{
//write provides methods
}
Android-Dagger
3- SubComponents
next , links subcomponent to the main components through appModule:
@Module(subcomponents = [
MainActivityComponent::class,
DetailActivityComponent::class])
class AppModule {
@Provides
@Singleton
fun provideContext(Application application) : Context = return
application
}
Android-Dagger
4-
DispatchingAndroidInjector<T>
Applications has activities, That is why
we implements app class from
HasActivityInjector
public class App extends Application implements
HasActivityInjector {
@Inject
DispatchingAndroidInjector<Activity>
activityDispatchingAndroidInjector;
@Override
public void onCreate() {
super.onCreate();
//simplified
}
@Override
public DispatchingAndroidInjector<Activity>
activityInjector() {
return activityDispatchingAndroidInjector;
}
}
Android-Dagger
public class DetailActivity extends AppCompatActivity implements
HasSupportFragmentInjector {
@Inject
DispatchingAndroidInjector<Fragment> fragmentDispatchingAndroidInjector;
//simplified
@Override
public AndroidInjector<Fragment> supportFragmentInjector() {
return fragmentDispatchingAndroidInjector;
}
}
4- DispatchingAndroidInjector<T>
● Also for activity class
Android-Dagger
The dagger app graph will be as below:
Android-Dagger
Finish? No!
Because Activity and Fragment should not know about how it is injected. So how do we inject
now?
In Activity:
@Override
protected void onCreate(Bundle savedInstanceState)
{
AndroidInjection.inject(this);
super.onCreate(savedInstanceState);
}
In Fragment:
@Override
public void onAttach(Context context) {
AndroidSupportInjection.inject(this);
super.onAttach(context);
}
Is it too hard??
Let’s Simplified it
Android-Dagger
Let’s look at dagger graph again, looks some classes can be removed.
Android-Dagger
Instead of creating subcomponent, module, and boilerplate codes in each activity just use
@ContributesAndroidInjector
Android-Dagger
@ContributesAndroidInjector:
Dagger Android introduced an annotation which can reduce the Binds, Subcomponent,
ActivityKey, FragmentKey, etc boilerplate
@Module
public abstract class ActivityBuilder {
@ContributesAndroidInjector(modules = MainActivityModule.class)
abstract MainActivity bindMainActivity();
@ContributesAndroidInjector(modules = {DetailActivityModule.class})
abstract DetailActivity bindDetailActivity();
}
Android-Dagger
● Now, do not needs to implements android components classes from anymore
interfaces.
● Just extends them from DaggerApplication, DaggerAppCompatActivity,
DaggerFragment
● Finally, application class will be changed as follow:public class App extends DaggerApplication {
@Override
protected AndroidInjector<? extends AndroidSampleApp> applicationInjector() {
return DaggerAppComponent.builder().create(this);
}
}
Thank you!
Let’s try it in a real
project!

More Related Content

What's hot (20)

PDF
Android application architecture
Romain Rochegude
 
PDF
Using Dagger in a Clean Architecture project
Fabio Collini
 
PPTX
[Android] DI in multimodule application
Oleg Mazhukin
 
PDF
DI with Dagger2
Eugen Martynov
 
PPTX
Angular 1.x vs 2 - In code level
Anuradha Bandara
 
PPTX
React Hooks
Erez Cohen
 
PDF
Building Universal Applications with Angular 2
Minko Gechev
 
ODP
Introduction to Angular 2
Knoldus Inc.
 
ODP
Docker - An Introduction
Knoldus Inc.
 
PDF
Angular 2 Crash Course
Elisha Kramer
 
ODP
Microservices in a netshell
Knoldus Inc.
 
PPTX
Presenter deck icenium hol
Dhananjay Kumar
 
PDF
Angular 2 : le réveil de la force
Nicolas PENNEC
 
PDF
Angular 2 - An Introduction
NexThoughts Technologies
 
PDF
Introduction to Angular 2
Naveen Pete
 
PDF
Java Constructor
Soba Arjun
 
PDF
Angular2 with TypeScript
Rohit Bishnoi
 
ODP
Introduction to ReactJS
Knoldus Inc.
 
PDF
Di code steps
Brian Kiptoo
 
PDF
React native bridge for iOS and android
Katy Slemon
 
Android application architecture
Romain Rochegude
 
Using Dagger in a Clean Architecture project
Fabio Collini
 
[Android] DI in multimodule application
Oleg Mazhukin
 
DI with Dagger2
Eugen Martynov
 
Angular 1.x vs 2 - In code level
Anuradha Bandara
 
React Hooks
Erez Cohen
 
Building Universal Applications with Angular 2
Minko Gechev
 
Introduction to Angular 2
Knoldus Inc.
 
Docker - An Introduction
Knoldus Inc.
 
Angular 2 Crash Course
Elisha Kramer
 
Microservices in a netshell
Knoldus Inc.
 
Presenter deck icenium hol
Dhananjay Kumar
 
Angular 2 : le réveil de la force
Nicolas PENNEC
 
Angular 2 - An Introduction
NexThoughts Technologies
 
Introduction to Angular 2
Naveen Pete
 
Java Constructor
Soba Arjun
 
Angular2 with TypeScript
Rohit Bishnoi
 
Introduction to ReactJS
Knoldus Inc.
 
Di code steps
Brian Kiptoo
 
React native bridge for iOS and android
Katy Slemon
 

Similar to Dependency injection using dagger2 (20)

PDF
It's complicated, but it doesn't have to be: a Dagger journey
Thiago “Fred” Porciúncula
 
PPTX
Sword fighting with Dagger GDG-NYC Jan 2016
Mike Nakhimovich
 
PDF
Dependency injection with dagger 2
Nischal0101
 
PDF
Advanced Dagger talk from 360andev
Mike Nakhimovich
 
PPTX
Dependency injection using dagger 2
Mahmoud El-Naggar
 
PDF
Dagger for android
Kan-Han (John) Lu
 
PDF
Dagger 2, 2 years later
K. Matthew Dupree
 
PPTX
The Power of Dependency Injection with Dagger 2 and Kotlin
Knoldus Inc.
 
PPTX
Android architecture
Trong-An Bui
 
PPTX
Антон Минашкин "Dagger 2. Right way to do Dependency Injections"
Fwdays
 
PPTX
Dagger 2. The Right Way to Dependency Injections
GlobalLogic Ukraine
 
PDF
Dagger 2. Right way to do Dependency Injection
Stfalcon Meetups
 
PDF
Anton Minashkin Dagger 2 light
Michael Pustovit
 
PPTX
Di &amp; dagger
Vitali Pekelis
 
PDF
Dagger1
Ramesh Akula
 
PPTX
Dependency Injection for Android @ Ciklum speakers corner Kiev 29. May 2014
First Tuesday Bergen
 
PPTX
Dependency Injection for Android
First Tuesday Bergen
 
PDF
Dagger 2 - Ciklum Speakers' Corner
Constantine Mars
 
PDF
MCE^3 - Gregory Kick - Dagger 2
PROIDEA
 
PPTX
Di with dagger2 in android
Brian Kiptoo
 
It's complicated, but it doesn't have to be: a Dagger journey
Thiago “Fred” Porciúncula
 
Sword fighting with Dagger GDG-NYC Jan 2016
Mike Nakhimovich
 
Dependency injection with dagger 2
Nischal0101
 
Advanced Dagger talk from 360andev
Mike Nakhimovich
 
Dependency injection using dagger 2
Mahmoud El-Naggar
 
Dagger for android
Kan-Han (John) Lu
 
Dagger 2, 2 years later
K. Matthew Dupree
 
The Power of Dependency Injection with Dagger 2 and Kotlin
Knoldus Inc.
 
Android architecture
Trong-An Bui
 
Антон Минашкин "Dagger 2. Right way to do Dependency Injections"
Fwdays
 
Dagger 2. The Right Way to Dependency Injections
GlobalLogic Ukraine
 
Dagger 2. Right way to do Dependency Injection
Stfalcon Meetups
 
Anton Minashkin Dagger 2 light
Michael Pustovit
 
Di &amp; dagger
Vitali Pekelis
 
Dagger1
Ramesh Akula
 
Dependency Injection for Android @ Ciklum speakers corner Kiev 29. May 2014
First Tuesday Bergen
 
Dependency Injection for Android
First Tuesday Bergen
 
Dagger 2 - Ciklum Speakers' Corner
Constantine Mars
 
MCE^3 - Gregory Kick - Dagger 2
PROIDEA
 
Di with dagger2 in android
Brian Kiptoo
 
Ad

Recently uploaded (20)

PPTX
Introduction to Neural Networks and Perceptron Learning Algorithm.pptx
Kayalvizhi A
 
PDF
International Journal of Information Technology Convergence and services (IJI...
ijitcsjournal4
 
PDF
Ethics and Trustworthy AI in Healthcare – Governing Sensitive Data, Profiling...
AlqualsaDIResearchGr
 
PDF
Reasons for the succes of MENARD PRESSUREMETER.pdf
majdiamz
 
PPTX
Types of Bearing_Specifications_PPT.pptx
PranjulAgrahariAkash
 
PDF
PORTFOLIO Golam Kibria Khan — architect with a passion for thoughtful design...
MasumKhan59
 
PPTX
Mechanical Design of shell and tube heat exchangers as per ASME Sec VIII Divi...
shahveer210504
 
PDF
GTU Civil Engineering All Semester Syllabus.pdf
Vimal Bhojani
 
PDF
Pressure Measurement training for engineers and Technicians
AIESOLUTIONS
 
DOC
MRRS Strength and Durability of Concrete
CivilMythili
 
PPTX
GitOps_Repo_Structure for begeinner(Scaffolindg)
DanialHabibi2
 
PPTX
美国电子版毕业证南卡罗莱纳大学上州分校水印成绩单USC学费发票定做学位证书编号怎么查
Taqyea
 
PPTX
Hashing Introduction , hash functions and techniques
sailajam21
 
DOCX
CS-802 (A) BDH Lab manual IPS Academy Indore
thegodhimself05
 
PDF
Set Relation Function Practice session 24.05.2025.pdf
DrStephenStrange4
 
PPTX
Arduino Based Gas Leakage Detector Project
CircuitDigest
 
PDF
Design Thinking basics for Engineers.pdf
CMR University
 
PPTX
Element 11. ELECTRICITY safety and hazards
merrandomohandas
 
PPTX
Lecture 1 Shell and Tube Heat exchanger-1.pptx
mailforillegalwork
 
PPTX
GitOps_Without_K8s_Training_detailed git repository
DanialHabibi2
 
Introduction to Neural Networks and Perceptron Learning Algorithm.pptx
Kayalvizhi A
 
International Journal of Information Technology Convergence and services (IJI...
ijitcsjournal4
 
Ethics and Trustworthy AI in Healthcare – Governing Sensitive Data, Profiling...
AlqualsaDIResearchGr
 
Reasons for the succes of MENARD PRESSUREMETER.pdf
majdiamz
 
Types of Bearing_Specifications_PPT.pptx
PranjulAgrahariAkash
 
PORTFOLIO Golam Kibria Khan — architect with a passion for thoughtful design...
MasumKhan59
 
Mechanical Design of shell and tube heat exchangers as per ASME Sec VIII Divi...
shahveer210504
 
GTU Civil Engineering All Semester Syllabus.pdf
Vimal Bhojani
 
Pressure Measurement training for engineers and Technicians
AIESOLUTIONS
 
MRRS Strength and Durability of Concrete
CivilMythili
 
GitOps_Repo_Structure for begeinner(Scaffolindg)
DanialHabibi2
 
美国电子版毕业证南卡罗莱纳大学上州分校水印成绩单USC学费发票定做学位证书编号怎么查
Taqyea
 
Hashing Introduction , hash functions and techniques
sailajam21
 
CS-802 (A) BDH Lab manual IPS Academy Indore
thegodhimself05
 
Set Relation Function Practice session 24.05.2025.pdf
DrStephenStrange4
 
Arduino Based Gas Leakage Detector Project
CircuitDigest
 
Design Thinking basics for Engineers.pdf
CMR University
 
Element 11. ELECTRICITY safety and hazards
merrandomohandas
 
Lecture 1 Shell and Tube Heat exchanger-1.pptx
mailforillegalwork
 
GitOps_Without_K8s_Training_detailed git repository
DanialHabibi2
 
Ad

Dependency injection using dagger2

  • 1. Dependency Injection with Dagger2 M.Javad Hashemi
  • 2. Dependency Injection ● A style of creation in which an object are created by an external entity or technique whereby one object supplies the dependencies of another object. ● IoC ( Inversion of Control) ○ The modules of top levels shouldn’t depend on modules of the lower levels. The modules of all levels should depend on abstractions. ○ The abstractions shouldn’t depend on details. The details should depend on abstractions.
  • 3. Dependency Injection Benefits of DI ? ● Test ● Maintenance ● Reusable code
  • 4. Dependency Injection class CoffeeMaker { private final Heater heater; private final Pump pump; public CoffeeMaker() { heater = new ElectricHeater(); pump = new Thermpsiphon(heater); } Coffee makeCaffee(){ /* ... */ } } class CoffeeMain { public static void main(String[] args){ Coffee coffee = new CoffeeMaker().makeCaffee(); } }
  • 5. Dependency Injection class CoffeeMaker { private final Heater heater; private final Pump pump; public CoffeeMaker(Heater heater, Pump pump) { this.heater = heater; this.pump = pump; } Coffee makeCaffee(){ /* ... */ } } class CoffeeMain { public static void main(String[] args){ Heater heater = new ElectricHeater(); Pump pump = new Thermosiphon(heater) Coffee coffee = new CoffeeMaker(heater, pump).makeCaffee(); } }
  • 6. Dagger2 ● Generate all the dependency injection boilerplate codes based on annotation processing ● Base elements: ○ @Inject ○ @Module/@Provide ○ @Component ○ @Scope ○ @Named/@Qualifier
  • 7. @Inject Request dependency ● Constructor Injection ● Field Injection ● Method Injection the @Inject annotation will tell the "Dagger" which dependency needed to be passed to the dependant class. public class Starks{ /** * Explaining different usage * of Inject annotations of dagger **/ //Feild injection @Inject Allies allies; //Constructor injection @Inject public Starks(){ //do something.. } //Method injection @Inject private void prepareForWar(){ //do something.. } } COMPILE TIME CHECK !!!
  • 8. @Module/@Provide @Module: Classes with methods “provide dependencies” @Provide:  methods inside @Module, which “tell Dagger how we want to build and present a dependency“ @Module class DatabaseModule { @Provides fun provideRoomDatabase(application: App): AppDatabase { return Room .databaseBuilder(application, AppDatabase::class.java, Constants.DATABASE_NAME) .build() } @Provides fun provideDebitDao(appDatabase: AppDatabase): DebitDao { return appDatabase.getDebitDao() } }
  • 9. @Component bridge between @Inject and @Module. @Component( modules = [ AndroidSupportInjectionModule::class, DatabaseModule::class, ] ) interface AppComponent : AndroidInjector<App> { @Component.Builder abstract class Builder : AndroidInjector.Builder<App>() } class App : Application() { override fun onCreate() { super.onCreate() DaggerAppComponent.builder().create(this) } }
  • 11. @Scope ● When we want to share some variables across fragments but not across activities, then we need to have the variable that lives in activity scope. ● It’s main job is to ensure a variable is only created once and could be reused within a given scope. @Scope @kotlin.annotation.Retention(AnnotationRetention.RUNTIME) annotation class FragmentScope { } @Scope @kotlin.annotation.Retention(AnnotationRetention.RUN TIME) annotation class ActivityScope { }
  • 12. @Named/@Qualifier ● We can’t provide two methods which returned the same Object ● So, how can do that ? @Module class CatModule { @Provides fun provideGarfield(): Cat = Cat("Garfield") @Provides fun provideHelloKitty(): Cat = Cat("Hello Kitty") } error: [Dagger/DuplicateBindings] packagename.something.something.Cat is bound multiple times:
  • 13. @Named/@Qualifier ● The @Named annotation is good for identifying which provider to be used when we are trying to inject the dependency of the same type. @Module class CatModule { @Provides @Named("Garfield") fun provideGarfield(): Cat = Cat("Garfield") @Provides @Named("HelloKitty") fun provideHelloKitty(): Cat = Cat("Hello Kitty") } class QualifierActivity : AppCompatActivity() { @Inject @Named("Garfield") lateinit var garfield: Cat @Inject @Named("HelloKitty") lateinit var helloKitty: Cat override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_qualifier) DaggerCatComponent.builder().build().inject(this ) garfieldTextView.text = "injected: ${garfield.name}" helloKittyTextView.text = "injected: ${helloKitty.name}" } }
  • 14. @Named/@Qualifier ● The @Qualifier is the same as @Named, but it is defined with custom annotation @Module class CatModule { @Provides @NamedClone("Red Apple") fun provideRedApple(): Apple = Apple("red") @Provides @NamedClone("Green Apple") fun provideGreenApple(): Apple = Apple("green") } @Qualifier @MustBeDocumented @Retention(AnnotationRetention.RUNTIME) annotation class NamedClone(val value: String = "") { } @Inject @field:NamedClone("Red Apple") lateinit var redApple: Apple @Inject @field:NamedClone("Green Apple") lateinit var greenApple: Apple
  • 15. Is it the end? Not Yet !!! We Implement all dagger parts but something missing. Did you find it?
  • 16. Android-Dagger ● Android Framework Components, like activity, fragment,... have no constructor injection support ● It is done by 4 steps:
  • 17. Android-Dagger 1- AndroidInjectionModule An internal class from dagger 2.10 which provides our activities and fragments. That is why we have used it in Component modules: @Component( modules = [ AndroidInjectionModule::class, DatabaseModule::class, ] ) interface AppComponent : AndroidInjector<App> { @Component.Builder abstract class Builder : AndroidInjector.Builder<App>() }
  • 18. Android-Dagger 2- ActivityBuilder It’s an module which map all of activities to help dagger knows our activities in compile time. Let’s imagine we have 2 activities : Main and Detail @Module public abstract class ActivityBuilder { @Binds @IntoMap @ActivityKey(MainActivity.class) abstract AndroidInjector.Factory<? extends Activity> bindMainActivity(MainActivityComponent.Builder builder); @Binds @IntoMap @ActivityKey(DetailActivity.class) abstract AndroidInjector.Factory<? extends Activity> bindDetailActivity(DetailActivityComponent.Builder builder); }
  • 19. Android-Dagger 3- SubComponents We can think apps in three layers while using Dagger. ● Application Component (Must be one) ● Activity Component ● Fragment Component So. each Activity (also each Fragment) must have its own component. These are defined as SubComponents
  • 20. Android-Dagger 3- SubComponents We have 2 Activities, so needs to create 2 subcomponents, and each activities have own specific modules, for example for mainActivity: @Subcomponent(modules = MainActivityModule.class) public interface MainActivityComponent extends AndroidInjector<MainActivity>{ @Subcomponent.Builder abstract class Builder extends AndroidInjector.Builder<MainActivity>{} } @Module public class MainActivityModule { //write provides methods }
  • 21. Android-Dagger 3- SubComponents next , links subcomponent to the main components through appModule: @Module(subcomponents = [ MainActivityComponent::class, DetailActivityComponent::class]) class AppModule { @Provides @Singleton fun provideContext(Application application) : Context = return application }
  • 22. Android-Dagger 4- DispatchingAndroidInjector<T> Applications has activities, That is why we implements app class from HasActivityInjector public class App extends Application implements HasActivityInjector { @Inject DispatchingAndroidInjector<Activity> activityDispatchingAndroidInjector; @Override public void onCreate() { super.onCreate(); //simplified } @Override public DispatchingAndroidInjector<Activity> activityInjector() { return activityDispatchingAndroidInjector; } }
  • 23. Android-Dagger public class DetailActivity extends AppCompatActivity implements HasSupportFragmentInjector { @Inject DispatchingAndroidInjector<Fragment> fragmentDispatchingAndroidInjector; //simplified @Override public AndroidInjector<Fragment> supportFragmentInjector() { return fragmentDispatchingAndroidInjector; } } 4- DispatchingAndroidInjector<T> ● Also for activity class
  • 24. Android-Dagger The dagger app graph will be as below:
  • 25. Android-Dagger Finish? No! Because Activity and Fragment should not know about how it is injected. So how do we inject now? In Activity: @Override protected void onCreate(Bundle savedInstanceState) { AndroidInjection.inject(this); super.onCreate(savedInstanceState); } In Fragment: @Override public void onAttach(Context context) { AndroidSupportInjection.inject(this); super.onAttach(context); }
  • 26. Is it too hard?? Let’s Simplified it
  • 27. Android-Dagger Let’s look at dagger graph again, looks some classes can be removed.
  • 28. Android-Dagger Instead of creating subcomponent, module, and boilerplate codes in each activity just use @ContributesAndroidInjector
  • 29. Android-Dagger @ContributesAndroidInjector: Dagger Android introduced an annotation which can reduce the Binds, Subcomponent, ActivityKey, FragmentKey, etc boilerplate @Module public abstract class ActivityBuilder { @ContributesAndroidInjector(modules = MainActivityModule.class) abstract MainActivity bindMainActivity(); @ContributesAndroidInjector(modules = {DetailActivityModule.class}) abstract DetailActivity bindDetailActivity(); }
  • 30. Android-Dagger ● Now, do not needs to implements android components classes from anymore interfaces. ● Just extends them from DaggerApplication, DaggerAppCompatActivity, DaggerFragment ● Finally, application class will be changed as follow:public class App extends DaggerApplication { @Override protected AndroidInjector<? extends AndroidSampleApp> applicationInjector() { return DaggerAppComponent.builder().create(this); } }
  • 31. Thank you! Let’s try it in a real project!

Editor's Notes

  • #19: This is the test