SlideShare a Scribd company logo
1 of 45
Download to read offline
Pedro V. Gómez Sánchez - pedro@karumi.com - @pedro_g_s - github.com/pedrovgs
Dependency Injection on Android
Pedro Vicente Gómez Sánchez
Senior Android Developer at Karumi
pedro@karumi.com
@pedro_g_s
github.com/pedrovgs
Pedro V. Gómez Sánchez - pedro@karumi.com - @pedro_g_s - github.com/pedrovgs
Pedro V. Gómez Sánchez - pedro@karumi.com - @pedro_g_s - github.com/pedrovgs
Alberto Gragera
Technical Director
Jorge Barroso
Senior Android Developer
Davide Mendolia
Senior Full Stack Engineer
Sergio Gutierrez
Senior Full Stack Engineer
Pedro V. Gómez Sánchez - pedro@karumi.com - @pedro_g_s - github.com/pedrovgs
Agenda
● Introduction.
● What can I do using Dependency Injection?
● Scoped Graphs.
● Dagger on Android.
● Dagger 2.
Pedro V. Gómez Sánchez - pedro@karumi.com - @pedro_g_s - github.com/pedrovgs
Introduction
Inversion of Control
vs
Dependency Inversion
vs
Dependency Injection
Pedro V. Gómez Sánchez - pedro@karumi.com - @pedro_g_s - github.com/pedrovgs
Inversion of Control
The natural evolution of the structured and imperative
programming and the usage of reusable frameworks.
The execution point where you software takes the control
is not defined by your software.
Pedro V. Gómez Sánchez - pedro@karumi.com - @pedro_g_s - github.com/pedrovgs
Dependency Inversion
One of the most important S.O.L.I.D principles.
Depend on abstractions, forget about implementation
details.
Provide implementation details depending on
abstractions can be tedious.
Pedro V. Gómez Sánchez - pedro@karumi.com - @pedro_g_s - github.com/pedrovgs
Dependency Injection
Software Design Pattern used to facilitate the usage of
Dependency Inversion.
Provides dependencies given some previous
configuration where abstractions are linked with
concretions.
Pedro V. Gómez Sánchez - pedro@karumi.com - @pedro_g_s - github.com/pedrovgs
Why?
The usage of IoC, Dependency Inversion and Dependency
Injection provides give us the following benefits:
● Testability.
● Decoupling.
● Modularity.
Pedro V. Gómez Sánchez - pedro@karumi.com - @pedro_g_s - github.com/pedrovgs
Show me the code!
Pedro V. Gómez Sánchez - pedro@karumi.com - @pedro_g_s - github.com/pedrovgs
Dependency Injection Example
The presenter inside this fragment has to be instantiated
with all the dependency tree. Without a dependency
injector this can be tedious.
Pedro V. Gómez Sánchez - pedro@karumi.com - @pedro_g_s - github.com/pedrovgs
Dependency Injection Example
All the dependencies are passed in construction.
Pedro V. Gómez Sánchez - pedro@karumi.com - @pedro_g_s - github.com/pedrovgs
Dependency Injection Example
The implementation details are part of the Dependency
Injector configuration.
Pedro V. Gómez Sánchez - pedro@karumi.com - @pedro_g_s - github.com/pedrovgs
Dependency Injection Example
Now the Presenter is injected into the Fragment.
Pedro V. Gómez Sánchez - pedro@karumi.com - @pedro_g_s - github.com/pedrovgs
What can I do with a
Dependency Injector?
Pedro V. Gómez Sánchez - pedro@karumi.com - @pedro_g_s - github.com/pedrovgs
What can I do?
● Do not link the class to the runtime lifecycle or frameworks.
● Improve testability.
● Decouple your code and improve our software design.
● Detect code smells related to class dependencies and SRP violations.
● Change the implementation details in build time.
● Extract implementation details out of the business logic.
Pedro V. Gómez Sánchez - pedro@karumi.com - @pedro_g_s - github.com/pedrovgs
What can I do?
Do not link the class to the runtime lifecycle.
Pedro V. Gómez Sánchez - pedro@karumi.com - @pedro_g_s - github.com/pedrovgs
What can I do?
Improve testability.
Pedro V. Gómez Sánchez - pedro@karumi.com - @pedro_g_s - github.com/pedrovgs
What can I do?
Detect code smells related to class dependencies and single
responsibility principle violations.
Pedro V. Gómez Sánchez - pedro@karumi.com - @pedro_g_s - github.com/pedrovgs
What can I do?
Change the implementation details in build time.
Pedro V. Gómez Sánchez - pedro@karumi.com - @pedro_g_s - github.com/pedrovgs
What can I do?
Decouple your code and improve our software design.
Using a Dependency Injector combined with the correct usage of
Dependency Inversion we can easily develop the same feature at the
same time in different teams and layers.
Pedro V. Gómez Sánchez - pedro@karumi.com - @pedro_g_s - github.com/pedrovgs
What can I do?
Extract implementation details out of the business logic.
Develop software being agnostic of the Framework or the Runtime used.
Implementation details are provided using the dependency injector and
your business logic does not depend on it.
Pedro V. Gómez Sánchez - pedro@karumi.com - @pedro_g_s - github.com/pedrovgs
What can I do?
The dependency container provide us different configurations to provide
dependencies:
● Singletons.
● Lazy Initialization.
● Provided dependencies.
● Named dependencies.
● Override dependencies already configured.
● Custom qualifiers.
Pedro V. Gómez Sánchez - pedro@karumi.com - @pedro_g_s - github.com/pedrovgs
What can I do?
Singletons
Pedro V. Gómez Sánchez - pedro@karumi.com - @pedro_g_s - github.com/pedrovgs
What can I do?
Lazy Initialization
Pedro V. Gómez Sánchez - pedro@karumi.com - @pedro_g_s - github.com/pedrovgs
What can I do?
Provided dependencies
Pedro V. Gómez Sánchez - pedro@karumi.com - @pedro_g_s - github.com/pedrovgs
What can I do?
Named Dependencies
Pedro V. Gómez Sánchez - pedro@karumi.com - @pedro_g_s - github.com/pedrovgs
What can I do?
Custom Qualifiers
Pedro V. Gómez Sánchez - pedro@karumi.com - @pedro_g_s - github.com/pedrovgs
Scoped Graphs
Pedro V. Gómez Sánchez - pedro@karumi.com - @pedro_g_s - github.com/pedrovgs
Scoped Graphs
Dependencies provided by the Dependency Container or Object Graph
inside the Application class are linked to the Application lifecycle.
We need more than one graph to improve the Dependency Injector usage
and performance.
At the same time, some dependencies are needed just when some
components like Activities or Custom Views are initialized.
Pedro V. Gómez Sánchez - pedro@karumi.com - @pedro_g_s - github.com/pedrovgs
Scoped Graphs
Application Graph
Activity A
Graph
Activity B
Graph
Create Activity A
Create Activity B
Pedro V. Gómez Sánchez - pedro@karumi.com - @pedro_g_s - github.com/pedrovgs
Dagger on Android
Pedro V. Gómez Sánchez - pedro@karumi.com - @pedro_g_s - github.com/pedrovgs
Dagger on Android
Step 1. Add Dagger and Dagger Compiler as dependencies:
Pedro V. Gómez Sánchez - pedro@karumi.com - @pedro_g_s - github.com/pedrovgs
Dagger on Android
Step 2. Create an Application extension and configure the Object Graph:
Pedro V. Gómez Sánchez - pedro@karumi.com - @pedro_g_s - github.com/pedrovgs
Dagger on Android
Step 3. Configure Dagger Modules:
Pedro V. Gómez Sánchez - pedro@karumi.com - @pedro_g_s - github.com/pedrovgs
Dagger on Android
Step 4. Initialize your Application Object Graph:
Pedro V. Gómez Sánchez - pedro@karumi.com - @pedro_g_s - github.com/pedrovgs
Dagger on Android
Step 5. Create a method to extend the Application Object Graph:
Pedro V. Gómez Sánchez - pedro@karumi.com - @pedro_g_s - github.com/pedrovgs
Dagger on Android
Step 6. Create a BaseActivity with the code needed to extend the
Application Graph.
Pedro V. Gómez Sánchez - pedro@karumi.com - @pedro_g_s - github.com/pedrovgs
Step 7. Create an abstract method requesting the Activity modules:
Dagger on Android
Pedro V. Gómez Sánchez - pedro@karumi.com - @pedro_g_s - github.com/pedrovgs
Step 8. Extend the Application Object Graph using the Activity modules:
Dagger on Android
Pedro V. Gómez Sánchez - pedro@karumi.com - @pedro_g_s - github.com/pedrovgs
https://www.github.com/pedrovgs/EffectiveAndroidUI
Pedro V. Gómez Sánchez - pedro@karumi.com - @pedro_g_s - github.com/pedrovgs
Can you repeat, please?
Dependency Injection on Android Workshop
Pedro V. Gómez Sánchez - pedro@karumi.com - @pedro_g_s - github.com/pedrovgs
Dagger 2
Pedro V. Gómez Sánchez - pedro@karumi.com - @pedro_g_s - github.com/pedrovgs
Dagger 2
● Configuration based on components not in graphs.
● Generated code is now something you can read.
● Reflection free.
● 13 % faster.
● Full graph validated in build time.
Pedro V. Gómez Sánchez - pedro@karumi.com - @pedro_g_s - github.com/pedrovgs
Questions?

