<?xml version="1.0" encoding="utf-8"?>

<feed xmlns="http://www.w3.org/2005/Atom">
  <title>Daniel Perez</title>
  <link href="https://danperez.dev/atom.xml" rel="self"/>
  <link href="https://danperez.dev/"/>
  <updated>2026-08-30T15:06:12+00:00</updated>
  <id>https://danperez.dev/</id>
  <author>
    <name>Daniel Perez</name>
  </author>

  <entry>
    <title>A Pragmatic Introduction To Dagger on Android - Why use Dagger?(Part 2)</title>
    <link href="https://danperez.dev/pragmatic-dagger-part-2/"/>
    <updated>2023-09-27T00:00:00+00:00</updated>
    <id>https://danperez.dev/pragmatic-dagger-part-2</id>
    <content type="html">&lt;h1&gt;A Pragmatic Introduction To Dagger on Android Part 2: Setup with Retrofit&lt;/h1&gt;
&lt;p&gt;Note: This article assumes some &lt;a href=&quot;https://square.github.io/retrofit/&quot;&gt;familiarity with Retrofit&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;Welcome back to &quot;A Pragmatic Introduction to Dagger on Android&quot;. &lt;a href=&quot;https://danperez.dev/blog/1&quot;&gt;In the last article&lt;/a&gt; we discussed &lt;em&gt;why&lt;/em&gt; you would
want to use a Dependency Injection framework like Dagger 2. In this article, we&apos;re going to see &lt;em&gt;how&lt;/em&gt; by configuring
a custom &lt;code&gt;OkHttpClient&lt;/code&gt; wired into a &lt;code&gt;Retrofit&lt;/code&gt; instance, which is then used to create a &lt;code&gt;TypicodeService&lt;/code&gt;. Our
dependency chain will look like the following:&lt;/p&gt;
&lt;div class=&quot;language-kotlin highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nc&quot;&gt;TypicodeService&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Retrofit&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;OkHttpClient&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
&lt;p&gt;In this article, we&apos;ll do the following to use Dagger and inject &lt;code&gt;MainActivity&lt;/code&gt; with a &lt;code&gt;TypicodeService&lt;/code&gt;:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Enable kapt&lt;/li&gt;
&lt;li&gt;Configure Dagger 2 with gradle&lt;/li&gt;
&lt;li&gt;Configure a custom &lt;em&gt;Component&lt;/em&gt;&lt;/li&gt;
&lt;li&gt;Configure a custom &lt;code&gt;Application&lt;/code&gt; class&lt;/li&gt;
&lt;li&gt;Set up our dependency chain with Dagger &lt;em&gt;Modules&lt;/em&gt;&lt;/li&gt;
&lt;li&gt;Inject our &lt;code&gt;TypicodeService&lt;/code&gt; into an Activity&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;To start, you&apos;ll need to clone this guides code with the following command:&lt;/p&gt;
&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;git clone --branch part-2-start https://github.com/danielPerez97/pragmatic-dagger.git
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
&lt;p&gt;Once you&apos;ve cloned the code, open it in Android Studio and sync with Gradle.&lt;/p&gt;
&lt;h2&gt;1) Enabling &lt;code&gt;kapt&lt;/code&gt;&lt;/h2&gt;
&lt;p&gt;First, we&apos;ll need to enable &lt;code&gt;kapt&lt;/code&gt; (Kotlin Annotation Processing Tool) in our app-module &lt;code&gt;build.gradle.kts&lt;/code&gt; file.&lt;/p&gt;
&lt;p&gt;In our &lt;code&gt;plugins{}&lt;/code&gt; block, add the following line:&lt;/p&gt;
&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;kotlin(&quot;kapt&quot;)
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
&lt;p&gt;Sync your IDE with Gradle.&lt;/p&gt;
&lt;h2&gt;2) Configure Dagger 2 with Gradle&lt;/h2&gt;
&lt;p&gt;&lt;img src=&quot;https://maven-badges.herokuapp.com/maven-central/com.google.dagger/dagger/badge.svg&quot; alt=&quot;Maven Central&quot; /&gt;&lt;/p&gt;
&lt;p&gt;Add these lines to the &lt;code&gt;dependencies {}&lt;/code&gt; block of your app-module &lt;code&gt;build.gradle.kts&lt;/code&gt; file:&lt;/p&gt;
&lt;div class=&quot;language-kotlin highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;c1&quot;&gt;// Dagger - Use the correct version in the banner above for &quot;2.x&quot;&lt;/span&gt;
&lt;span class=&quot;nf&quot;&gt;implementation&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;com.google.dagger:dagger:2.x&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;nf&quot;&gt;kapt&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;com.google.dagger:dagger-compiler:2.x&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
&lt;p&gt;Sync your IDE with Gradle.&lt;/p&gt;
&lt;h2&gt;3) Configuring a custom Component and Generating Code&lt;/h2&gt;
&lt;p&gt;Next up, we&apos;re going to create something called a Component. Before we do that though, let&apos;s talk about what a
Component is and what it&apos;s used for.&lt;/p&gt;
&lt;p&gt;In the last article, we said this about Dagger:&lt;/p&gt;
&lt;p&gt;&lt;em&gt;&lt;strong&gt;&quot;Is it actually this hard to set up?&quot;&lt;/strong&gt;&lt;/em&gt;&lt;br /&gt;
&lt;em&gt;Yeah, I&apos;d go ahead and say Dagger 2 is a bit hard to set up, but that&apos;s partly because it&apos;s based on 100% code
generation instead of reflection. We make this tradeoff for major performance gains on mobile devices.&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;Compared to something like Spring Dependency Injection where it&apos;s an out-of-the-box solution, Dagger 2 is often
configured manually and requires us to call into the generated code. &lt;em&gt;I still think it&apos;s almost as easy to use as
possible given this constraint because of Components and Annotations.&lt;/em&gt; Almost.*&lt;/p&gt;
&lt;p&gt;A Component is just an interface that &lt;strong&gt;you&lt;/strong&gt; define to begin using Dagger&apos;s generated code. Components are able to
expose dependencies from the Dagger graph directly as well as perform members-injection on other classes. We will
define a component for our Android application, use Dagger-specific annotations on it, then take a look at the
generated code.&lt;/p&gt;
&lt;p&gt;Let&apos;s go ahead and define a &lt;code&gt;TypicodeAppComponent&lt;/code&gt; in the &lt;code&gt;dev.danperez.typicode&lt;/code&gt; example, with nothing
Dagger-specific yet:&lt;/p&gt;
&lt;div class=&quot;language-kotlin highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;c1&quot;&gt;// main/java/dev/danperez/typicode/TypicodeAppComponent.kt&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;package&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;dev.danperez.typicode&lt;/span&gt;

