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

PDF
Dependency Injection with Dagger 2 presentation
JT Liew
 
PDF
Swagger / Quick Start Guide
Andrii Gakhov
 
PPTX
Bugzilla
Komal Gandhi
 
PDF
Dagger Hilt Pros and Cons - Android Summit 2020
Vasiliy Zukanov
 
PPTX
Introducing Swagger
Tony Tam
 
PPTX
JVM++: The Graal VM
Martin Toshev
 
PDF
Getting Started with Spring for GraphQL
VMware Tanzu
 
PPTX
Flutter Session GDSC BPIT.pptx
khushbooGupta928250
 
Dependency Injection with Dagger 2 presentation
JT Liew
 
Swagger / Quick Start Guide
Andrii Gakhov
 
Bugzilla
Komal Gandhi
 
Dagger Hilt Pros and Cons - Android Summit 2020
Vasiliy Zukanov
 
Introducing Swagger
Tony Tam
 
JVM++: The Graal VM
Martin Toshev
 
Getting Started with Spring for GraphQL
VMware Tanzu
 
Flutter Session GDSC BPIT.pptx
khushbooGupta928250
 

Similar to Android Dagger2 (20)

PPTX
Android Dagger 2
Sanket Shah
 
PDF
Dagger2 Intro
Young-Hyuk Yoo
 
PDF
Dagger for android
Kan-Han (John) Lu
 
PDF
Getting started with dagger 2
Rodrigo Henriques
 
PPTX
[Android] DI in multimodule application
Oleg Mazhukin
 
PPTX
Dependency injection using dagger2
Javad Hashemi
 
PPTX
Android architecture
Trong-An Bui
 
PDF
Di code steps
Brian Kiptoo
 
PPTX
How To Dependency Inject a Kitten: An Introduction to Dagger 2
Todd Burgess
 
PPTX
The Power of Dependency Injection with Dagger 2 and Kotlin
Knoldus Inc.
 
PPTX
Reason to choose Angular JS for your Web Application
Priyanka Verma
 
PDF
Building android apps with MVP, Dagger, Retrofit, Gson, JSON, Kotlin Data Cl...
Hammad Tariq
 
PPTX
Study Jam Session 2
Boston Android
 
PDF
Dependency injection and dagger2 in android paramvir singh
Paramvir Singh
 
ODP
Introduction to Angular 2
Knoldus Inc.
 
PPTX
JCON 2020: Mobile Java Web Applications with MVC and OpenDDR
Werner Keil
 
PDF
Rapid Prototyping with TurboGears2
Alessandro Molina
 
PDF
Github-Source code management system SRS
Aditya Narayan Swami
 
PPTX
Dagger 2 ppt
Swati Srivastava
 
PPTX
Di with dagger2 in android
Brian Kiptoo
 
Android Dagger 2
Sanket Shah
 
Dagger2 Intro
Young-Hyuk Yoo
 
Dagger for android
Kan-Han (John) Lu
 
Getting started with dagger 2
Rodrigo Henriques
 
[Android] DI in multimodule application
Oleg Mazhukin
 
Dependency injection using dagger2
Javad Hashemi
 
Android architecture
Trong-An Bui
 
Di code steps
Brian Kiptoo
 
How To Dependency Inject a Kitten: An Introduction to Dagger 2
Todd Burgess
 
The Power of Dependency Injection with Dagger 2 and Kotlin
Knoldus Inc.
 
Reason to choose Angular JS for your Web Application
Priyanka Verma
 
Building android apps with MVP, Dagger, Retrofit, Gson, JSON, Kotlin Data Cl...
Hammad Tariq
 
Study Jam Session 2
Boston Android
 
Dependency injection and dagger2 in android paramvir singh
Paramvir Singh
 
Introduction to Angular 2
Knoldus Inc.
 
JCON 2020: Mobile Java Web Applications with MVC and OpenDDR
Werner Keil
 
Rapid Prototyping with TurboGears2
Alessandro Molina
 
Github-Source code management system SRS
Aditya Narayan Swami
 
Dagger 2 ppt
Swati Srivastava
 
Di with dagger2 in android
Brian Kiptoo
 
Ad

Recently uploaded (20)

PDF
MASTERDECK GRAPHSUMMIT SYDNEY (Public).pdf
Neo4j
 