More Related Content

What's hot

From zero to hero with kotlin & spring boot
From zero to hero with kotlin & spring bootFrom zero to hero with kotlin & spring boot
From zero to hero with kotlin & spring bootDebora Gomez Bertoli
 
Fearless Internationalization and Localization Across the Nations
Fearless Internationalization and Localization Across the NationsFearless Internationalization and Localization Across the Nations
Fearless Internationalization and Localization Across the NationsSiena Aguayo
 
Vaadin Flow - How to start - a short intro for Java Devs
Vaadin Flow - How to start - a short intro for Java DevsVaadin Flow - How to start - a short intro for Java Devs
Vaadin Flow - How to start - a short intro for Java DevsSven Ruppert
 
Why reactive programing matter, and how PicCollage adaptive it.
Why reactive programing matter, and how PicCollage adaptive it.Why reactive programing matter, and how PicCollage adaptive it.
Why reactive programing matter, and how PicCollage adaptive it.PRADA Hsiung
 
20180524 Android Taipei : RxJava Threading
20180524 Android Taipei : RxJava Threading20180524 Android Taipei : RxJava Threading
20180524 Android Taipei : RxJava ThreadingPRADA Hsiung
 

What's hot (6)

From zero to hero with kotlin & spring boot
From zero to hero with kotlin & spring bootFrom zero to hero with kotlin & spring boot
From zero to hero with kotlin & spring boot
 
Fearless Internationalization and Localization Across the Nations
Fearless Internationalization and Localization Across the NationsFearless Internationalization and Localization Across the Nations
Fearless Internationalization and Localization Across the Nations
 
Vaadin Flow - How to start - a short intro for Java Devs
Vaadin Flow - How to start - a short intro for Java DevsVaadin Flow - How to start - a short intro for Java Devs
Vaadin Flow - How to start - a short intro for Java Devs
 