&lt;span class=&quot;kd&quot;&gt;interface&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;TypicodeAppComponent&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;fun&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;inject&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;mainActivity&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;MainActivity&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
&lt;p&gt;We&apos;re going to be using Members Injection on &lt;code&gt;MainActivity&lt;/code&gt;, so the &lt;code&gt;fun inject(mainActivity: MainActivity)&lt;/code&gt;
function is about as simple as we can make it. We won&apos;t provide the implementation for TypicodeAppComponent; instead,
Dagger will generate it for us.&lt;/p&gt;
&lt;p&gt;To make the Dagger Compiler aware of our &lt;code&gt;TypicodeAppComponent&lt;/code&gt;, all we have to do is add the &lt;code&gt;@dagger.Component&lt;/code&gt;
and &lt;code&gt;@javax.inject.Singleton&lt;/code&gt; annotations:&lt;/p&gt;
&lt;div class=&quot;language-kotlin highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;c1&quot;&gt;// main/java/dev/danperez/typicode/TypicodeAppComponent.kt&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;package&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;dev.danperez.typicode&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;dagger.Component&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;javax.inject.Singleton&lt;/span&gt;

&lt;span class=&quot;nd&quot;&gt;@Singleton&lt;/span&gt;
&lt;span class=&quot;nd&quot;&gt;@Component&lt;/span&gt;
&lt;span class=&quot;kd&quot;&gt;interface&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;TypicodeAppComponent&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;fun&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;inject&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;mainActivity&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;MainActivity&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
&lt;p&gt;&lt;code&gt;@Singleton&lt;/code&gt; is an annotation we will use for a feature called scopes. We can use this later to tell Dagger NOT to
create expensive objects over and over again, but to cache it with the lifecycle of the Component.&lt;/p&gt;
&lt;p&gt;In order for Dagger to begin generating code, we&apos;ll need to build the app module. You can use the hammer icon at the
top of Android Studio&apos;s New UI to do this or use the following command:&lt;/p&gt;
&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;./gradlew :app:clean :app:assembleDebug
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
&lt;p&gt;&lt;strong&gt;Note: For this command to work, your JAVA_HOME environment variable MUST point to at least a Java 11 JDK.&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;This will generate &lt;code&gt;DaggerTypicodeAppComponent&lt;/code&gt; at
&lt;code&gt;/app/build/generated/source/kapt/debug/dev/danperez/typicode/DaggerTypicodeAppComponent.java&lt;/code&gt;. You can also view it
below.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Note: Up to this point, all of our code has been in Kotlin. Dagger generates Java code, so be prepared to read
Java code if you&apos;re ever looking at what Dagger generates.&lt;/strong&gt;&lt;/p&gt;
&lt;div class=&quot;language-java highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;c1&quot;&gt;// Generated by Dagger (https://dagger.dev).&lt;/span&gt;
&lt;span class=&quot;kn&quot;&gt;package&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;dev.danperez.typicode&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;;&lt;/span&gt;

&lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;dagger.internal.DaggerGenerated&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;;&lt;/span&gt;

&lt;span class=&quot;nd&quot;&gt;@DaggerGenerated&lt;/span&gt;
&lt;span class=&quot;nd&quot;&gt;@SuppressWarnings&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;({&lt;/span&gt;
    &lt;span class=&quot;s&quot;&gt;&quot;unchecked&quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;s&quot;&gt;&quot;rawtypes&quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;s&quot;&gt;&quot;KotlinInternal&quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;s&quot;&gt;&quot;KotlinInternalInJava&quot;&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;})&lt;/span&gt;
&lt;span class=&quot;kd&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;final&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;DaggerTypicodeAppComponent&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;kd&quot;&gt;private&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;DaggerTypicodeAppComponent&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;

  &lt;span class=&quot;kd&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;static&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Builder&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;builder&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;Builder&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;();&lt;/span&gt;
  &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;

  &lt;span class=&quot;kd&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;static&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;TypicodeAppComponent&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;create&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;Builder&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;().&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;build&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;();&lt;/span&gt;
  &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;

  &lt;span class=&quot;kd&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;static&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;final&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Builder&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;kd&quot;&gt;private&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;Builder&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;

    &lt;span class=&quot;kd&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;TypicodeAppComponent&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;build&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
      &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;TypicodeAppComponentImpl&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;();&lt;/span&gt;
    &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
  &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;

  &lt;span class=&quot;kd&quot;&gt;private&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;static&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;final&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;TypicodeAppComponentImpl&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;implements&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;TypicodeAppComponent&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;kd&quot;&gt;private&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;final&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;TypicodeAppComponentImpl&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;typicodeAppComponentImpl&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;;&lt;/span&gt;

    &lt;span class=&quot;kd&quot;&gt;private&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;TypicodeAppComponentImpl&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;


    &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;

    &lt;span class=&quot;nd&quot;&gt;@Override&lt;/span&gt;
    &lt;span class=&quot;kd&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;inject&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;MainActivity&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;mainActivity&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
  &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