PPTX
Dev Dives: Automate, test, and deploy in one place—with Unified Developer Exp...
AndreeaTom
 
PDF
AI Unleashed - Shaping the Future -Starting Today - AIOUG Yatra 2025 - For Co...
Sandesh Rao
 
PDF
Presentation about Hardware and Software in Computer
snehamodhawadiya
 
PPTX
Agile Chennai 18-19 July 2025 Ideathon | AI Powered Microfinance Literacy Gui...
AgileNetwork
 
PDF
Unlocking the Future- AI Agents Meet Oracle Database 23ai - AIOUG Yatra 2025.pdf
Sandesh Rao
 
PPTX
Applied-Statistics-Mastering-Data-Driven-Decisions.pptx
parmaryashparmaryash
 
PDF
AI-Cloud-Business-Management-Platforms-The-Key-to-Efficiency-Growth.pdf
Artjoker Software Development Company
 
PDF
Google I/O Extended 2025 Baku - all ppts
HusseinMalikMammadli
 
PDF
The Future of Artificial Intelligence (AI)
Mukul
 
PDF
OFFOFFBOX™ – A New Era for African Film | Startup Presentation
ambaicciwalkerbrian
 
PDF
How ETL Control Logic Keeps Your Pipelines Safe and Reliable.pdf
Stryv Solutions Pvt. Ltd.
 
PPTX
New ThousandEyes Product Innovations: Cisco Live June 2025
ThousandEyes
 
PPTX
The Future of AI & Machine Learning.pptx
pritsen4700
 
PDF
Get More from Fiori Automation - What’s New, What Works, and What’s Next.pdf
Precisely
 
PPTX
IT Runs Better with ThousandEyes AI-driven Assurance
ThousandEyes
 
PDF
Peak of Data & AI Encore - Real-Time Insights & Scalable Editing with ArcGIS
Safe Software
 
PDF
Structs to JSON: How Go Powers REST APIs
Emily Achieng
 
PDF
Trying to figure out MCP by actually building an app from scratch with open s...
Julien SIMON
 
PDF
NewMind AI Weekly Chronicles - July'25 - Week IV
NewMind AI
 
MASTERDECK GRAPHSUMMIT SYDNEY (Public).pdf
Neo4j
 
Dev Dives: Automate, test, and deploy in one place—with Unified Developer Exp...
AndreeaTom
 
AI Unleashed - Shaping the Future -Starting Today - AIOUG Yatra 2025 - For Co...
Sandesh Rao
 
Presentation about Hardware and Software in Computer
snehamodhawadiya
 
Agile Chennai 18-19 July 2025 Ideathon | AI Powered Microfinance Literacy Gui...
AgileNetwork
 
Unlocking the Future- AI Agents Meet Oracle Database 23ai - AIOUG Yatra 2025.pdf
Sandesh Rao
 
Applied-Statistics-Mastering-Data-Driven-Decisions.pptx
parmaryashparmaryash
 
AI-Cloud-Business-Management-Platforms-The-Key-to-Efficiency-Growth.pdf
Artjoker Software Development Company
 
Google I/O Extended 2025 Baku - all ppts
HusseinMalikMammadli
 
The Future of Artificial Intelligence (AI)
Mukul
 
OFFOFFBOX™ – A New Era for African Film | Startup Presentation
ambaicciwalkerbrian
 
How ETL Control Logic Keeps Your Pipelines Safe and Reliable.pdf
Stryv Solutions Pvt. Ltd.
 
New ThousandEyes Product Innovations: Cisco Live June 2025
ThousandEyes
 
The Future of AI & Machine Learning.pptx
pritsen4700
 
Get More from Fiori Automation - What’s New, What Works, and What’s Next.pdf
Precisely
 
IT Runs Better with ThousandEyes AI-driven Assurance
ThousandEyes
 
Peak of Data & AI Encore - Real-Time Insights & Scalable Editing with ArcGIS
Safe Software
 
Structs to JSON: How Go Powers REST APIs
Emily Achieng
 
Trying to figure out MCP by actually building an app from scratch with open s...
Julien SIMON
 
NewMind AI Weekly Chronicles - July'25 - Week IV
NewMind AI
 
Ad

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/