Why reactive programing matter, and how PicCollage adaptive it.
Why reactive programing matter, and how PicCollage adaptive it.Why reactive programing matter, and how PicCollage adaptive it.
Why reactive programing matter, and how PicCollage adaptive it.
 
Usando gradle a seu favor
Usando gradle a seu favorUsando gradle a seu favor
Usando gradle a seu favor
 
20180524 Android Taipei : RxJava Threading
20180524 Android Taipei : RxJava Threading20180524 Android Taipei : RxJava Threading
20180524 Android Taipei : RxJava Threading
 

Viewers also liked

Android cleanarchitecture
Android cleanarchitectureAndroid cleanarchitecture
Android cleanarchitectureTomoaki Imai
 
Is Activity God? ~ The MVP Architecture ~
Is Activity God? ~ The MVP Architecture ~Is Activity God? ~ The MVP Architecture ~
Is Activity God? ~ The MVP Architecture ~Ken William
 
Android Clean Architecture for Dummies
Android Clean Architecture for DummiesAndroid Clean Architecture for Dummies
Android Clean Architecture for DummiesKengo Suzuki
 
Android Design Principles and Popular Patterns
Android Design Principles and Popular PatternsAndroid Design Principles and Popular Patterns
Android Design Principles and Popular PatternsFaiz Malkani
 
A Separation of Concerns: Clean Architecture on Android
A Separation of Concerns: Clean Architecture on AndroidA Separation of Concerns: Clean Architecture on Android
A Separation of Concerns: Clean Architecture on AndroidOutware Mobile
 
Lightning Talk - Clean Architecture and Design
Lightning Talk - Clean Architecture and DesignLightning Talk - Clean Architecture and Design
Lightning Talk - Clean Architecture and DesignDeivison Sporteman
 
Clean architecture: Android
Clean architecture: AndroidClean architecture: Android
Clean architecture: Androidintive
 
Introducing Clean Architecture
Introducing Clean ArchitectureIntroducing Clean Architecture
Introducing Clean ArchitectureRoc Boronat
 
Clean architecture
Clean architectureClean architecture
Clean architectureandbed
 
Design pattern in android
Design pattern in androidDesign pattern in android
Design pattern in androidJay Kumarr
 
Reactive Programming on Android - RxAndroid - RxJava
Reactive Programming on Android - RxAndroid - RxJavaReactive Programming on Android - RxAndroid - RxJava
Reactive Programming on Android - RxAndroid - RxJavaAli Muzaffar
 
MVVM and RxJava – the perfect mix
MVVM and RxJava – the perfect mixMVVM and RxJava – the perfect mix
MVVM and RxJava – the perfect mixFlorina Muntenescu
 
MOPCON 2015 - Tips of Mobile Continuous Delivery
MOPCON 2015 - Tips of Mobile Continuous DeliveryMOPCON 2015 - Tips of Mobile Continuous Delivery
MOPCON 2015 - Tips of Mobile Continuous Deliveryanistar sung
 
Continuous Delivery for Cross-Platform Mobile Apps
Continuous Delivery for Cross-Platform Mobile AppsContinuous Delivery for Cross-Platform Mobile Apps
Continuous Delivery for Cross-Platform Mobile AppsMovel
 
Continuous Delivery: Playing with Immutable servers @commitporto 2016
Continuous Delivery: Playing with Immutable servers @commitporto 2016Continuous Delivery: Playing with Immutable servers @commitporto 2016
Continuous Delivery: Playing with Immutable servers @commitporto 2016João Cravo
 
A Journey Through MV Wonderland
A Journey Through MV WonderlandA Journey Through MV Wonderland
A Journey Through MV WonderlandFlorina Muntenescu
 

Viewers also liked (20)

Software Design patterns on Android English
Software Design patterns on Android EnglishSoftware Design patterns on Android English
Software Design patterns on Android English
 
Android cleanarchitecture
Android cleanarchitectureAndroid cleanarchitecture
Android cleanarchitecture
 
The Good Developer - Spanish
The Good Developer - SpanishThe Good Developer - Spanish
The Good Developer - Spanish
 
Is Activity God? ~ The MVP Architecture ~
Is Activity God? ~ The MVP Architecture ~Is Activity God? ~ The MVP Architecture ~
Is Activity God? ~ The MVP Architecture ~
 
Android Clean Architecture for Dummies
Android Clean Architecture for DummiesAndroid Clean Architecture for Dummies
Android Clean Architecture for Dummies
 
Android Design Principles and Popular Patterns
Android Design Principles and Popular PatternsAndroid Design Principles and Popular Patterns
Android Design Principles and Popular Patterns
 
A Separation of Concerns: Clean Architecture on Android
A Separation of Concerns: Clean Architecture on AndroidA Separation of Concerns: Clean Architecture on Android
A Separation of Concerns: Clean Architecture on Android
 
Lightning Talk - Clean Architecture and Design
Lightning Talk - Clean Architecture and DesignLightning Talk - Clean Architecture and Design
Lightning Talk - Clean Architecture and Design
 
Clean architecture: Android
Clean architecture: AndroidClean architecture: Android
Clean architecture: Android
 
Introducing Clean Architecture
Introducing Clean ArchitectureIntroducing Clean Architecture
Introducing Clean Architecture
 
Clean architecture
Clean architectureClean architecture
Clean architecture
 
Design pattern in android
Design pattern in androidDesign pattern in android
Design pattern in android
 
Reactive Programming on Android - RxAndroid - RxJava
Reactive Programming on Android - RxAndroid - RxJavaReactive Programming on Android - RxAndroid - RxJava
Reactive Programming on Android - RxAndroid - RxJava
 