&lt;p&gt;This isn&apos;t doing anything yet, but it might be useful to see how this changes later. Note that it provides a static
&lt;code&gt;create()&lt;/code&gt;method we can use to retrieve our &lt;code&gt;TypicodeAppComponent&lt;/code&gt;. It also has an internal, private
&lt;code&gt;TypicodeAppComponentImpl&lt;/code&gt;, but it&apos;s constructor and &lt;code&gt;inject(MainActivity mainActivity)&lt;/code&gt; don&apos;t have anything in
their bodies.&lt;/p&gt;
&lt;p&gt;Nonetheless, we&apos;ll go ahead and configure this in a custom Application class where we can retrieve it from
Activities, Fragments, Services, etc.&lt;/p&gt;
&lt;h2&gt;4a) Configuring a custom &lt;code&gt;Application&lt;/code&gt; Class&lt;/h2&gt;
&lt;p&gt;The first time I ever saw a custom application class was in the context of learning Dagger, so I&apos;m going to assume
this is your first time seeing it too.&lt;/p&gt;
&lt;p&gt;A custom application class is a class the Android OS will set up for you in place of the default
&lt;code&gt;android.app.Application&lt;/code&gt; class. It&apos;s similar to an activity in that you declare it in the manifest and has similar
lifecycle methods(onCreate(), onDestroy(), etc.) you can hook into. Your custom Application class will be created
before any of the 4 main Android OS components(Activities, BroadcastReceivers, ContentProviders, Services) and will be
destroyed after.&lt;/p&gt;
&lt;p&gt;First, create a new file called &lt;code&gt;TypicodeApplication.kt&lt;/code&gt; in the &lt;code&gt;dev.danperez.typicode&lt;/code&gt; package and use the following
code:&lt;/p&gt;
&lt;div class=&quot;language-kotlin highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;package&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;dev.danperez.typicode&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;android.app.Application&lt;/span&gt;

&lt;span class=&quot;kd&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;TypicodeApplication&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Application&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;lateinit&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;py&quot;&gt;typicodeAppComponent&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;TypicodeAppComponent&lt;/span&gt;
    
    &lt;span class=&quot;k&quot;&gt;override&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;fun&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;onCreate&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;super&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;onCreate&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;typicodeAppComponent&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;DaggerTypicodeAppComponent&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;create&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
&lt;p&gt;Then, to instruct the Android OS to use your custom application class instead of the default one, simply declare a
&lt;code&gt;name&lt;/code&gt; attribute on the &lt;code&gt;&amp;lt;application&amp;gt;&lt;/code&gt; tag in your &lt;code&gt;AndroidManifest.xml&lt;/code&gt;:&lt;/p&gt;
&lt;div class=&quot;language-kotlin highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;application&lt;/span&gt;
        &lt;span class=&quot;o&quot;&gt;..&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;android&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;.TypicodeApplication&quot;&lt;/span&gt;
        &lt;span class=&quot;o&quot;&gt;..&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;&amp;gt;&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;application&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
&lt;h2&gt;4b) Inject MainActivity&lt;/h2&gt;
&lt;p&gt;Now, from our &lt;code&gt;MainActivity&lt;/code&gt;, we&apos;ll be able to look up our &lt;code&gt;TypicodeAppComponent&lt;/code&gt; to retrieve dependencies from the
Dagger graph. Let&apos;s go ahead and try to inject a &lt;code&gt;TypicodeService&lt;/code&gt; by adding the following lines to &lt;code&gt;MainActivity&lt;/code&gt;:&lt;/p&gt;
&lt;div class=&quot;language-kotlin highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;o&quot;&gt;..&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;javax.inject.Inject//&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;&amp;lt;--&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;ADD&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;THIS&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;..&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;
&lt;span class=&quot;kd&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;MainActivity&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;ComponentActivity&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
&lt;span class=&quot;nd&quot;&gt;@Inject&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;lateinit&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;py&quot;&gt;typicodeService&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;TypicodeService&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;// &amp;lt;-- ADD THIS&lt;/span&gt;
&lt;span class=&quot;kd&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;py&quot;&gt;text&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;by&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;mutableStateOf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;Hello!&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;..&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
&lt;p&gt;Try and build the app module like we did before. You should see a problem:&lt;/p&gt;
&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;[Dagger/MissingBinding] dev.danperez.typicode.TypicodeService cannot be provided without an @Provides-annotated method.
public abstract interface TypicodeAppComponent {
                ^
  
  Missing binding usage:
      dev.danperez.typicode.TypicodeService is injected at
          dev.danperez.typicode.MainActivity.typicodeService
      dev.danperez.typicode.MainActivity is injected at
          dev.danperez.typicode.TypicodeAppComponent.inject(dev.danperez.typicode.MainActivity)
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
&lt;p&gt;&lt;code&gt;[Dagger/MissingBinding]&lt;/code&gt; is a well-known error, and it should immediately tell you one thing: Dagger could not find
an object on its graph. This error is saying we&apos;re missing a binding for &lt;code&gt;TypicodeService&lt;/code&gt; which is supposed to be
injected at &lt;code&gt;MainActivity&lt;/code&gt;. In this case, it&apos;s because &lt;em&gt;we never told Dagger to put &lt;code&gt;TypicodeService&lt;/code&gt; on the graph.&lt;/em&gt;
Let&apos;s fix that.&lt;/p&gt;
&lt;h2&gt;5) Set up our dependency chain with Dagger &lt;em&gt;Modules&lt;/em&gt;&lt;/h2&gt;
&lt;p&gt;Let&apos;s revisit our Dependency chain for &lt;code&gt;TypicodeService&lt;/code&gt;:&lt;/p&gt;
&lt;div class=&quot;language-kotlin highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nc&quot;&gt;TypicodeService&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Retrofit&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;OkHttpClient&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
&lt;p&gt;TypicodeService is another interface we will not be generating, Retrofit will. The difference between how Dagger and
Retrofit does this is simple: Dagger uses code generation at compile-time, Retrofit uses reflection at runtime. For
us, this means we now have 2 interfaces whose implementation we won&apos;t be writing out by hand.&lt;/p&gt;
&lt;p&gt;Since TypicodeService has a &lt;em&gt;dependency&lt;/em&gt; on Retrofit, we will need to tell Dagger how to use Retrofit to create
&lt;code&gt;TypicodeService&lt;/code&gt;. For classes and implementations whose code we cannot manually modify, we have to use Dagger modules.&lt;/p&gt;
&lt;p&gt;Dagger Modules are the mechanism to say &quot;hey Dagger, here&apos;s something I&apos;d like for you to stick on your graph. When
you retrieve this type for me, here&apos;s the code you&apos;ll need to run in order to retrieve it.&quot;&lt;/p&gt;
&lt;p&gt;Let&apos;s create a brand new package called &lt;code&gt;di&lt;/code&gt; at &lt;code&gt;dev.danperez.typicode.di&lt;/code&gt;. In this package, create
&lt;code&gt;TypicodeModule.kt&lt;/code&gt; with the following code:&lt;/p&gt;
&lt;div class=&quot;language-kotlin highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;package&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;dev.danperez.typicode.di&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;dagger.Module&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;dagger.Provides&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;dev.danperez.typicode.TypicodeService&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;okhttp3.OkHttpClient&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;retrofit2.Retrofit&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;javax.inject.Singleton&lt;/span&gt;

