SlideShare a Scribd company logo
©W3Engineers2018
Android-DI
Dagger2
Md. Moniruzzaman (Monir)
Sr. Software Engineer
Sk Arman
Junior Software Engineer
13-09-2018
Android-dagger2
Monir & Arman
What is DI
Dependency Injection in build upon the concept of Inversion of Control. Which
says that a class should get its dependencies from outside. In simple words, no
class should instantiate another class but should get the instances from a
configuration class.
Android-dagger2
Monir & Arman
Why do we need Dependency Injection?
If a java class creates an instance of another class via the new operator, then it
cannot be used and tested independently from that class and is called a hard
dependency.
Android-dagger2
Monir & Arman
Benefits
So, what are the benefits of providing the dependencies from outside the class?
● The most important advantage is that it increases the possibility of reusing the
class and to be able to test them independent of other classes.
● This sounds awesome to create a class that is not a specific implementation
of a business logic.
Android-dagger2
Monir & Arman
How do we do DI
To answer this question, we have to look back into the past:
A framework class called dependency container was used to analyzes the
dependencies of a class. With this analysis, it was able to create an instance of
the class and inject the objects into the defined dependencies via Java
Reflections. This eliminated the hard dependencies. That way the class could
be tested in isolation, ex. by using mock objects. This was Dagger1.
Android-dagger2
Monir & Arman
Disadvantages of past process
Main disadvantages of this process were two folds. First, the Reflection is slow
in itself and second, it used to perform dependency resolution at runtime,
leading to unexpected crashes.
This lead to the birth of Dagger2 forked from Square Dagger1 by Google.
Android-dagger2
Monir & Arman
What is Dagger2
Dagger is a fully static, compile-time dependency injection framework for
both Java and Android. It is an adaptation of an earlier version created by Square
and now maintained by Google.— Dagger Team
Android-dagger2
Monir & Arman
What new in dagger2?
● The big changes that were brought in Dagger2 were generating the
dependency graph using annotation processor. The classes providing the
dependencies were now generated at build time using javax inject package.
This facilitated the possible error checking before the application runs. The
generated code is highly readable as if written by hand.
Note: Annotation Processor is a way to read the compiled files during build
time to generate source code files to be used in the project.
Android-dagger2
Monir & Arman
Mode of Injection
The standard java annotations for describing the dependencies of a class are
defined in the Java Specification Request 330 (JSR 330)
● Constructor Injection: Injecting the method parameters.
● Field Injection: Injecting the member variable (must not be private).
● Method Injection: Injecting the method parameter.
Android-dagger2
Monir & Arman
Order of dependency injection
Order of dependency injection according to JSR330:
1. Constructor
2. Field
3. Method
Android-dagger2
Monir & Arman
Important things to remember
● The order in which the methods or fields annotated with @Inject are called is
not defined by JSR330. You cannot assume that the methods or fields are
called in the order of their declaration in the class.
● As fields and method parameters are injected after the constructor is called,
you cannot use injected member variables in the constructor.
Android-dagger2
Monir & Arman
Now proceed to understand Dagger2
A dependency consumer asks for the dependency(Object) from a dependency
provider through a connector.
 Dependency consumer: The @Inject annotation is used to define a
dependency.
.
Android-dagger2
Monir & Arman
Example of consumer
@Singleton
public class DataManager {
private Context mContext;
private DbHelper mDbHelper;
private SharedPrefsHelper mSharedPrefsHelper;
@Inject public DataManager(@ApplicationContext Context context,
DbHelper dbHelper, SharedPrefsHelper
sharedPrefsHelper) {
mContext = context;
mDbHelper = dbHelper;
mSharedPrefsHelper = sharedPrefsHelper;
}
public void saveAccessToken(String accessToken) {
mSharedPrefsHelper.put(SharedPrefsHelper.PREF_KEY_ACCESS_TOKEN, accessToken);
}
public Long createUser(User user) throws Exception {
return mDbHelper.insertUser(user);
}}
Android-dagger2
Monir & Arman
Now proceed to understand Dagger2
 Dependency provider: Classes annotated with @Module are responsible for