MVVM and RxJava – the perfect mix
MVVM and RxJava – the perfect mixMVVM and RxJava – the perfect mix
MVVM and RxJava – the perfect mix
 
MOPCON 2015 - Tips of Mobile Continuous Delivery
MOPCON 2015 - Tips of Mobile Continuous DeliveryMOPCON 2015 - Tips of Mobile Continuous Delivery
MOPCON 2015 - Tips of Mobile Continuous Delivery
 
Continuous Delivery for Cross-Platform Mobile Apps
Continuous Delivery for Cross-Platform Mobile AppsContinuous Delivery for Cross-Platform Mobile Apps
Continuous Delivery for Cross-Platform Mobile Apps
 
Continuous Delivery: Playing with Immutable servers @commitporto 2016
Continuous Delivery: Playing with Immutable servers @commitporto 2016Continuous Delivery: Playing with Immutable servers @commitporto 2016
Continuous Delivery: Playing with Immutable servers @commitporto 2016
 
Mobile Game Publishing Through The ZALO Game Center
Mobile Game Publishing Through The ZALO Game Center Mobile Game Publishing Through The ZALO Game Center
Mobile Game Publishing Through The ZALO Game Center
 
Choice Paralysis
Choice ParalysisChoice Paralysis
Choice Paralysis
 
A Journey Through MV Wonderland
A Journey Through MV WonderlandA Journey Through MV Wonderland
A Journey Through MV Wonderland
 

Similar to Dependency injection on Android

Build and Deploy a Python Web App to Amazon in 30 Mins
Build and Deploy a Python Web App to Amazon in 30 MinsBuild and Deploy a Python Web App to Amazon in 30 Mins
Build and Deploy a Python Web App to Amazon in 30 MinsJeff Hull
 
WordCamp LA 2014- Writing Code that Scales
WordCamp LA 2014-  Writing Code that ScalesWordCamp LA 2014-  Writing Code that Scales
WordCamp LA 2014- Writing Code that ScalesSpectrOMTech.com
 
Is Your App Hackable for droidcon Berlin 2015
Is Your App Hackable for droidcon Berlin 2015Is Your App Hackable for droidcon Berlin 2015
Is Your App Hackable for droidcon Berlin 2015Licel
 
Learn Django Tips, Tricks & Techniques for Developers
Learn Django Tips, Tricks & Techniques for DevelopersLearn Django Tips, Tricks & Techniques for Developers
Learn Django Tips, Tricks & Techniques for DevelopersMars Devs
 
Angular 2 : learn TypeScript already with Angular 1
Angular 2 : learn TypeScript already with Angular 1Angular 2 : learn TypeScript already with Angular 1
Angular 2 : learn TypeScript already with Angular 1David Amend
 
Ionic - Revolutionizing Hybrid Mobile Application Development
Ionic - Revolutionizing Hybrid Mobile Application DevelopmentIonic - Revolutionizing Hybrid Mobile Application Development
Ionic - Revolutionizing Hybrid Mobile Application DevelopmentJustin James
 
🤵-Friendly, Data Science Stack With 🏕️ envd.pdf
🤵-Friendly, Data Science Stack  With 🏕️ envd.pdf🤵-Friendly, Data Science Stack  With 🏕️ envd.pdf
🤵-Friendly, Data Science Stack With 🏕️ envd.pdfCe Gao
 
Frontend Development vs Backend Development | Detailed Comparison
Frontend Development vs Backend Development | Detailed ComparisonFrontend Development vs Backend Development | Detailed Comparison
Frontend Development vs Backend Development | Detailed ComparisonMariya James
 
Docker, your best ally to migrate & upgrading your Drupal - Drupal Dev Days S...
Docker, your best ally to migrate & upgrading your Drupal - Drupal Dev Days S...Docker, your best ally to migrate & upgrading your Drupal - Drupal Dev Days S...
Docker, your best ally to migrate & upgrading your Drupal - Drupal Dev Days S...La Drupalera
 
Creating Gradle Plugins
Creating Gradle PluginsCreating Gradle Plugins
Creating Gradle PluginsAnnyce Davis
 
Pain Driven Development by Alexandr Sugak
Pain Driven Development by Alexandr SugakPain Driven Development by Alexandr Sugak
Pain Driven Development by Alexandr SugakSigma Software
 
App in android Computer Science homework help.docx
App in android Computer Science homework help.docxApp in android Computer Science homework help.docx
App in android Computer Science homework help.docxstirlingvwriters
 
App in android Computer Science homework help.docx
App in android Computer Science homework help.docxApp in android Computer Science homework help.docx
App in android Computer Science homework help.docxbkbk37
 
Moderne Android Builds mit Gradle
Moderne Android Builds mit GradleModerne Android Builds mit Gradle
Moderne Android Builds mit Gradleinovex GmbH
 
UpdatedMuhammadBilalResume.docx (1)
UpdatedMuhammadBilalResume.docx (1)UpdatedMuhammadBilalResume.docx (1)
UpdatedMuhammadBilalResume.docx (1)Muhammad Bilal Ahmed
 
Arquitectura MVVM para la construcción de aplicaciones Windows Store
Arquitectura MVVM para la construcción de aplicaciones Windows StoreArquitectura MVVM para la construcción de aplicaciones Windows Store
Arquitectura MVVM para la construcción de aplicaciones Windows StoreDamian Schenkelman
 
ColdBox APIs + VueJS - powering Mobile, Desktop and Web Apps with 1 VueJS cod...
ColdBox APIs + VueJS - powering Mobile, Desktop and Web Apps with 1 VueJS cod...ColdBox APIs + VueJS - powering Mobile, Desktop and Web Apps with 1 VueJS cod...
ColdBox APIs + VueJS - powering Mobile, Desktop and Web Apps with 1 VueJS cod...Gavin Pickin
 