&lt;span class=&quot;nd&quot;&gt;@Module&lt;/span&gt;
&lt;span class=&quot;kd&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;TypicodeModule&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;nd&quot;&gt;@Provides&lt;/span&gt;
    &lt;span class=&quot;nd&quot;&gt;@Singleton&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;fun&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;provideOkHttpClient&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;():&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;OkHttpClient&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;OkHttpClient&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

    &lt;span class=&quot;nd&quot;&gt;@Provides&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;fun&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;provideRetrofit&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;client&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;OkHttpClient&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Retrofit&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Retrofit&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;Builder&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
            &lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;baseUrl&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;https://jsonplaceholder.typicode.com&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
            &lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;build&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

    &lt;span class=&quot;nd&quot;&gt;@Provides&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;fun&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;provideTypicodeService&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;retrofit&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Retrofit&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;TypicodeService&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;retrofit&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;create&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;TypicodeService&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;class&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;java&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
&lt;p&gt;Here we&apos;re using two new annotations, &lt;code&gt;@Module&lt;/code&gt; and &lt;code&gt;@Provides&lt;/code&gt;. If we look at the source code(&lt;code&gt;CMD + B&lt;/code&gt; or &lt;code&gt;CTRL + B&lt;/code&gt;) for
these, we&apos;ll see these comments on them from the Dagger 2 maintainers:&lt;/p&gt;
&lt;p&gt;&lt;code&gt;@Module&lt;/code&gt;: &quot;Annotates a class that contributes to the object graph.&quot;&lt;/p&gt;
&lt;p&gt;&lt;code&gt;@Provides&lt;/code&gt;: &quot;Annotates methods of a module to create a provider method binding. &lt;strong&gt;The method&apos;s return type is bound
to its returned value.&lt;/strong&gt; The component implementation will pass dependencies to the method as parameters.&quot;&lt;/p&gt;
&lt;p&gt;Essentially, &lt;code&gt;@Module&lt;/code&gt; tells Dagger &quot;Here&apos;s a class/interface that&apos;s going to provide some dependencies for your
graph.&quot; and &lt;code&gt;@Provides&lt;/code&gt; tells Dagger &quot;Here is a specific dependency to add to your graph.&quot; The two go hand in hand
very often.&lt;/p&gt;
&lt;p&gt;Knowing this, we can quickly tell that &lt;code&gt;provideOkHttpClient()&lt;/code&gt; binds an &lt;code&gt;OkHttpClient&lt;/code&gt; type onto the Dagger graph.
&lt;code&gt;provideRetrofit(client: OkHttpClient)&lt;/code&gt; deserves a bit more attention though.&lt;/p&gt;
&lt;p&gt;&lt;code&gt;provideRetrofit(client: OkHttpClient)&lt;/code&gt; actually &lt;em&gt;uses&lt;/em&gt; the Dagger graph to request an &lt;code&gt;OkHttpClient&lt;/code&gt; type. This way,
we can use our &lt;code&gt;OkHttpClient&lt;/code&gt; binding multiple times to create different services throughout our Dagger code (or inject
it into an Activity). If &lt;code&gt;OkHttpClient&lt;/code&gt; wasn&apos;t on the graph already, we would have gotten another &lt;code&gt;[MissingBinding]&lt;/code&gt;
error. And as a bonus, &lt;code&gt;@Singleton&lt;/code&gt; combined with &lt;code&gt;@Provides&lt;/code&gt; tells Dagger, &quot;Create this once and re-use it
everywhere on the graph.&quot; Effectively, this is enforcing the Singleton pattern for us in Dagger&apos;s generated
code.(&lt;a href=&quot;https://www.youtube.com/watch?v=t34AQlblSeE&amp;amp;t=4s&quot;&gt;It&apos;s also good practice to use one OkHttpClient for your entire app too&lt;/a&gt;)&lt;/p&gt;
&lt;p&gt;The final thing we have to do is edit the &lt;code&gt;@Component&lt;/code&gt; annotation on &lt;code&gt;TypicodeAppComponent&lt;/code&gt;to the following:&lt;/p&gt;
&lt;div class=&quot;language-java highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nd&quot;&gt;@Component&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;modules&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;nl&quot;&gt;TypicodeModule:&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;kd&quot;&gt;class&lt;/span&gt;&lt;span class=&quot;err&quot;&gt;])&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
&lt;p&gt;This &quot;installs&quot; our module into the &lt;code&gt;TypicodeAppComponent&lt;/code&gt;. We MUST do this because an application can have multiple
Components, and we have to specify which modules belong to which components.&lt;/p&gt;
&lt;h2&gt;6) Inject MainActivity: Round 2&lt;/h2&gt;
&lt;p&gt;Finally, we can attempt to inject MainActivity. Go ahead and build your app module again.&lt;/p&gt;
&lt;p&gt;Now that we&apos;re past the compile error, we just have to &lt;em&gt;actually inject&lt;/em&gt; &lt;code&gt;MainActivity&lt;/code&gt; by calling Dagger&apos;s
generated code. We&apos;re going to use a very common method to retrieve our Dagger Component: cast the
&lt;code&gt;applicationContext&lt;/code&gt; to our custom Application class, retrieve the &lt;code&gt;Component&lt;/code&gt;, and call the &lt;code&gt;inject()&lt;/code&gt; method. In
our case, it looks like this:&lt;/p&gt;
&lt;div class=&quot;language-kotlin highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;applicationContext&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;as&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;TypicodeApplication&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;typicodeAppComponent&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;inject&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
&lt;p&gt;Call this code &lt;strong&gt;before&lt;/strong&gt; &lt;code&gt;super.onCreate()&lt;/code&gt; in &lt;code&gt;MainActivity&lt;/code&gt;:&lt;/p&gt;
&lt;div class=&quot;language-kotlin highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;package&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;dev.danperez.typicode&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;android.os.Bundle&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;androidx.appcompat.app.AppCompatActivity&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;javax.inject.Inject&lt;/span&gt;