providing objects which can be injected. Such classes define methods
annotated with @Provides. The returned objects from these methods are
available for dependency injection.
Android-dagger2
Monir & Arman
Example of Provider
@Module
public class ActivityModule {
private Activity mActivity;
public ActivityModule(Activity activity) {
mActivity = activity;
}
@Provides
@ActivityContext
Context provideContext() {
return mActivity;
}
@Provides
Activity provideActivity() {
return mActivity;
}
}
Android-dagger2
Monir & Arman
Now proceed to understand Dagger2
Connecting consumer and producer
(Connector): A @Component annotated interface defines the connection
between the provider of objects (modules) and the objects which express a
dependency. The class for this connection is generated by the Dagger.
Android-dagger2
Monir & Arman
Example of Connector
@Singleton
@Component(modules = ActivityModule.class)
public interface ActivityComponent {
void inject(DemoApplication demoApplication);
@ApplicationContext
Context getContext();
Application getApplication();
DataManager getDataManager();
SharedPrefsHelper getPreferenceHelper();
DbHelper getDbHelper();
}
Android-dagger2
Monir & Arman
Limitations of Dagger2
● Dagger2 does not inject fields automatically.
● It cannot inject private fields.
● If you want to use field injection you have to define a method in
your @Component annotated interface which takes the instance of the class
into which you want to inject the member variable.
Android-dagger2
Monir & Arman
References
● https://blog.mindorks.com/introduction-to-dagger-2-using-dependency-
injection-in-android-part-1-223289c2a01b
● https://medium.com/@harivigneshjayapalan/dagger-2-for-android-beginners-
di-part-i-f5cc4e5ad878
● https://android.jlelse.eu/the-simplest-dagger2-dependency-injection-sample-
80a0eb60e33b
● https://www.simplifiedcoding.net/dagger-2-android-example/
Android-dagger2
Monir & Arman
Thanks!

More Related Content

PPTX
SQLite database in android
PDF
Accessing Hardware on Android
PPTX
Software testing life cycle
PDF
Inheritance In Java
PPTX
Event Handling in java
PPT
Introduction to method overloading & method overriding in java hdm
PPTX
Constructor and destructor
PDF
The aggregate function - from sequential and parallel folds to parallel aggre...
SQLite database in android
Accessing Hardware on Android
Software testing life cycle
Inheritance In Java
Event Handling in java
Introduction to method overloading & method overriding in java hdm
Constructor and destructor
The aggregate function - from sequential and parallel folds to parallel aggre...

What's hot (20)

ODP
Présentation de Robot framework
PPTX
Java awt (abstract window toolkit)
PPTX
Getting started with Octopus Deploy
PDF
Java Applet and Graphics
PPTX
UI Programming with Qt-Quick and QML
PDF
Java Concurrency - Quiz Questions
PPT
Standard Library Functions
PPT
16 virtual function
PPTX
Java loops for, while and do...while
PDF
Pipeline oriented programming
PPTX
Php cookies
PDF
Object-oriented Programming-with C#
PPTX
Introduction to Kotlin Language and its application to Android platform
PPT
A journey beyond the page object pattern
PPTX
Effective Software Test Case Design Approach
PPTX
QSpiders - Jdk Jvm Jre and Jit
PPTX
constructors in java ppt
PPTX
Python Seminar PPT
Présentation de Robot framework
Java awt (abstract window toolkit)
Getting started with Octopus Deploy
Java Applet and Graphics
UI Programming with Qt-Quick and QML
Java Concurrency - Quiz Questions
Standard Library Functions
16 virtual function
Java loops for, while and do...while
Pipeline oriented programming
Php cookies
Object-oriented Programming-with C#
Introduction to Kotlin Language and its application to Android platform
A journey beyond the page object pattern
Effective Software Test Case Design Approach
QSpiders - Jdk Jvm Jre and Jit
constructors in java ppt
Python Seminar PPT
Ad

Similar to Android Dagger2 (20)

PDF
Dagger2 Intro
PPTX
Android Dagger 2
PPTX
The Power of Dependency Injection with Dagger 2 and Kotlin
PDF
Dependency injection and dagger2 in android paramvir singh
PPTX
Di with dagger2 in android
PPTX
Introduction to Dagger 2 by Ratanak
PPTX
Dependency injection using dagger 2
PDF
Dagger 2 - Ciklum Speakers' Corner
PDF
Getting started with dagger 2
PDF
Android talks #08 dagger2
PPTX
Introduction to Depedency Injection in Android
PDF
DI with Dagger2
PPTX
How To Dependency Inject a Kitten: An Introduction to Dagger 2
PDF
Dependency injection with dagger 2
PPTX
Dependency Injection in Android with Dagger 2
PDF
Dagger for android
PPTX
Dagger 2
PPTX
Dagger 2 ppt
PDF
DI using dagger
PPTX
Android architecture
Dagger2 Intro
Android Dagger 2
The Power of Dependency Injection with Dagger 2 and Kotlin
Dependency injection and dagger2 in android paramvir singh
Di with dagger2 in android
Introduction to Dagger 2 by Ratanak
Dependency injection using dagger 2
Dagger 2 - Ciklum Speakers' Corner
Getting started with dagger 2
Android talks #08 dagger2
Introduction to Depedency Injection in Android
DI with Dagger2
How To Dependency Inject a Kitten: An Introduction to Dagger 2
Dependency injection with dagger 2
Dependency Injection in Android with Dagger 2
Dagger for android
Dagger 2
Dagger 2 ppt
DI using dagger
Android architecture
Ad