Intro To Django
Intro To DjangoIntro To Django
Intro To DjangoUdi Bauman
 
Docker and Django Meet For A Tango - London Meetup
Docker and Django Meet For A Tango - London MeetupDocker and Django Meet For A Tango - London Meetup
Docker and Django Meet For A Tango - London Meetupfrentrup
 
Web Development in Django
Web Development in DjangoWeb Development in Django
Web Development in DjangoLakshman Prasad
 

Similar to Dependency injection on Android (20)

Build and Deploy a Python Web App to Amazon in 30 Mins
Build and Deploy a Python Web App to Amazon in 30 MinsBuild and Deploy a Python Web App to Amazon in 30 Mins
Build and Deploy a Python Web App to Amazon in 30 Mins
 
WordCamp LA 2014- Writing Code that Scales
WordCamp LA 2014-  Writing Code that ScalesWordCamp LA 2014-  Writing Code that Scales
WordCamp LA 2014- Writing Code that Scales
 
Is Your App Hackable for droidcon Berlin 2015
Is Your App Hackable for droidcon Berlin 2015Is Your App Hackable for droidcon Berlin 2015
Is Your App Hackable for droidcon Berlin 2015
 
Learn Django Tips, Tricks & Techniques for Developers
Learn Django Tips, Tricks & Techniques for DevelopersLearn Django Tips, Tricks & Techniques for Developers
Learn Django Tips, Tricks & Techniques for Developers
 
Angular 2 : learn TypeScript already with Angular 1
Angular 2 : learn TypeScript already with Angular 1Angular 2 : learn TypeScript already with Angular 1
Angular 2 : learn TypeScript already with Angular 1
 
Ionic - Revolutionizing Hybrid Mobile Application Development
Ionic - Revolutionizing Hybrid Mobile Application DevelopmentIonic - Revolutionizing Hybrid Mobile Application Development
Ionic - Revolutionizing Hybrid Mobile Application Development
 
🤵-Friendly, Data Science Stack With 🏕️ envd.pdf
🤵-Friendly, Data Science Stack  With 🏕️ envd.pdf🤵-Friendly, Data Science Stack  With 🏕️ envd.pdf
🤵-Friendly, Data Science Stack With 🏕️ envd.pdf
 
Frontend Development vs Backend Development | Detailed Comparison
Frontend Development vs Backend Development | Detailed ComparisonFrontend Development vs Backend Development | Detailed Comparison
Frontend Development vs Backend Development | Detailed Comparison
 
Docker, your best ally to migrate & upgrading your Drupal - Drupal Dev Days S...
Docker, your best ally to migrate & upgrading your Drupal - Drupal Dev Days S...Docker, your best ally to migrate & upgrading your Drupal - Drupal Dev Days S...
Docker, your best ally to migrate & upgrading your Drupal - Drupal Dev Days S...
 
Creating Gradle Plugins
Creating Gradle PluginsCreating Gradle Plugins
Creating Gradle Plugins
 
Pain Driven Development by Alexandr Sugak
Pain Driven Development by Alexandr SugakPain Driven Development by Alexandr Sugak
Pain Driven Development by Alexandr Sugak
 
App in android Computer Science homework help.docx
App in android Computer Science homework help.docxApp in android Computer Science homework help.docx
App in android Computer Science homework help.docx
 
App in android Computer Science homework help.docx
App in android Computer Science homework help.docxApp in android Computer Science homework help.docx
App in android Computer Science homework help.docx
 
Moderne Android Builds mit Gradle
Moderne Android Builds mit GradleModerne Android Builds mit Gradle
Moderne Android Builds mit Gradle
 
UpdatedMuhammadBilalResume.docx (1)
UpdatedMuhammadBilalResume.docx (1)UpdatedMuhammadBilalResume.docx (1)
UpdatedMuhammadBilalResume.docx (1)
 
Arquitectura MVVM para la construcción de aplicaciones Windows Store
Arquitectura MVVM para la construcción de aplicaciones Windows StoreArquitectura MVVM para la construcción de aplicaciones Windows Store
Arquitectura MVVM para la construcción de aplicaciones Windows Store
 
ColdBox APIs + VueJS - powering Mobile, Desktop and Web Apps with 1 VueJS cod...
ColdBox APIs + VueJS - powering Mobile, Desktop and Web Apps with 1 VueJS cod...ColdBox APIs + VueJS - powering Mobile, Desktop and Web Apps with 1 VueJS cod...
ColdBox APIs + VueJS - powering Mobile, Desktop and Web Apps with 1 VueJS cod...
 
Intro To Django
Intro To DjangoIntro To Django
Intro To Django
 
Docker and Django Meet For A Tango - London Meetup
Docker and Django Meet For A Tango - London MeetupDocker and Django Meet For A Tango - London Meetup
Docker and Django Meet For A Tango - London Meetup
 
Web Development in Django
Web Development in DjangoWeb Development in Django
Web Development in Django
 

Recently uploaded

Past, Present and Future of Generative AI
Past, Present and Future of Generative AIPast, Present and Future of Generative AI
Past, Present and Future of Generative AIabhishek36461
 
Artificial-Intelligence-in-Electronics (K).pptx
Artificial-Intelligence-in-Electronics (K).pptxArtificial-Intelligence-in-Electronics (K).pptx
Artificial-Intelligence-in-Electronics (K).pptxbritheesh05
 
Sachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
Sachpazis Costas: Geotechnical Engineering: A student's Perspective IntroductionSachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
Sachpazis Costas: Geotechnical Engineering: A student's Perspective IntroductionDr.Costas Sachpazis
 