&lt;span class=&quot;kd&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;MainActivity&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;AppCompatActivity&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;nd&quot;&gt;@Inject&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;lateinit&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;py&quot;&gt;typicodeService&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;TypicodeService&lt;/span&gt;
    &lt;span class=&quot;o&quot;&gt;..&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;

    &lt;span class=&quot;k&quot;&gt;override&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;fun&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;onCreate&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;savedInstanceState&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Bundle&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;?)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;application&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;as&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;TypicodeApplication&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;typicodeAppComponent&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;inject&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;// INJECTION&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;super&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;onCreate&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;savedInstanceState&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;nf&quot;&gt;setContentView&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;R&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;layout&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;activity_main&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
        
        &lt;span class=&quot;c1&quot;&gt;// This is a coroutine that runs in the lifecycle scope of the activity. It&apos;s used to perform asynchronous tasks, &lt;/span&gt;
        &lt;span class=&quot;c1&quot;&gt;// like network requests, off the main thread. Here, we&apos;re using it to test our injected TypicodeService by &lt;/span&gt;
        &lt;span class=&quot;c1&quot;&gt;// requesting a post from an API and updating the &apos;text&apos; state with the response.&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;lifecycleScope&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;launch&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;text&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;typicodeService&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;getPost&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;string&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
        &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;o&quot;&gt;..&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
&lt;p&gt;Now, you should be able to use &lt;code&gt;TypicodeService&lt;/code&gt; throughout MainActivity. In the above code, I&apos;ve provided some code
that should display the result of the network request in the UI(it should just be a JSON String).&lt;/p&gt;
&lt;h2&gt;Recap&lt;/h2&gt;
&lt;p&gt;What have we achieved in this article?&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;We set up Dagger in our Gradle build&lt;/li&gt;
&lt;li&gt;Created a custom Dagger Component and viewed generated code&lt;/li&gt;
&lt;li&gt;Configured a custom Application class to provide access to our Dagger Component&lt;/li&gt;
&lt;li&gt;Set up our dependency chain for TypicodeService and reviewed a Dagger &lt;code&gt;MissingBinding&lt;/code&gt; error&lt;/li&gt;
&lt;li&gt;Used Dagger to inject our TypicodeService into MainActivity&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;That&apos;s quite a bit to digest for one article, but I believe that Dagger pays off this upfront complexity with its
early failures and runtime speed. Perhaps at a smaller scale, all this setup isn&apos;t worth it and &lt;a href=&quot;https://github.com/InsertKoinIO/koin&quot;&gt;Koin&lt;/a&gt; might be
a better fit for your project. However, if your project consists of many thousands of lines of code and many, many
modules, Dagger 2 is probably your best choice for validating your dependency graph at compile-time as well as its
speed.&lt;/p&gt;
&lt;h2&gt;Part 3&lt;/h2&gt;
&lt;p&gt;Stay tuned for Part 3 where we&apos;ll discuss how to use the Factory pattern to inject Android ViewModel&apos;s.&lt;/p&gt;
&lt;h2&gt;Notes&lt;/h2&gt;
&lt;h4&gt;&quot;I still think it&apos;s almost as easy to use as possible. Almost.&quot;&lt;/h4&gt;
&lt;p&gt;By this, I mean using Dagger + Anvil to automatically install Modules into the Component. I wish it was built into
Dagger itself.&lt;/p&gt;
</content>
  </entry>

  <entry>
    <title>A Pragmatic Introduction To Dagger on Android - Why use Dagger?(Part 1)</title>
    <link href="https://danperez.dev/pragmatic-dagger-part-1/"/>
    <updated>2023-09-19T00:00:00+00:00</updated>
    <id>https://danperez.dev/pragmatic-dagger-part-1</id>
    <content type="html">&lt;h1&gt;A Pragmatic Introduction To Dagger on Android Part 1: Why use Dagger?&lt;/h1&gt;
&lt;p&gt;Dagger 2 is one of the most infamous tools in Android development and yet one of the most indispensable. Newcomers have
struggled to understand why it&apos;s used, what Dependency Injection is, and how to set Dependency Injection up in their own
projects.
If you are currently struggling with this topic, you may share in some of these thoughts I used to have:&lt;/p&gt;
&lt;p&gt;&lt;em&gt;&lt;strong&gt;&quot;What is Dagger?&quot;&lt;/strong&gt;&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;&lt;em&gt;&lt;strong&gt;&quot;Why would I ever use this?&quot;&lt;/strong&gt;&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;&lt;em&gt;&lt;strong&gt;&quot;Is it actually this hard to set up?&quot;&lt;/strong&gt;&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;In this article, I&apos;ll attempt to answer all of these questions before showing you how to implement Dagger in your own
Android app in Part 2.&lt;/p&gt;
&lt;p&gt;In order to answer &lt;strong&gt;&quot;What is Dagger?&quot;&lt;/strong&gt;, it would be helpful if we learned what Dependency Injection is first.&lt;/p&gt;
&lt;h2&gt;What is Dependency Injection?&lt;/h2&gt;
&lt;p&gt;James Shore &lt;a href=&quot;http://www.jamesshore.com/v2/blog/2006/dependency-injection-demystified&quot;&gt;worded it perfectly&lt;/a&gt; on his blog, &lt;em&gt;&quot;The Art of Agile&quot;&lt;/em&gt;:&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;“Dependency Injection” is a 25-dollar term for a 5-cent concept.
&quot;Dependency Injection&quot; sounds scary, but it refers to an extremely simple concept we&apos;re all familiar with: taking
parameters.&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;No really&lt;/strong&gt;, it&apos;s just taking parameters. Taking parameters in functions, taking parameters in constructors, and
setting mutable members (on a class).&lt;/p&gt;
&lt;p&gt;In the next code sample, there are three demonstrations of different kinds of Dependency Injection:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Constructor Injection&lt;/li&gt;
&lt;li&gt;Method Injection&lt;/li&gt;
&lt;li&gt;Field Injection&lt;/li&gt;
&lt;/ul&gt;
&lt;div class=&quot;language-kotlin highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;c1&quot;&gt;// Constructor Injection&lt;/span&gt;
&lt;span class=&quot;kd&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;NetworkHandler&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kd&quot;&gt;val&lt;/span&gt; &lt;span class=&quot;py&quot;&gt;client&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;OkHttpClient&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;fun&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;doSomething&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;kd&quot;&gt;val&lt;/span&gt; &lt;span class=&quot;py&quot;&gt;client&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;OkHttpClient&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
    &lt;span class=&quot;kd&quot;&gt;val&lt;/span&gt; &lt;span class=&quot;py&quot;&gt;networkHandler&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;NetworkHandler&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;client&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;// Here we &quot;inject&quot; an OkHttpClient by passing a parameter to the constructor&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;// Method Injection&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;fun&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;setPet&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;pet&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Pet&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;pet&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;pet&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;// Members Injection&lt;/span&gt;