Recently uploaded (20)

PPTX
A Presentation on Artificial Intelligence
PDF
Agricultural_Statistics_at_a_Glance_2022_0.pdf
PDF
Assigned Numbers - 2025 - Bluetooth® Document
PDF
Review of recent advances in non-invasive hemoglobin estimation
PPTX
Big Data Technologies - Introduction.pptx
PDF
Empathic Computing: Creating Shared Understanding
PDF
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
PDF
Building Integrated photovoltaic BIPV_UPV.pdf
PDF
Network Security Unit 5.pdf for BCA BBA.
PPTX
20250228 LYD VKU AI Blended-Learning.pptx
PPTX
Digital-Transformation-Roadmap-for-Companies.pptx
PDF
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
PPTX
Programs and apps: productivity, graphics, security and other tools
PDF
The Rise and Fall of 3GPP – Time for a Sabbatical?
PPT
Teaching material agriculture food technology
PPTX
Machine Learning_overview_presentation.pptx
PDF
MIND Revenue Release Quarter 2 2025 Press Release
PPT
“AI and Expert System Decision Support & Business Intelligence Systems”
PDF
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
PDF
Chapter 3 Spatial Domain Image Processing.pdf
A Presentation on Artificial Intelligence
Agricultural_Statistics_at_a_Glance_2022_0.pdf
Assigned Numbers - 2025 - Bluetooth® Document
Review of recent advances in non-invasive hemoglobin estimation
Big Data Technologies - Introduction.pptx
Empathic Computing: Creating Shared Understanding
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
Building Integrated photovoltaic BIPV_UPV.pdf
Network Security Unit 5.pdf for BCA BBA.
20250228 LYD VKU AI Blended-Learning.pptx
Digital-Transformation-Roadmap-for-Companies.pptx
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
Programs and apps: productivity, graphics, security and other tools
The Rise and Fall of 3GPP – Time for a Sabbatical?
Teaching material agriculture food technology
Machine Learning_overview_presentation.pptx
MIND Revenue Release Quarter 2 2025 Press Release
“AI and Expert System Decision Support & Business Intelligence Systems”
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
Chapter 3 Spatial Domain Image Processing.pdf