Software and Systems Engineering Standards: Verification and Validation of Sy...
Software and Systems Engineering Standards: Verification and Validation of Sy...Software and Systems Engineering Standards: Verification and Validation of Sy...
Software and Systems Engineering Standards: Verification and Validation of Sy...VICTOR MAESTRE RAMIREZ
 
Comparative Analysis of Text Summarization Techniques
Comparative Analysis of Text Summarization TechniquesComparative Analysis of Text Summarization Techniques
Comparative Analysis of Text Summarization Techniquesugginaramesh
 
IVE Industry Focused Event - Defence Sector 2024
IVE Industry Focused Event - Defence Sector 2024IVE Industry Focused Event - Defence Sector 2024
IVE Industry Focused Event - Defence Sector 2024Mark Billinghurst
 
INFLUENCE OF NANOSILICA ON THE PROPERTIES OF CONCRETE
INFLUENCE OF NANOSILICA ON THE PROPERTIES OF CONCRETEINFLUENCE OF NANOSILICA ON THE PROPERTIES OF CONCRETE
INFLUENCE OF NANOSILICA ON THE PROPERTIES OF CONCRETEroselinkalist12
 
Oxy acetylene welding presentation note.
Oxy acetylene welding presentation note.Oxy acetylene welding presentation note.
Oxy acetylene welding presentation note.eptoze12
 
Introduction to Machine Learning Unit-3 for II MECH
Introduction to Machine Learning Unit-3 for II MECHIntroduction to Machine Learning Unit-3 for II MECH
Introduction to Machine Learning Unit-3 for II MECHC Sai Kiran
 
What are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptxWhat are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptxwendy cai
 
Churning of Butter, Factors affecting .
Churning of Butter, Factors affecting  .Churning of Butter, Factors affecting  .
Churning of Butter, Factors affecting .Satyam Kumar
 
Concrete Mix Design - IS 10262-2019 - .pptx
Concrete Mix Design - IS 10262-2019 - .pptxConcrete Mix Design - IS 10262-2019 - .pptx
Concrete Mix Design - IS 10262-2019 - .pptxKartikeyaDwivedi3
 
Electronically Controlled suspensions system .pdf
Electronically Controlled suspensions system .pdfElectronically Controlled suspensions system .pdf
Electronically Controlled suspensions system .pdfme23b1001
 
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube ExchangerStudy on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube ExchangerAnamika Sarkar
 
TechTAC® CFD Report Summary: A Comparison of Two Types of Tubing Anchor Catchers
TechTAC® CFD Report Summary: A Comparison of Two Types of Tubing Anchor CatchersTechTAC® CFD Report Summary: A Comparison of Two Types of Tubing Anchor Catchers
TechTAC® CFD Report Summary: A Comparison of Two Types of Tubing Anchor Catcherssdickerson1
 
8251 universal synchronous asynchronous receiver transmitter
8251 universal synchronous asynchronous receiver transmitter8251 universal synchronous asynchronous receiver transmitter
8251 universal synchronous asynchronous receiver transmitterShivangiSharma879191
 
Call Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile serviceCall Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile servicerehmti665
 
Biology for Computer Engineers Course Handout.pptx
Biology for Computer Engineers Course Handout.pptxBiology for Computer Engineers Course Handout.pptx
Biology for Computer Engineers Course Handout.pptxDeepakSakkari2
 

Recently uploaded (20)

Past, Present and Future of Generative AI
Past, Present and Future of Generative AIPast, Present and Future of Generative AI
Past, Present and Future of Generative AI
 
Artificial-Intelligence-in-Electronics (K).pptx
Artificial-Intelligence-in-Electronics (K).pptxArtificial-Intelligence-in-Electronics (K).pptx
Artificial-Intelligence-in-Electronics (K).pptx
 
Sachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
Sachpazis Costas: Geotechnical Engineering: A student's Perspective IntroductionSachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
Sachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
 
9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf
9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf
9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf
 
Software and Systems Engineering Standards: Verification and Validation of Sy...
Software and Systems Engineering Standards: Verification and Validation of Sy...Software and Systems Engineering Standards: Verification and Validation of Sy...
Software and Systems Engineering Standards: Verification and Validation of Sy...
 
Comparative Analysis of Text Summarization Techniques
Comparative Analysis of Text Summarization TechniquesComparative Analysis of Text Summarization Techniques
Comparative Analysis of Text Summarization Techniques
 
IVE Industry Focused Event - Defence Sector 2024
IVE Industry Focused Event - Defence Sector 2024IVE Industry Focused Event - Defence Sector 2024
IVE Industry Focused Event - Defence Sector 2024
 
young call girls in Green Park🔝 9953056974 🔝 escort Service
young call girls in Green Park🔝 9953056974 🔝 escort Serviceyoung call girls in Green Park🔝 9953056974 🔝 escort Service
young call girls in Green Park🔝 9953056974 🔝 escort Service
 
INFLUENCE OF NANOSILICA ON THE PROPERTIES OF CONCRETE
INFLUENCE OF NANOSILICA ON THE PROPERTIES OF CONCRETEINFLUENCE OF NANOSILICA ON THE PROPERTIES OF CONCRETE
INFLUENCE OF NANOSILICA ON THE PROPERTIES OF CONCRETE
 
Oxy acetylene welding presentation note.
Oxy acetylene welding presentation note.Oxy acetylene welding presentation note.
Oxy acetylene welding presentation note.
 
Introduction to Machine Learning Unit-3 for II MECH
Introduction to Machine Learning Unit-3 for II MECHIntroduction to Machine Learning Unit-3 for II MECH
Introduction to Machine Learning Unit-3 for II MECH
 