&lt;span class=&quot;kd&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;MyActivity&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;lateinit&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;py&quot;&gt;okHttpClient&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;OkHttpClient&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;// &amp;lt;- Public, mutable member&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;kd&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Injector&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kd&quot;&gt;val&lt;/span&gt; &lt;span class=&quot;py&quot;&gt;appOkHttpClient&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;OkHttpClient&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;fun&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;injectMyActivity&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;activity&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;MyActivity&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;activity&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;okHttpClient&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;okHttpClient&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;// &amp;lt;- Here we set the member, or &quot;inject&quot; it&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
&lt;p&gt;These are all examples of Dependency Injection in object-oriented programming.&lt;/p&gt;
&lt;h2&gt;Okay, but, &lt;em&gt;why would I use this pattern?&lt;/em&gt; What are we getting out of it?&lt;/h2&gt;
&lt;p&gt;Dependency Injection has quite a few benefits when applied throughout a codebase, but testing is one of the most talked
about especially with discussion around Dagger.&lt;/p&gt;
&lt;p&gt;&lt;em&gt;&lt;strong&gt;Note: From this point forward I will shorthand Dependency Injection with &quot;DI&quot;.&lt;/strong&gt;&lt;/em&gt;&lt;/p&gt;
&lt;h3&gt;Testing&lt;/h3&gt;
&lt;p&gt;Using DI consistently helps us write testable code. Suppose you have the following class and interface:&lt;/p&gt;
&lt;div class=&quot;language-kotlin highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;c1&quot;&gt;// Pet.kt&lt;/span&gt;
&lt;span class=&quot;kd&quot;&gt;data class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Pet&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kd&quot;&gt;val&lt;/span&gt; &lt;span class=&quot;py&quot;&gt;id&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Long&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;val&lt;/span&gt; &lt;span class=&quot;py&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;String&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;// PetDataSource.kt&lt;/span&gt;
&lt;span class=&quot;kd&quot;&gt;interface&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;PetDataSource&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;fun&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;getPets&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;():&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;List&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;Pet&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
&lt;p&gt;Nothing special yet, but here&apos;s how we might use it in a &lt;code&gt;PetListPresenter&lt;/code&gt; class:&lt;/p&gt;
&lt;div class=&quot;language-kotlin highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;c1&quot;&gt;// PetListPresenter.kt&lt;/span&gt;
&lt;span class=&quot;kd&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;PetListPresenter&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kd&quot;&gt;val&lt;/span&gt; &lt;span class=&quot;py&quot;&gt;petDataSource&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;PetDataSource&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;// Constructor Injection&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;fun&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;getPetByid&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;id&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Long&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Pet&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;?&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;petDataSource&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;getPets&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;().&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;find&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;it&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;id&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;id&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;    
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;


&lt;span class=&quot;c1&quot;&gt;// SQLitePetDataSource.kt&lt;/span&gt;
&lt;span class=&quot;kd&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;SQLitePetDataSource&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kd&quot;&gt;val&lt;/span&gt; &lt;span class=&quot;py&quot;&gt;petDb&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;PetDb&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;PetDataSource&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;override&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;fun&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;getPets&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;():&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;List&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;Pet&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;o&quot;&gt;..&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;pets&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;// PetActivity.kt&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;..&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;
&lt;span class=&quot;kd&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;PetActivity&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;AppCompatActivity&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;override&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;fun&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;onCreate&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;super&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;onCreate&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
        &lt;span class=&quot;kd&quot;&gt;val&lt;/span&gt; &lt;span class=&quot;py&quot;&gt;sqlitePetDataSource&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;SQLitePetDataSource&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;db&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;context&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;getDatabase&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;())&lt;/span&gt;
        &lt;span class=&quot;kd&quot;&gt;val&lt;/span&gt; &lt;span class=&quot;py&quot;&gt;petListPresenter&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;PetListPresenter&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;petDataSource&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;sqlitePetDataSource&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;// Using constructor injection to satisfy the PetDataSource parameter in production code&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

    &lt;span class=&quot;k&quot;&gt;fun&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Context&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;getDatabase&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;():&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;PetDb&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;o&quot;&gt;..&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;db&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
&lt;p&gt;Still, nothing special is happening yet. All we did was abstract away &lt;em&gt;where&lt;/em&gt; the data is coming from. We could have
just as easily used a &lt;code&gt;NetworkPetDataSource.kt&lt;/code&gt; to retrieve our pets. However, when it comes time to
test &lt;code&gt;PetListPresenter&lt;/code&gt;, this
pattern has a clear benefit: we can pass fakes/mocks into our object under test quickly, especially if we leverage
Kotlin&apos;s &lt;code&gt;object&lt;/code&gt;
keyword.&lt;/p&gt;
&lt;div class=&quot;language-kotlin highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;c1&quot;&gt;// TestPetListPresenter.kt&lt;/span&gt;

&lt;span class=&quot;kd&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;TestPetListPresenter&lt;/span&gt;                                                      
&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;private&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;lateinit&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;py&quot;&gt;petListPresenter&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;PetListPresenter&lt;/span&gt;
    
    &lt;span class=&quot;nd&quot;&gt;@Before&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;fun&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;setup&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;kd&quot;&gt;val&lt;/span&gt; &lt;span class=&quot;py&quot;&gt;fakePetDataSource&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;PetDataSource&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;object&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;PetDataSource&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
            &lt;span class=&quot;k&quot;&gt;override&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;fun&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;getPets&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;():&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;List&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;Pet&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
                &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;listOf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;Pet&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;id&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;name&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;Wheatley&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
            &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;   
        &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
        
        &lt;span class=&quot;n&quot;&gt;petListPresenter&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;PetListPresenter&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;fakePetDataSource&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;// Using constructor injection to satisfy the PetDataSource parameter in test code&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

    &lt;span class=&quot;nd&quot;&gt;@Test&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;fun&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;testGetPetById&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;kd&quot;&gt;val&lt;/span&gt; &lt;span class=&quot;py&quot;&gt;pet&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;petListPresenter&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;getPetById&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;nf&quot;&gt;assertTrue&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;pet&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;!=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;null&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;nf&quot;&gt;assertEquals&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;Wheatley&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;pet&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