Android Dagger2

  • 1. ©W3Engineers2018 Android-DI Dagger2 Md. Moniruzzaman (Monir) Sr. Software Engineer Sk Arman Junior Software Engineer 13-09-2018
  • 2. Android-dagger2 Monir & Arman What is DI Dependency Injection in build upon the concept of Inversion of Control. Which says that a class should get its dependencies from outside. In simple words, no class should instantiate another class but should get the instances from a configuration class.
  • 3. Android-dagger2 Monir & Arman Why do we need Dependency Injection? If a java class creates an instance of another class via the new operator, then it cannot be used and tested independently from that class and is called a hard dependency.
  • 4. Android-dagger2 Monir & Arman Benefits So, what are the benefits of providing the dependencies from outside the class? ● The most important advantage is that it increases the possibility of reusing the class and to be able to test them independent of other classes. ● This sounds awesome to create a class that is not a specific implementation of a business logic.
  • 5. Android-dagger2 Monir & Arman How do we do DI To answer this question, we have to look back into the past: A framework class called dependency container was used to analyzes the dependencies of a class. With this analysis, it was able to create an instance of the class and inject the objects into the defined dependencies via Java Reflections. This eliminated the hard dependencies. That way the class could be tested in isolation, ex. by using mock objects. This was Dagger1.
  • 6. Android-dagger2 Monir & Arman Disadvantages of past process Main disadvantages of this process were two folds. First, the Reflection is slow in itself and second, it used to perform dependency resolution at runtime, leading to unexpected crashes. This lead to the birth of Dagger2 forked from Square Dagger1 by Google.
  • 7. Android-dagger2 Monir & Arman What is Dagger2 Dagger is a fully static, compile-time dependency injection framework for both Java and Android. It is an adaptation of an earlier version created by Square and now maintained by Google.— Dagger Team
  • 8. Android-dagger2 Monir & Arman What new in dagger2? ● The big changes that were brought in Dagger2 were generating the dependency graph using annotation processor. The classes providing the dependencies were now generated at build time using javax inject package. This facilitated the possible error checking before the application runs. The generated code is highly readable as if written by hand. Note: Annotation Processor is a way to read the compiled files during build time to generate source code files to be used in the project.
  • 9. Android-dagger2 Monir & Arman Mode of Injection The standard java annotations for describing the dependencies of a class are defined in the Java Specification Request 330 (JSR 330) ● Constructor Injection: Injecting the method parameters. ● Field Injection: Injecting the member variable (must not be private). ● Method Injection: Injecting the method parameter.
  • 10. Android-dagger2 Monir & Arman Order of dependency injection Order of dependency injection according to JSR330: 1. Constructor 2. Field 3. Method
  • 11. Android-dagger2 Monir & Arman Important things to remember ● The order in which the methods or fields annotated with @Inject are called is not defined by JSR330. You cannot assume that the methods or fields are called in the order of their declaration in the class. ● As fields and method parameters are injected after the constructor is called, you cannot use injected member variables in the constructor.
  • 12. Android-dagger2 Monir & Arman Now proceed to understand Dagger2 A dependency consumer asks for the dependency(Object) from a dependency provider through a connector.  Dependency consumer: The @Inject annotation is used to define a dependency. .
  • 13. Android-dagger2 Monir & Arman Example of consumer @Singleton public class DataManager { private Context mContext; private DbHelper mDbHelper; private SharedPrefsHelper mSharedPrefsHelper; @Inject public DataManager(@ApplicationContext Context context, DbHelper dbHelper, SharedPrefsHelper sharedPrefsHelper) { mContext = context; mDbHelper = dbHelper; mSharedPrefsHelper = sharedPrefsHelper; } public void saveAccessToken(String accessToken) { mSharedPrefsHelper.put(SharedPrefsHelper.PREF_KEY_ACCESS_TOKEN, accessToken); } public Long createUser(User user) throws Exception { return mDbHelper.insertUser(user); }}
  • 14. Android-dagger2 Monir & Arman Now proceed to understand Dagger2  Dependency provider: Classes annotated with @Module are responsible for providing objects which can be injected. Such classes define methods annotated with @Provides. The returned objects from these methods are available for dependency injection.
  • 15. Android-dagger2 Monir & Arman Example of Provider @Module public class ActivityModule { private Activity mActivity; public ActivityModule(Activity activity) { mActivity = activity; } @Provides @ActivityContext Context provideContext() { return mActivity; } @Provides Activity provideActivity() { return mActivity; } }
  • 16. Android-dagger2 Monir & Arman Now proceed to understand Dagger2 Connecting consumer and producer (Connector): A @Component annotated interface defines the connection between the provider of objects (modules) and the objects which express a dependency. The class for this connection is generated by the Dagger.
  • 17. Android-dagger2 Monir & Arman Example of Connector @Singleton @Component(modules = ActivityModule.class) public interface ActivityComponent { void inject(DemoApplication demoApplication); @ApplicationContext Context getContext(); Application getApplication(); DataManager getDataManager(); SharedPrefsHelper getPreferenceHelper(); DbHelper getDbHelper(); }
  • 18. Android-dagger2 Monir & Arman Limitations of Dagger2 ● Dagger2 does not inject fields automatically. ● It cannot inject private fields. ● If you want to use field injection you have to define a method in your @Component annotated interface which takes the instance of the class into which you want to inject the member variable.
  • 19. Android-dagger2 Monir & Arman References ● https://blog.mindorks.com/introduction-to-dagger-2-using-dependency- injection-in-android-part-1-223289c2a01b ● https://medium.com/@harivigneshjayapalan/dagger-2-for-android-beginners- di-part-i-f5cc4e5ad878 ● https://android.jlelse.eu/the-simplest-dagger2-dependency-injection-sample- 80a0eb60e33b ● https://www.simplifiedcoding.net/dagger-2-android-example/