What are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptxWhat are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptx
 
Churning of Butter, Factors affecting .
Churning of Butter, Factors affecting  .Churning of Butter, Factors affecting  .
Churning of Butter, Factors affecting .
 
Concrete Mix Design - IS 10262-2019 - .pptx
Concrete Mix Design - IS 10262-2019 - .pptxConcrete Mix Design - IS 10262-2019 - .pptx
Concrete Mix Design - IS 10262-2019 - .pptx
 
Electronically Controlled suspensions system .pdf
Electronically Controlled suspensions system .pdfElectronically Controlled suspensions system .pdf
Electronically Controlled suspensions system .pdf
 
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube ExchangerStudy on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
 
TechTAC® CFD Report Summary: A Comparison of Two Types of Tubing Anchor Catchers
TechTAC® CFD Report Summary: A Comparison of Two Types of Tubing Anchor CatchersTechTAC® CFD Report Summary: A Comparison of Two Types of Tubing Anchor Catchers
TechTAC® CFD Report Summary: A Comparison of Two Types of Tubing Anchor Catchers
 
8251 universal synchronous asynchronous receiver transmitter
8251 universal synchronous asynchronous receiver transmitter8251 universal synchronous asynchronous receiver transmitter
8251 universal synchronous asynchronous receiver transmitter
 
Call Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile serviceCall Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile service
 
Biology for Computer Engineers Course Handout.pptx
Biology for Computer Engineers Course Handout.pptxBiology for Computer Engineers Course Handout.pptx
Biology for Computer Engineers Course Handout.pptx
 