&lt;p&gt;Since our &lt;code&gt;PetListPresenter&lt;/code&gt; has no idea where the data is coming from (or knowledge of the implementation), we were
able to spin up a &lt;code&gt;PetDataSource&lt;/code&gt; on the fly for our test. We have now used the DI pattern to make our testing
significantly
easier. Consider the case where &lt;code&gt;PetListDataSource&lt;/code&gt; had defined &lt;code&gt;SQLitePetDataSource&lt;/code&gt; as a constructor parameter:&lt;/p&gt;
&lt;div class=&quot;language-kotlin highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;c1&quot;&gt;// PetListPresenter.kt&lt;/span&gt;
&lt;span class=&quot;kd&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;PetListPresenter&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kd&quot;&gt;val&lt;/span&gt; &lt;span class=&quot;py&quot;&gt;petDataSource&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;SQLitePetDataSource&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
&lt;p&gt;In this case, our test would have had to do a bit more configuration to satisfy the constructor parameter
for &lt;code&gt;SQLitePetDataSource&lt;/code&gt;, which might involve setting up an SQLite database on the file system then making sure we
delete it when the tests are done so we don&apos;t waste computer resources. Yuck.&lt;/p&gt;
&lt;h2&gt;I see. Dependency Injection is just a software pattern. But how come people use Dagger?&lt;/h2&gt;
&lt;p&gt;Dagger is a DI &lt;em&gt;framework&lt;/em&gt;. It helps you facilitate this pattern in your code by building a graph of your dependencies,
calling the constructors &lt;em&gt;for you&lt;/em&gt;,
and getting your objects exactly where you need them. In the above example, if one had used Dagger, our &lt;code&gt;PetActivity&lt;/code&gt;
would have looked like this:&lt;/p&gt;
&lt;div class=&quot;language-kotlin highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kd&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;PetActivity&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;AppCompatActivity&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;nd&quot;&gt;@Inject&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;lateinit&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;py&quot;&gt;petListPresenter&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;PetListPresenter&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;// Public mutable member that gets injected later &lt;/span&gt;
    
    &lt;span class=&quot;k&quot;&gt;override&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;fun&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;onCreate&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;nf&quot;&gt;getInjector&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;().&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;inject&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;// INJECTION&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;super&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;onCreate&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
        &lt;span class=&quot;c1&quot;&gt;// You can use petListPresenter anywhere you need to now, as long as onCreate() was called&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
    
    &lt;span class=&quot;k&quot;&gt;fun&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;getInjector&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;():&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Injector&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;o&quot;&gt;..&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;injector&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
