SlideShare a Scribd company logo
1 of 24
DI in multi-module
application
Oleg Mazhukin
Senior Android Developer
DI has many faces
● IoC – Inversion of Control
● DI(P) – Dependency Inversion Principle
● DI – Dependency Injection
Inversion of Control
Design principle inverts the flow of control as compared to traditional
control flow. E.g.
To create user screen we extend Activity class with generic
functionality can be selectively changed by additional user-written
code i.e. Android framework provides custom-written portions of a
computer program receive the flow of control from a generic
framework
Dependency Inversion Principle
● High-level modules should not depend on low-level modules. Both
should depend on abstractions (e.g. interfaces). e.g. Clean
Architecture
● Abstractions should not depend on details. Details (concrete
implementations) should depend on abstractions.
Dependency Injection
Design pattern used to implement IoC. It allows the creation of dependent objects
outside of a class and provides those objects to a class through different ways.
Design pattern vs design principle
● Design principles provide high level guidelines to design better software
applications. They do not provide implementation guidelines and are not
bound to any programming language.
Examples: SOLID, GRASP
● Design Pattern suggests a specific implementation for the specific object-
oriented programming problem i.e provides reliable solution in the specific
context.
Examples: Observer, Factory, Adapter, etc.
Multi-module application
app module
● combines all feature modules
● provides common dependencies
○ OkHttpClient, RoomDao, Preferences, etc.
feature modules
● contains feature specific module
● can have one of more scopes
core modules
● utils/common module solutions
Do you need multi-module app?
● Modules can be reused in different apps
● Instant apps, app bundles
● Faster build time ( in theory )
● Separate modules are more convenient for big developers teams
Creating our own DI Framework
- injection implementation
- same types/different target solution
- lifecycle/visibility markes
- providers
Meet JSR-330: javax.inject.*
● @Inject - target for dependency
● @Qualifier (i.e @Named ) - same types/different target solution
● @Scope (i.e @Singleton ) - lifecycle/visibility markes
JSR-330 Implementations
● dagger2
● spring
● toothpick
Component dependency vs submodules
● Submodules have access to all objects from all modules it possesses without
an explicit declaration of those dependencies in the parent Component
SubComponent plus(SubcomponentModule module)
● Component dependency requires explicit dependency declaration
@Component(
dependencies = [ComponentDependencies::class],..
) interface Component {
Example app
Features
● login
● content
Dagger graph
● Features ask for dependencies
● Feature can use common Injector or create its own
● App module provides dependencies
● Component destroyed based on lifecycle
App Module
Feature App Dependencies
Feature App Modules
Core Module
Core Module
Main App class
Main App class
Feature component usage
Resolving common module dependencies
● moving dependency to app module
● moving dependency to core module
● using interfaces
Tips
● use feature prefixes
● don’t use direct feature dependencies
● split feature on data/feature modules
● use android free modules if possible
● use feature scopes only on objects with state
● use composite builds
Resources
Google recommends https://developer.android.com/training/dependency-injection/dagger-multi-module
Best Dagger Article https://habr.com/ru/post/279125/
Composite builds https://docs.gradle.org/current/userguide/composite_builds.html

More Related Content

What's hot

Programming Game AI by Example. Ch7. Raven
Programming Game AI by Example. Ch7. RavenProgramming Game AI by Example. Ch7. Raven
Programming Game AI by Example. Ch7. RavenRyan Park
 
Gradle Kotlin 컨벤션 플러그인으로 효율적으로 멀티 모듈 관리하기
Gradle Kotlin 컨벤션 플러그인으로 효율적으로 멀티 모듈 관리하기Gradle Kotlin 컨벤션 플러그인으로 효율적으로 멀티 모듈 관리하기
Gradle Kotlin 컨벤션 플러그인으로 효율적으로 멀티 모듈 관리하기YoungjikYoon
 
Using Dagger in a Clean Architecture project
Using Dagger in a Clean Architecture projectUsing Dagger in a Clean Architecture project
Using Dagger in a Clean Architecture projectFabio Collini
 
Scalable JavaScript Design Patterns
Scalable JavaScript Design PatternsScalable JavaScript Design Patterns
Scalable JavaScript Design PatternsAddy Osmani
 
안드로이드 윈도우 마스터 되기
안드로이드 윈도우 마스터 되기안드로이드 윈도우 마스터 되기
안드로이드 윈도우 마스터 되기Myungwook Ahn
 
Design Pattern - MVC, MVP and MVVM
Design Pattern - MVC, MVP and MVVMDesign Pattern - MVC, MVP and MVVM
Design Pattern - MVC, MVP and MVVMMudasir Qazi
 
소프트웨어 개선 그룹(Sig) 개발 원칙
소프트웨어 개선 그룹(Sig) 개발 원칙소프트웨어 개선 그룹(Sig) 개발 원칙
소프트웨어 개선 그룹(Sig) 개발 원칙Hong Hyo Sang
 
[오픈소스컨설팅]쿠버네티스를 활용한 개발환경 구축
[오픈소스컨설팅]쿠버네티스를 활용한 개발환경 구축[오픈소스컨설팅]쿠버네티스를 활용한 개발환경 구축
[오픈소스컨설팅]쿠버네티스를 활용한 개발환경 구축Ji-Woong Choi
 
[22]Efficient and Testable MVVM pattern
[22]Efficient and Testable MVVM pattern[22]Efficient and Testable MVVM pattern
[22]Efficient and Testable MVVM patternNAVER Engineering
 
LetsSwift(강민규스피커,안정민서포터).pptx
LetsSwift(강민규스피커,안정민서포터).pptxLetsSwift(강민규스피커,안정민서포터).pptx
LetsSwift(강민규스피커,안정민서포터).pptxssuser2601f7
 
Abstract Factory Pattern (Example & Implementation in Java)
Abstract Factory Pattern (Example & Implementation in Java)Abstract Factory Pattern (Example & Implementation in Java)
Abstract Factory Pattern (Example & Implementation in Java)Jubayer Al Mahmud
 
우아한테크세미나-우아한멀티모듈
우아한테크세미나-우아한멀티모듈우아한테크세미나-우아한멀티모듈
우아한테크세미나-우아한멀티모듈용근 권
 
Massive service basic
Massive service basicMassive service basic
Massive service basicDaeMyung Kang
 
Amazon ECS를 통한 도커 기반 콘테이너 서비스 구축하기 - AWS Summit Seoul 2017
Amazon ECS를 통한 도커 기반 콘테이너 서비스 구축하기 - AWS Summit Seoul 2017Amazon ECS를 통한 도커 기반 콘테이너 서비스 구축하기 - AWS Summit Seoul 2017
Amazon ECS를 통한 도커 기반 콘테이너 서비스 구축하기 - AWS Summit Seoul 2017Amazon Web Services Korea
 
IBM Integeration Bus(IIB) Fundamentals
IBM Integeration Bus(IIB) FundamentalsIBM Integeration Bus(IIB) Fundamentals
IBM Integeration Bus(IIB) FundamentalsHamid Ghorbani
 
GitLab Premium 라이선스 기능소개 - 인포그랩
GitLab Premium 라이선스 기능소개 - 인포그랩GitLab Premium 라이선스 기능소개 - 인포그랩
GitLab Premium 라이선스 기능소개 - 인포그랩InfoGrab LC
 
Web assembly: a brief overview
Web assembly: a brief overviewWeb assembly: a brief overview
Web assembly: a brief overviewPavlo Iatsiuk
 
Scala 3 by Example - Algebraic Data Types for Domain Driven Design - Part 1
Scala 3 by Example - Algebraic Data Types for Domain Driven Design - Part 1Scala 3 by Example - Algebraic Data Types for Domain Driven Design - Part 1
Scala 3 by Example - Algebraic Data Types for Domain Driven Design - Part 1Philip Schwarz
 

What's hot (20)

Programming Game AI by Example. Ch7. Raven
Programming Game AI by Example. Ch7. RavenProgramming Game AI by Example. Ch7. Raven
Programming Game AI by Example. Ch7. Raven
 
Gradle Kotlin 컨벤션 플러그인으로 효율적으로 멀티 모듈 관리하기
Gradle Kotlin 컨벤션 플러그인으로 효율적으로 멀티 모듈 관리하기Gradle Kotlin 컨벤션 플러그인으로 효율적으로 멀티 모듈 관리하기
Gradle Kotlin 컨벤션 플러그인으로 효율적으로 멀티 모듈 관리하기
 
Using Dagger in a Clean Architecture project
Using Dagger in a Clean Architecture projectUsing Dagger in a Clean Architecture project
Using Dagger in a Clean Architecture project
 
Kotlin on android
Kotlin on androidKotlin on android
Kotlin on android
 
Scalable JavaScript Design Patterns
Scalable JavaScript Design PatternsScalable JavaScript Design Patterns
Scalable JavaScript Design Patterns
 
안드로이드 윈도우 마스터 되기
안드로이드 윈도우 마스터 되기안드로이드 윈도우 마스터 되기
안드로이드 윈도우 마스터 되기
 
Design Pattern - MVC, MVP and MVVM
Design Pattern - MVC, MVP and MVVMDesign Pattern - MVC, MVP and MVVM
Design Pattern - MVC, MVP and MVVM
 
소프트웨어 개선 그룹(Sig) 개발 원칙
소프트웨어 개선 그룹(Sig) 개발 원칙소프트웨어 개선 그룹(Sig) 개발 원칙
소프트웨어 개선 그룹(Sig) 개발 원칙
 
[오픈소스컨설팅]쿠버네티스를 활용한 개발환경 구축
[오픈소스컨설팅]쿠버네티스를 활용한 개발환경 구축[오픈소스컨설팅]쿠버네티스를 활용한 개발환경 구축
[오픈소스컨설팅]쿠버네티스를 활용한 개발환경 구축
 
[22]Efficient and Testable MVVM pattern
[22]Efficient and Testable MVVM pattern[22]Efficient and Testable MVVM pattern
[22]Efficient and Testable MVVM pattern
 
Introduction to Maven
Introduction to MavenIntroduction to Maven
Introduction to Maven
 
LetsSwift(강민규스피커,안정민서포터).pptx
LetsSwift(강민규스피커,안정민서포터).pptxLetsSwift(강민규스피커,안정민서포터).pptx
LetsSwift(강민규스피커,안정민서포터).pptx
 
Abstract Factory Pattern (Example & Implementation in Java)
Abstract Factory Pattern (Example & Implementation in Java)Abstract Factory Pattern (Example & Implementation in Java)
Abstract Factory Pattern (Example & Implementation in Java)
 
우아한테크세미나-우아한멀티모듈
우아한테크세미나-우아한멀티모듈우아한테크세미나-우아한멀티모듈
우아한테크세미나-우아한멀티모듈
 
Massive service basic
Massive service basicMassive service basic
Massive service basic
 
Amazon ECS를 통한 도커 기반 콘테이너 서비스 구축하기 - AWS Summit Seoul 2017
Amazon ECS를 통한 도커 기반 콘테이너 서비스 구축하기 - AWS Summit Seoul 2017Amazon ECS를 통한 도커 기반 콘테이너 서비스 구축하기 - AWS Summit Seoul 2017
Amazon ECS를 통한 도커 기반 콘테이너 서비스 구축하기 - AWS Summit Seoul 2017
 
IBM Integeration Bus(IIB) Fundamentals
IBM Integeration Bus(IIB) FundamentalsIBM Integeration Bus(IIB) Fundamentals
IBM Integeration Bus(IIB) Fundamentals
 
GitLab Premium 라이선스 기능소개 - 인포그랩
GitLab Premium 라이선스 기능소개 - 인포그랩GitLab Premium 라이선스 기능소개 - 인포그랩
GitLab Premium 라이선스 기능소개 - 인포그랩
 
Web assembly: a brief overview
Web assembly: a brief overviewWeb assembly: a brief overview
Web assembly: a brief overview
 
Scala 3 by Example - Algebraic Data Types for Domain Driven Design - Part 1
Scala 3 by Example - Algebraic Data Types for Domain Driven Design - Part 1Scala 3 by Example - Algebraic Data Types for Domain Driven Design - Part 1
Scala 3 by Example - Algebraic Data Types for Domain Driven Design - Part 1
 

Similar to [Android] DI in multimodule application

Design Patterns Part1
Design Patterns  Part1Design Patterns  Part1
Design Patterns Part1Tom Chen
 
Angular.ppt
Angular.pptAngular.ppt
Angular.pptMytrux1
 
Innovation Generation - The Mobile Meetup: Android Best Practices
Innovation Generation - The Mobile Meetup: Android Best PracticesInnovation Generation - The Mobile Meetup: Android Best Practices
Innovation Generation - The Mobile Meetup: Android Best PracticesSolstice Mobile Argentina
 
3.o o design -_____________lecture 3
3.o o design -_____________lecture 33.o o design -_____________lecture 3
3.o o design -_____________lecture 3Warui Maina
 
Dependency Injection, Design Principles and Patterns
Dependency Injection, Design Principles and PatternsDependency Injection, Design Principles and Patterns
Dependency Injection, Design Principles and PatternsJuan Lopez
 
Dependency injection using Google guice
Dependency injection using Google guiceDependency injection using Google guice
Dependency injection using Google guiceAman Verma
 
Mobile App Architectures & Coding guidelines
Mobile App Architectures & Coding guidelinesMobile App Architectures & Coding guidelines
Mobile App Architectures & Coding guidelinesQamar Abbas
 
Internship-Report-VitulChauhan-18132023-IT_CRUD-OPERATION.pdf
Internship-Report-VitulChauhan-18132023-IT_CRUD-OPERATION.pdfInternship-Report-VitulChauhan-18132023-IT_CRUD-OPERATION.pdf
Internship-Report-VitulChauhan-18132023-IT_CRUD-OPERATION.pdfVitulChauhan
 
[DevDay2018] Liferay DXP – A powerful Enterprise Solution - By Vy Bui, Develo...
[DevDay2018] Liferay DXP – A powerful Enterprise Solution - By Vy Bui, Develo...[DevDay2018] Liferay DXP – A powerful Enterprise Solution - By Vy Bui, Develo...
[DevDay2018] Liferay DXP – A powerful Enterprise Solution - By Vy Bui, Develo...DevDay.org
 
Benefits of using software design patterns and when to use design pattern
Benefits of using software design patterns and when to use design patternBenefits of using software design patterns and when to use design pattern
Benefits of using software design patterns and when to use design patternBeroza Paul
 
Foster - Getting started with Angular
Foster - Getting started with AngularFoster - Getting started with Angular
Foster - Getting started with AngularMukundSonaiya1
 
Dependency Injection in .NET
Dependency Injection in .NETDependency Injection in .NET
Dependency Injection in .NETssusere19c741
 
Introduction to Spring Framework
Introduction to Spring FrameworkIntroduction to Spring Framework
Introduction to Spring FrameworkASG
 

Similar to [Android] DI in multimodule application (20)

Clean Architecture
Clean ArchitectureClean Architecture
Clean Architecture
 
Dagger2 Intro
Dagger2 IntroDagger2 Intro
Dagger2 Intro
 
Design Patterns Part1
Design Patterns  Part1Design Patterns  Part1
Design Patterns Part1
 
Angular.ppt
Angular.pptAngular.ppt
Angular.ppt
 
Innovation Generation - The Mobile Meetup: Android Best Practices
Innovation Generation - The Mobile Meetup: Android Best PracticesInnovation Generation - The Mobile Meetup: Android Best Practices
Innovation Generation - The Mobile Meetup: Android Best Practices
 
3.o o design -_____________lecture 3
3.o o design -_____________lecture 33.o o design -_____________lecture 3
3.o o design -_____________lecture 3
 
Dependency Injection, Design Principles and Patterns
Dependency Injection, Design Principles and PatternsDependency Injection, Design Principles and Patterns
Dependency Injection, Design Principles and Patterns
 
Dependency injection using Google guice
Dependency injection using Google guiceDependency injection using Google guice
Dependency injection using Google guice
 
JavaFX in Action Part I
JavaFX in Action Part IJavaFX in Action Part I
JavaFX in Action Part I
 
Mobile App Architectures & Coding guidelines
Mobile App Architectures & Coding guidelinesMobile App Architectures & Coding guidelines
Mobile App Architectures & Coding guidelines
 
Internship-Report-VitulChauhan-18132023-IT_CRUD-OPERATION.pdf
Internship-Report-VitulChauhan-18132023-IT_CRUD-OPERATION.pdfInternship-Report-VitulChauhan-18132023-IT_CRUD-OPERATION.pdf
Internship-Report-VitulChauhan-18132023-IT_CRUD-OPERATION.pdf
 
[DevDay2018] Liferay DXP – A powerful Enterprise Solution - By Vy Bui, Develo...
[DevDay2018] Liferay DXP – A powerful Enterprise Solution - By Vy Bui, Develo...[DevDay2018] Liferay DXP – A powerful Enterprise Solution - By Vy Bui, Develo...
[DevDay2018] Liferay DXP – A powerful Enterprise Solution - By Vy Bui, Develo...
 
Software Engineering CSE/IT.pptx
 Software Engineering CSE/IT.pptx Software Engineering CSE/IT.pptx
Software Engineering CSE/IT.pptx
 
Benefits of using software design patterns and when to use design pattern
Benefits of using software design patterns and when to use design patternBenefits of using software design patterns and when to use design pattern
Benefits of using software design patterns and when to use design pattern
 
MVP Clean Architecture
MVP Clean  Architecture MVP Clean  Architecture
MVP Clean Architecture
 
Foster - Getting started with Angular
Foster - Getting started with AngularFoster - Getting started with Angular
Foster - Getting started with Angular
 
Libreplan architecture
Libreplan architectureLibreplan architecture
Libreplan architecture
 
Dependency Injection in .NET
Dependency Injection in .NETDependency Injection in .NET
Dependency Injection in .NET
 
Introduction to Spring Framework
Introduction to Spring FrameworkIntroduction to Spring Framework
Introduction to Spring Framework
 
SOLID
SOLIDSOLID
SOLID
 

[Android] DI in multimodule application

  • 1. DI in multi-module application Oleg Mazhukin Senior Android Developer
  • 2. DI has many faces ● IoC – Inversion of Control ● DI(P) – Dependency Inversion Principle ● DI – Dependency Injection
  • 3. Inversion of Control Design principle inverts the flow of control as compared to traditional control flow. E.g. To create user screen we extend Activity class with generic functionality can be selectively changed by additional user-written code i.e. Android framework provides custom-written portions of a computer program receive the flow of control from a generic framework
  • 4. Dependency Inversion Principle ● High-level modules should not depend on low-level modules. Both should depend on abstractions (e.g. interfaces). e.g. Clean Architecture ● Abstractions should not depend on details. Details (concrete implementations) should depend on abstractions.
  • 5. Dependency Injection Design pattern used to implement IoC. It allows the creation of dependent objects outside of a class and provides those objects to a class through different ways.
  • 6. Design pattern vs design principle ● Design principles provide high level guidelines to design better software applications. They do not provide implementation guidelines and are not bound to any programming language. Examples: SOLID, GRASP ● Design Pattern suggests a specific implementation for the specific object- oriented programming problem i.e provides reliable solution in the specific context. Examples: Observer, Factory, Adapter, etc.
  • 7. Multi-module application app module ● combines all feature modules ● provides common dependencies ○ OkHttpClient, RoomDao, Preferences, etc. feature modules ● contains feature specific module ● can have one of more scopes core modules ● utils/common module solutions
  • 8. Do you need multi-module app? ● Modules can be reused in different apps ● Instant apps, app bundles ● Faster build time ( in theory ) ● Separate modules are more convenient for big developers teams
  • 9. Creating our own DI Framework - injection implementation - same types/different target solution - lifecycle/visibility markes - providers
  • 10. Meet JSR-330: javax.inject.* ● @Inject - target for dependency ● @Qualifier (i.e @Named ) - same types/different target solution ● @Scope (i.e @Singleton ) - lifecycle/visibility markes JSR-330 Implementations ● dagger2 ● spring ● toothpick
  • 11. Component dependency vs submodules ● Submodules have access to all objects from all modules it possesses without an explicit declaration of those dependencies in the parent Component SubComponent plus(SubcomponentModule module) ● Component dependency requires explicit dependency declaration @Component( dependencies = [ComponentDependencies::class],.. ) interface Component {
  • 13. Dagger graph ● Features ask for dependencies ● Feature can use common Injector or create its own ● App module provides dependencies ● Component destroyed based on lifecycle
  • 22. Resolving common module dependencies ● moving dependency to app module ● moving dependency to core module ● using interfaces
  • 23. Tips ● use feature prefixes ● don’t use direct feature dependencies ● split feature on data/feature modules ● use android free modules if possible ● use feature scopes only on objects with state ● use composite builds
  • 24. Resources Google recommends https://developer.android.com/training/dependency-injection/dagger-multi-module Best Dagger Article https://habr.com/ru/post/279125/ Composite builds https://docs.gradle.org/current/userguide/composite_builds.html