Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
Dagger 2 : Back to basics
Search
Sponsored
·
Your Podcast. Everywhere. Effortlessly.
Share. Educate. Inspire. Entertain. You do you. We'll handle the rest.
→
Jeremie Martinez
November 10, 2015
Programming
1.1k
2
Share
Dagger 2 : Back to basics
Slides de mon talk sur Dagger 2 à la Droidcon Paris.
Jeremie Martinez
November 10, 2015
More Decks by Jeremie Martinez
See All by Jeremie Martinez
Gradle, je t'aime, moi non plus…
jeremiemartinez
0
350
The evolution of Android notification
jeremiemartinez
5
5.2k
Tips and tricks du rebranding de l'app Captain Train
jeremiemartinez
1
360
DevOps sur Android : from one git push to a Play Store release
jeremiemartinez
17
2.7k
See the Truth
jeremiemartinez
0
350
DevOps sur Android : D'un git push à une release Play Store
jeremiemartinez
5
600
La stack réseau Android, disponible également pour vos backs
jeremiemartinez
0
540
Other Decks in Programming
See All in Programming
AI-DLC 入門 〜AIコーディングの本質は「コード」ではなく「構造」〜 / Introduction to AI-DLC: The Essence of AI Coding Is Not “Code” but “Structure”
seike460
PRO
0
130
Smarter Angular mit Transformers.js & Prompt API
christianliebel
PRO
1
110
LM Linkで(非力な!)ノートPCでローカルLLM
seosoft
0
290
メッセージングを利用して時間的結合を分離しよう #phperkaigi
kajitack
3
520
ファインチューニングせずメインコンペを解く方法
pokutuna
0
220
Migration to Signals, Signal Forms, Resource API, and NgRx Signal Store @Angular Days 03/2026 Munich
manfredsteyer
PRO
0
200
OTP を自動で入力する裏技
megabitsenmzq
0
130
へんな働き方
yusukebe
6
2.9k
野球解説AI Agentを開発してみた - 2026/02/27 LayerX社内LT会資料
shinyorke
PRO
0
380
コードレビューをしない選択 #でぃーぷらすトウキョウ
kajitack
3
1.2k
GoのDB アクセスにおける 「型安全」と「柔軟性」の両立 - Bob という選択肢
tak848
0
290
今こそ押さえておきたい アマゾンウェブサービス(AWS)の データベースの基礎 おもクラ #6版
satoshi256kbyte
1
210
Featured
See All Featured
Rails Girls Zürich Keynote
gr2m
96
14k
GraphQLの誤解/rethinking-graphql
sonatard
75
12k
SEO in 2025: How to Prepare for the Future of Search
ipullrank
3
3.4k
Claude Code どこまでも/ Claude Code Everywhere
nwiizo
64
54k
The MySQL Ecosystem @ GitHub 2015
samlambert
251
13k
Unsuck your backbone
ammeep
672
58k
実際に使うSQLの書き方 徹底解説 / pgcon21j-tutorial
soudai
PRO
199
73k
Bioeconomy Workshop: Dr. Julius Ecuru, Opportunities for a Bioeconomy in West Africa
akademiya2063
PRO
1
79
The Pragmatic Product Professional
lauravandoore
37
7.2k
Principles of Awesome APIs and How to Build Them.
keavy
128
17k
What does AI have to do with Human Rights?
axbom
PRO
1
2.1k
Groundhog Day: Seeking Process in Gaming for Health
codingconduct
0
140
Transcript
Dagger 2 Back to basics @JeremMartinez 1 jeremie-martinez.com
Inversion of Control “Don’t call us, we’ll call you…” 2
Injection de dépendances JSR-330 Bob Lee Créateur de Guice Rod
Johnson Créateur de Spring 3
Pourquoi ? 4 Single Responsibility principle Meilleur découpage Facilement testable
Meilleur maintenabilité Qualité de code
Un peu d’Historique “Once upon a time…” 5
Problèmes 6
Problèmes BOILERPLATE 7 du code
Problèmes LENT à l’initialisation et à l’injection 8
Problèmes LOURD en mémoire et dans l’APK 9
Problèmes FAIL au runtime 10
Besoins Analyse statique Pas d’introspection Fail fast Aucun impact 11
Dagger by Square 12
Dagger 1 Créé en 2012 par Jesse Wilson (Guice) Encadré
par Bob Lee (Guice, JSR330, Guava, CTO Square) Ouvert en mai 2013 Dernière version en Juillet 2014 (1.2.2) 13
Bilan Rapide et facile JSR330 Compatible Utilisable sur Android Annotation
Processing Ouvert à la communauté Java Introspection sur les classes Pas totalement au compile time Analyse pas totalement statique Proguard 14
Dagger 2 by Google 15
Dagger 2 Repris par la Java Core Librairies Team Assisté
par Square Release en avril 2015 Résout les problématiques restantes de la version 1 16
API 17
API Fournir les dépendances : @Provides et @Module Demander des
dépendances : @Inject Lier les modules et les injections : @Component Implémenter le pattern singleton : @Singleton 18
3 formes d’injection 19
3 formes d’injection Par constructeur 20
Par constructeur public class ApiManager { private final Api api;
@Inject public ApiManager(Api api) { this.api = api; } … } 21
3 formes d’injection Par constructeur Par field 22
Par field public class ApiManager { @Inject Api api; …
} 23
3 formes d’injection Par setter Par constructeur Par field 24
Par field public class ApiManager { private Api api; @Inject
public void setApi(Api api) { … } … } 25
Par field public class ApiManager { private Api api; @Inject
public void setApi(Api api) { … } … } Not supported 26
Live coding 27
SpeakersActivity SlotsActivity 28
SpeakersActivity SlotsActivity SpeakerManager SlotManager 29
SpeakersActivity SlotsActivity SpeakerManager SlotManager 30
SpeakersActivity SlotsActivity SpeakerManager SlotManager ApiManager 31
SpeakersActivity SlotsActivity SpeakerManager SlotManager ApiManager HttpClient Converter 32
SpeakersActivity SlotsActivity SpeakerManager SlotManager ApiManager HttpClient EventBus Converter 33
SpeakersActivity SlotsActivity SpeakerManager SlotManager ApiManager HttpClient EventBus Converter 34
Pour aller plus loin… 35
Scope “(sous-)Graphe local à durée de vie limitée” 36
Scope - Exemple Application Scope UserScope UserScope Activity Scope ActivityScope
ActivityScope Activity Scope ActivityScope ActivityScope DetailsActivity ListActivity LoginActivity LoginActivity ListActivity DetailsActivity Connexion Changement 37 Deconnexion
Scope “(sous-)Graphe local à durée de vie limitée” Par exemple
: • Singleton • PerActivity •PerUser • PerRequest 38
Scope - API @Scope @Retention(RetentionPolicy.RUNTIME) public @interface UserScope{} Ne pas
oublier d’annoter le composant ! 39
Scope - Conclusion Clarté du graphe Mémoire Meilleure modélisation 40
Lazy @Inject Lazy<ApiManager> lazyApi; ApiManager api = lazyApi.get(); Déplace l’initialisation
de l’objet à sa première utilisation 41
Named Injection par chaine de caractères Un seul qualifier par
dépendance @Inject @Named("userPrefs") SharedPreferences userPrefs; @Inject @Named("appPrefs") SharedPreferences appPrefs; @Provides @Named("userPrefs") public SharedPreferences providesUserPrefs() @Provides @Named("appPrefs") public SharedPreferences providesAppPrefs() 42
Tests “Tester c’est douter…” 43
Tests - Dagger INUTILE “Si ça compile, ça marche !”
44
Tests - Unitaires public class ApiManager { private final EventBus
bus; private final Api api; @Inject public ApiManager(EventBus bus, Api api) { … } … } 45
Tests - Unitaires @RunWith(MockitoJUnitRunner.class) public class ApiManagerTest { @Mock EventBus
bus; @Mock Api api; @InjectMocks ApiManager apiManager; @Test public should_call_backend() { … } … } 46
Tests - Fonctionnels Pas de bonne solution 47
Tests - Fonctionnels Build variant sur l’Android Gradle Plugin Mock
des modules Dupliquer les configurations 48
Conclusion 49
Merci ! @JeremMartinez 50