&lt;p&gt;Take note of the &lt;strong&gt;INJECTION&lt;/strong&gt; comment and recall members injection from earlier.
That &lt;code&gt;Injector#inject(activity: PetActivity)&lt;/code&gt; is
doing something like this under the hood:&lt;/p&gt;
&lt;div class=&quot;language-kotlin highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;c1&quot;&gt;// Injector.kt. Dagger will generate similar code to this but in Java.&lt;/span&gt;
&lt;span class=&quot;kd&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Injector&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;..&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.)&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;o&quot;&gt;..&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;fun&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;inject&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;activity&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;PetActivity&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;c1&quot;&gt;// Note: sqlitePetDataSource could be a member of this Injector class, or it might be provided by a factory&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;activity&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;petListPresenter&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;PetListPresenter&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sqlitePetDataSource&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;// &amp;lt;- Here we set the member, or &quot;inject&quot; it&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
&lt;p&gt;Let&apos;s pause for a moment and look at our chain of dependencies for &lt;code&gt;PetListPresenter&lt;/code&gt;:&lt;/p&gt;
&lt;p&gt;&lt;code&gt;PetListPresenter&lt;/code&gt; -&amp;gt; &lt;code&gt;SQLitePetDataSource&lt;/code&gt; -&amp;gt; &lt;code&gt;PetDb&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;Dagger takes care of this entire chain for you so your &lt;code&gt;PetActivity&lt;/code&gt; doesn&apos;t have to care about &lt;code&gt;PetDb&lt;/code&gt; and
&lt;code&gt;SQLitePetDataSource&lt;/code&gt;. It can focus entirely on &lt;code&gt;PetListPresenter&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;Satisfying dependency chains so you can focus on only the object you need is what DI frameworks are great for.&lt;/p&gt;
&lt;h2&gt;Okay, so Dagger is a DI Framework. But do we have to call &lt;code&gt;getInjector().inject(this)&lt;/code&gt; all the time?&lt;/h2&gt;
&lt;p&gt;Yes. It is imperative that we call Dagger&apos;s generated code &lt;em&gt;somewhere&lt;/em&gt; for every object we&apos;d like to retrieve from
the Dagger graph.* In the case of Members Injection, something called a &lt;code&gt;MembersInjector&lt;/code&gt; gets generated for every
object whose members are annotated with &lt;code&gt;@Inject&lt;/code&gt;. We must call this generated &lt;code&gt;MembersInjector&lt;/code&gt; in order to inject
our objects.&lt;/p&gt;
&lt;p&gt;In the case of Android Framework types like Activity, you cannot use constructor injection** because the Android OS will
be creating these objects, not Dagger. Hence, Members Injection becomes the next best option.&lt;/p&gt;
&lt;h2&gt;Reflection Vs Code Generation&lt;/h2&gt;
&lt;p&gt;It isn&apos;t all sunshine and rainbows. One tradeoff for code generation vs reflection is that we have to call the generated
code somewhere instead of a more automatic-feeling solution, like Guice or Spring Dependency Injection.&lt;/p&gt;
&lt;p&gt;We also cannot inject private members using code generation, but reflection can. In some ways, Dagger 2 can be harder to
use
than a reflection-based DI framework. However, these sacrifices afford us an extremely fast dependency injector that&apos;s
appropriate for Android.&lt;/p&gt;
&lt;h2&gt;Wrapping up&lt;/h2&gt;
&lt;p&gt;Let&apos;s revisit those 3 questions from the beginning of the article and give an answer for each of them now:&lt;/p&gt;
&lt;p&gt;&lt;em&gt;&lt;strong&gt;&quot;What is Dagger?&quot;&lt;/strong&gt;&lt;/em&gt;&lt;br /&gt;
Dagger is a Dependency Injection Framework, and there are more like it including Guice and Spring Dependency Injection.&lt;/p&gt;
&lt;p&gt;&lt;em&gt;&lt;strong&gt;&quot;Why would I ever use this?&quot;&lt;/strong&gt;&lt;/em&gt;&lt;br /&gt;
To help you facilitate the Dependency Injection pattern in your code, which helps you test and decouple your code.&lt;/p&gt;
&lt;p&gt;&lt;em&gt;&lt;strong&gt;&quot;Is it actually this hard to set up?&quot;&lt;/strong&gt;&lt;/em&gt;&lt;br /&gt;
Yeah, I&apos;d go ahead and say Dagger 2 is a bit hard to set up, but that&apos;s partly because it&apos;s based on 100% code
generation instead of reflection. We make this tradeoff for major performance gains on mobile devices.&lt;/p&gt;
&lt;h2&gt;Part 2&lt;/h2&gt;
&lt;p&gt;Stay tuned for Part 2 where we&apos;ll discuss how to configure Dagger 2 in an Android app.&lt;/p&gt;
&lt;h2&gt;Notes&lt;/h2&gt;
&lt;h4&gt;&quot;Yes. It is imperative that we call Dagger&apos;s generated code&quot; &lt;em&gt;somewhere&lt;/em&gt; for every object we&apos;d like to retrieve from the Dagger graph.&quot;&lt;/h4&gt;
&lt;ul&gt;
&lt;li&gt;This isn&apos;t to say we can&apos;t be more clever about calling our &lt;code&gt;MembersInjector&lt;/code&gt;&apos;s. &lt;a href=&quot;https://slack.engineering/extending-anvil-for-fun-and-profit/&quot;&gt;Slack largely hid this&lt;/a&gt; by using
an extension to Dagger called &lt;a href=&quot;https://github.com/square/anvil&quot;&gt;Anvil&lt;/a&gt;.&lt;/li&gt;
&lt;/ul&gt;
&lt;h4&gt;&quot;In the case of Android Framework types like Activity, you cannot use constructor injection...&quot;&lt;/h4&gt;
&lt;ul&gt;
&lt;li&gt;If you&apos;re using &lt;code&gt;minSdk=28&lt;/code&gt;, you can use &lt;a href=&quot;https://developer.android.com/reference/android/app/AppComponentFactory&quot;&gt;&lt;code&gt;AppComponentFactory&lt;/code&gt;&lt;/a&gt; and &lt;a href=&quot;https://developer.android.com/reference/android/app/AppComponentFactory&quot;&gt;&lt;code&gt;FragmentFactory&lt;/code&gt;&lt;/a&gt; to use constructor
injection on Activities, Fragments, Services, Content Providers, etc. This will require a feature we&apos;ll discuss in the
future called Multibindings.&lt;/li&gt;
&lt;/ul&gt;
</content>
  </entry>

  <entry>
    <title>What makes Dagger2 Great</title>
    <link href="https://danperez.dev/what-makes-dagger2-great/"/>
    <updated>2023-09-18T00:00:00+00:00</updated>
    <id>https://danperez.dev/what-makes-dagger2-great</id>
    <content type="html">&lt;h2&gt;Okay, so Dagger is a DI Framework. But do we have to call &lt;code&gt;getInjector().inject(this)&lt;/code&gt; all the time?&lt;/h2&gt;
&lt;p&gt;Unfortunately, we do.* Lets take a look at the history of DI frameworks in Java to understand why.&lt;/p&gt;
&lt;p&gt;Once upon a time, Dependency Injection frameworks like &lt;a href=&quot;2&quot;&gt;Guice&lt;/a&gt;(created by Bob Lee) used reflection to achieve their DI. At
the time, these DI frameworks weren&apos;t built with mobile devices in mind, they were built for servers. Reflection has a runtime
cost in performance, but it&apos;s an appropriate tradeoff for a server environment. When mobile devices came along, this was
no longer the case and led to app slowdowns. We needed a DI framework that was blazing fast for mobile devices but used less
or no reflection.&lt;/p&gt;
&lt;p&gt;&lt;a href=&quot;3&quot;&gt;Dagger 1&lt;/a&gt;, originally created by Square(Block now), used a mixture of code generation and reflection to achieve DI. At
compile-time, the injecting code was generated by using annotations (@Inject) in your own code that the Dagger compiler would look at
and process. It worked, but the reflection that was hanging around left a desire for more. What if we could make a DI framework
that completely ignored reflection and just used code generation at compile-time? This would also yield a major advantage for
Dagger 2: failing early. When a dependency couldn&apos;t be satisfied(it wasn&apos;t on the DI frameworks graph), developers would
find out at runtime what had happened instead of compile-time. Early failures lead to faster fixes.&lt;/p&gt;
&lt;p&gt;Thus, Dagger 2 was born. Dagger 2 &lt;em&gt;skipped reflection entirely&lt;/em&gt;. Instead of making your users pay a performance cost, our
Gradle builds would eat the cost in build time and generate injecting code at compile time. It was now possible to use a
Dependency Injection framework without reflection.&lt;/p&gt;
&lt;p&gt;This made DI with the assistance of a framework &lt;em&gt;blazing fast&lt;/em&gt; and appropriate for mobile. We could have clean code without
having to call a ton of constructors ourselves. But &lt;em&gt;we have to hook into that generated code somewhere.&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;So, yes, we do have to call &lt;code&gt;getInjector().inject(this)&lt;/code&gt; because this is how we hook into Dagger&apos;s
generated code. I&apos;m skipping a lot for the sake of brevity but this is generally how injecting Fragments and Activities
on Android goes. Since we can&apos;t have custom constructors for Fragments and Activities(unless you&apos;re using minSdk 28) either,
this means we have to use Members Injection(not preferred).&lt;/p&gt;
</content>
  </entry>

</feed>