Dependency injection on Android

  • 1. Pedro V. Gómez Sánchez - pedro@karumi.com - @pedro_g_s - github.com/pedrovgs Dependency Injection on Android Pedro Vicente Gómez Sánchez Senior Android Developer at Karumi pedro@karumi.com @pedro_g_s github.com/pedrovgs
  • 2. Pedro V. Gómez Sánchez - pedro@karumi.com - @pedro_g_s - github.com/pedrovgs
  • 3. Pedro V. Gómez Sánchez - pedro@karumi.com - @pedro_g_s - github.com/pedrovgs Alberto Gragera Technical Director Jorge Barroso Senior Android Developer Davide Mendolia Senior Full Stack Engineer Sergio Gutierrez Senior Full Stack Engineer
  • 4. Pedro V. Gómez Sánchez - pedro@karumi.com - @pedro_g_s - github.com/pedrovgs Agenda ● Introduction. ● What can I do using Dependency Injection? ● Scoped Graphs. ● Dagger on Android. ● Dagger 2.
  • 5. Pedro V. Gómez Sánchez - pedro@karumi.com - @pedro_g_s - github.com/pedrovgs Introduction Inversion of Control vs Dependency Inversion vs Dependency Injection
  • 6. Pedro V. Gómez Sánchez - pedro@karumi.com - @pedro_g_s - github.com/pedrovgs Inversion of Control The natural evolution of the structured and imperative programming and the usage of reusable frameworks. The execution point where you software takes the control is not defined by your software.
  • 7. Pedro V. Gómez Sánchez - pedro@karumi.com - @pedro_g_s - github.com/pedrovgs Dependency Inversion One of the most important S.O.L.I.D principles. Depend on abstractions, forget about implementation details. Provide implementation details depending on abstractions can be tedious.
  • 8. Pedro V. Gómez Sánchez - pedro@karumi.com - @pedro_g_s - github.com/pedrovgs Dependency Injection Software Design Pattern used to facilitate the usage of Dependency Inversion. Provides dependencies given some previous configuration where abstractions are linked with concretions.
  • 9. Pedro V. Gómez Sánchez - pedro@karumi.com - @pedro_g_s - github.com/pedrovgs Why? The usage of IoC, Dependency Inversion and Dependency Injection provides give us the following benefits: ● Testability. ● Decoupling. ● Modularity.
  • 10. Pedro V. Gómez Sánchez - pedro@karumi.com - @pedro_g_s - github.com/pedrovgs Show me the code!
  • 11. Pedro V. Gómez Sánchez - pedro@karumi.com - @pedro_g_s - github.com/pedrovgs Dependency Injection Example The presenter inside this fragment has to be instantiated with all the dependency tree. Without a dependency injector this can be tedious.
  • 12. Pedro V. Gómez Sánchez - pedro@karumi.com - @pedro_g_s - github.com/pedrovgs Dependency Injection Example All the dependencies are passed in construction.
  • 13. Pedro V. Gómez Sánchez - pedro@karumi.com - @pedro_g_s - github.com/pedrovgs Dependency Injection Example The implementation details are part of the Dependency Injector configuration.
  • 14. Pedro V. Gómez Sánchez - pedro@karumi.com - @pedro_g_s - github.com/pedrovgs Dependency Injection Example Now the Presenter is injected into the Fragment.
  • 15. Pedro V. Gómez Sánchez - pedro@karumi.com - @pedro_g_s - github.com/pedrovgs What can I do with a Dependency Injector?
  • 16. Pedro V. Gómez Sánchez - pedro@karumi.com - @pedro_g_s - github.com/pedrovgs What can I do? ● Do not link the class to the runtime lifecycle or frameworks. ● Improve testability. ● Decouple your code and improve our software design. ● Detect code smells related to class dependencies and SRP violations. ● Change the implementation details in build time. ● Extract implementation details out of the business logic.
  • 17. Pedro V. Gómez Sánchez - pedro@karumi.com - @pedro_g_s - github.com/pedrovgs What can I do? Do not link the class to the runtime lifecycle.
  • 18. Pedro V. Gómez Sánchez - pedro@karumi.com - @pedro_g_s - github.com/pedrovgs What can I do? Improve testability.
  • 19. Pedro V. Gómez Sánchez - pedro@karumi.com - @pedro_g_s - github.com/pedrovgs What can I do? Detect code smells related to class dependencies and single responsibility principle violations.
  • 20. Pedro V. Gómez Sánchez - pedro@karumi.com - @pedro_g_s - github.com/pedrovgs What can I do? Change the implementation details in build time.
  • 21. Pedro V. Gómez Sánchez - pedro@karumi.com - @pedro_g_s - github.com/pedrovgs What can I do? Decouple your code and improve our software design. Using a Dependency Injector combined with the correct usage of Dependency Inversion we can easily develop the same feature at the same time in different teams and layers.
  • 22. Pedro V. Gómez Sánchez - pedro@karumi.com - @pedro_g_s - github.com/pedrovgs What can I do? Extract implementation details out of the business logic. Develop software being agnostic of the Framework or the Runtime used. Implementation details are provided using the dependency injector and your business logic does not depend on it.
  • 23. Pedro V. Gómez Sánchez - pedro@karumi.com - @pedro_g_s - github.com/pedrovgs What can I do? The dependency container provide us different configurations to provide dependencies: ● Singletons. ● Lazy Initialization. ● Provided dependencies. ● Named dependencies. ● Override dependencies already configured. ● Custom qualifiers.
  • 24. Pedro V. Gómez Sánchez - pedro@karumi.com - @pedro_g_s - github.com/pedrovgs What can I do? Singletons
  • 25. Pedro V. Gómez Sánchez - pedro@karumi.com - @pedro_g_s - github.com/pedrovgs What can I do? Lazy Initialization
  • 26. Pedro V. Gómez Sánchez - pedro@karumi.com - @pedro_g_s - github.com/pedrovgs What can I do? Provided dependencies
  • 27. Pedro V. Gómez Sánchez - pedro@karumi.com - @pedro_g_s - github.com/pedrovgs What can I do? Named Dependencies
  • 28. Pedro V. Gómez Sánchez - pedro@karumi.com - @pedro_g_s - github.com/pedrovgs What can I do? Custom Qualifiers
  • 29. Pedro V. Gómez Sánchez - pedro@karumi.com - @pedro_g_s - github.com/pedrovgs Scoped Graphs
  • 30. Pedro V. Gómez Sánchez - pedro@karumi.com - @pedro_g_s - github.com/pedrovgs Scoped Graphs Dependencies provided by the Dependency Container or Object Graph inside the Application class are linked to the Application lifecycle. We need more than one graph to improve the Dependency Injector usage and performance. At the same time, some dependencies are needed just when some components like Activities or Custom Views are initialized.
  • 31. Pedro V. Gómez Sánchez - pedro@karumi.com - @pedro_g_s - github.com/pedrovgs Scoped Graphs Application Graph Activity A Graph Activity B Graph Create Activity A Create Activity B
  • 32. Pedro V. Gómez Sánchez - pedro@karumi.com - @pedro_g_s - github.com/pedrovgs Dagger on Android
  • 33. Pedro V. Gómez Sánchez - pedro@karumi.com - @pedro_g_s - github.com/pedrovgs Dagger on Android Step 1. Add Dagger and Dagger Compiler as dependencies:
  • 34. Pedro V. Gómez Sánchez - pedro@karumi.com - @pedro_g_s - github.com/pedrovgs Dagger on Android Step 2. Create an Application extension and configure the Object Graph:
  • 35. Pedro V. Gómez Sánchez - pedro@karumi.com - @pedro_g_s - github.com/pedrovgs Dagger on Android Step 3. Configure Dagger Modules:
  • 36. Pedro V. Gómez Sánchez - pedro@karumi.com - @pedro_g_s - github.com/pedrovgs Dagger on Android Step 4. Initialize your Application Object Graph:
  • 37. Pedro V. Gómez Sánchez - pedro@karumi.com - @pedro_g_s - github.com/pedrovgs Dagger on Android Step 5. Create a method to extend the Application Object Graph:
  • 38. Pedro V. Gómez Sánchez - pedro@karumi.com - @pedro_g_s - github.com/pedrovgs Dagger on Android Step 6. Create a BaseActivity with the code needed to extend the Application Graph.
  • 39. Pedro V. Gómez Sánchez - pedro@karumi.com - @pedro_g_s - github.com/pedrovgs Step 7. Create an abstract method requesting the Activity modules: Dagger on Android
  • 40. Pedro V. Gómez Sánchez - pedro@karumi.com - @pedro_g_s - github.com/pedrovgs Step 8. Extend the Application Object Graph using the Activity modules: Dagger on Android
  • 41. Pedro V. Gómez Sánchez - pedro@karumi.com - @pedro_g_s - github.com/pedrovgs https://www.github.com/pedrovgs/EffectiveAndroidUI
  • 42. Pedro V. Gómez Sánchez - pedro@karumi.com - @pedro_g_s - github.com/pedrovgs Can you repeat, please? Dependency Injection on Android Workshop
  • 43. Pedro V. Gómez Sánchez - pedro@karumi.com - @pedro_g_s - github.com/pedrovgs Dagger 2
  • 44. Pedro V. Gómez Sánchez - pedro@karumi.com - @pedro_g_s - github.com/pedrovgs Dagger 2 ● Configuration based on components not in graphs. ● Generated code is now something you can read. ● Reflection free. ● 13 % faster. ● Full graph validated in build time.
  • 45. Pedro V. Gómez Sánchez - pedro@karumi.com - @pedro_g_s - github.com/pedrovgs Questions?