<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
xmlns:content="http://purl.org/rss/1.0/modules/content/"
xmlns:dc="http://purl.org/dc/elements/1.1/"
xmlns:atom="http://www.w3.org/2005/Atom"
xmlns:sy="http://purl.org/rss/1.0/modules/syndication/">
	<channel>
		<title>Stop Holding Dagger by the Blade: Use the Hilt | Infinum</title>
		<atom:link href="https://infinum.com/blog/dagger-hilt/feed/" rel="self" type="application/rss+xml" />
		<link>https://infinum.com/blog/dagger-hilt/</link>
		<description>Building digital products</description>
		<lastBuildDate>Mon, 04 May 2026 14:05:18 +0000</lastBuildDate>
		<sy:updatePeriod>hourly</sy:updatePeriod>
		<sy:updateFrequency>1</sy:updateFrequency>

					<item>
				<image>
					<url>7924https://infinum.com/uploads/2020/11/dagger-hilt-0.webp</url>
				</image>
				<title>Stop Holding Dagger by the Blade: Use the Hilt</title>
				<link>https://infinum.com/blog/dagger-hilt/</link>
				<pubDate>Thu, 03 Dec 2020 17:23:00 +0000</pubDate>
				<dc:creator>Stjepan Banek</dc:creator>
				<guid isPermaLink="false">https://infinum.com/the-capsized-eight/dagger-hilt/</guid>
				<description>
					<![CDATA[<p>Hilt will make your Dagger setup way more readable and much easier to maintain.</p>
<p>The post <a href="https://infinum.com/blog/dagger-hilt/">Stop Holding Dagger by the Blade: Use the Hilt</a> appeared first on <a href="https://infinum.com">Infinum</a>.</p>
]]>
				</description>
				<content:encoded>
					<![CDATA[<div
	class="wrapper"
	data-id="es-217"
	 data-animation-target='inner-items'>
		
			<div class="wrapper__inner">
			<div class="block-blog-content js-block-blog-content">
	
<div class="block-blog-content-sidebar" data-id="es-92">
	</div>

<div class="block-blog-content-main">
	
<div
	class="wrapper wrapper__use-simple--true"
	data-id="es-95"
	 data-animation='slideFade' data-animation-target='inner-items'>
		
			<div class="block-paragraph" data-id="es-93">
	<p	class='typography typography--size-16-text-roman js-typography block-paragraph__paragraph'
	data-id='es-94'
	>
	Hilt is an Android library that reduces stress levels and stabilizes the blood pressure while using Dagger. If you’ve used Dagger before, we can agree that it has a steep learning curve, a long setup process and often hardly understandable errors.</p></div>	</div>

<div
	class="wrapper wrapper__use-simple--true"
	data-id="es-98"
	 data-animation='slideFade' data-animation-target='inner-items'>
		
			<div class="block-paragraph" data-id="es-96">
	<p	class='typography typography--size-16-text-roman js-typography block-paragraph__paragraph'
	data-id='es-97'
	>
	In some ways, Hilt is a remedy for all three problems.</p></div>	</div>

<div
	class="wrapper wrapper__use-simple--true"
	data-id="es-101"
	 data-animation='slideFade' data-animation-target='inner-items'>
		
			<div class="block-heading" data-id="es-99">
	<h2	class='typography typography--size-52-default js-typography block-heading__heading'
	data-id='es-100'
	>
	Getting ready to migrate from Dagger to Hilt</h2></div>	</div>

<div
	class="wrapper wrapper__use-simple--true"
	data-id="es-104"
	 data-animation='slideFade' data-animation-target='inner-items'>
		
			<div class="block-paragraph" data-id="es-102">
	<p	class='typography typography--size-16-text-roman js-typography block-paragraph__paragraph'
	data-id='es-103'
	>
	It’s developed by Google and at the time of writing this blog post, it’s still in the alpha stage. So please check for the latest version before using it in production. It looks very promising! Judging by the official documentation page already being full of examples, we can expect a powerful and stable tool soon. Make sure to check the <a href="https://developer.android.com/training/dependency-injection/hilt-android">official documentation</a>.</p></div>	</div>

<div
	class="wrapper wrapper__use-simple--true"
	data-id="es-107"
	 data-animation='slideFade' data-animation-target='inner-items'>
		
			<div class="block-paragraph" data-id="es-105">
	<p	class='typography typography--size-16-text-roman js-typography block-paragraph__paragraph'
	data-id='es-106'
	>
	Hilt uses annotations to generate some parts of the dependency structure that you would normally have to write yourself. For example: <code>@HiltAndroidApp</code> will generate <code>ApplicationComponent</code>, <code>ApplicationModule</code>, <code>ActivityBuilderComponent</code> etc. This annotation is a starter pack solution for Dagger setup.</p></div>	</div>

<div
	class="wrapper wrapper__use-simple--true"
	data-id="es-110"
	 data-animation='slideFade' data-animation-target='inner-items'>
		
			<div class="block-paragraph" data-id="es-108">
	<p	class='typography typography--size-16-text-roman js-typography block-paragraph__paragraph'
	data-id='es-109'
	>
	Since the topic of Hilt has already been covered in the documentation, this post will focus on migrating an example project from Dagger to Hilt, so I will assume that you are already familiar with Dagger.</p></div>	</div>

<div
	class="wrapper wrapper__use-simple--true"
	data-id="es-113"
	 data-animation='slideFade' data-animation-target='inner-items'>
		
			<div class="block-paragraph" data-id="es-111">
	<p	class='typography typography--size-16-text-roman js-typography block-paragraph__paragraph'
	data-id='es-112'
	>
	Note this before we begin: It’s highly recommended that you do the migration all at once. Having both Hilt and Dagger (the old way) in your app will result in large overhead as Dagger and Hilt generate their dependency graphs separately. Also, each of your modules must be included in some Hilt component, or else the application won’t build.</p></div>	</div>

<div
	class="wrapper wrapper__use-simple--true"
	data-id="es-116"
	 data-animation='slideFade' data-animation-target='inner-items'>
		
			<div class="block-heading" data-id="es-114">
	<h2	class='typography typography--size-52-default js-typography block-heading__heading'
	data-id='es-115'
	>
	Step 1: Add HiltAndroidApp annotation</h2></div>	</div>

<div
	class="wrapper wrapper__use-simple--true"
	data-id="es-119"
	 data-animation='slideFade' data-animation-target='inner-items'>
		
			<div class="block-paragraph" data-id="es-117">
	<p	class='typography typography--size-16-text-roman js-typography block-paragraph__paragraph'
	data-id='es-118'
	>
	The application component is usually the entry point for Dagger dependency graph. Dependencies that are included here will be available through the whole application. This is an example of an application setup with Dagger.</p></div>	</div>

<div
	class="wrapper wrapper__use-simple--true"
	data-id="es-121"
	 data-animation='slideFade' data-animation-target='inner-items'>
		
			<div class="block-code">
	<pre class="phiki language-kotlin github-light" data-language="kotlin" style="background-color: #fff;color: #24292e;"><code><span class="line"><span class="token" style="color: #6a737d;">//</span><span class="token" style="color: #6a737d;"> Application Component class</span><span class="token">
</span></span><span class="line"><span class="token" style="color: #6f42c1;">@Component</span><span class="token">(
</span></span><span class="line"><span class="token">   modules </span><span class="token" style="color: #d73a49;">=</span><span class="token"> [
</span></span><span class="line"><span class="token">       AndroidSupportInjectionModule</span><span class="token">::</span><span class="token" style="color: #6f42c1;">class</span><span class="token">,
</span></span><span class="line"><span class="token">       DatabaseModule</span><span class="token">::</span><span class="token" style="color: #6f42c1;">class</span><span class="token">,
</span></span><span class="line"><span class="token">       FragmentBuilderModule</span><span class="token">::</span><span class="token" style="color: #6f42c1;">class</span><span class="token">,
</span></span><span class="line"><span class="token">       ActivityBuilderModule</span><span class="token">::</span><span class="token" style="color: #6f42c1;">class</span><span class="token">
</span></span><span class="line"><span class="token">   ]
</span></span><span class="line"><span class="token">)
</span></span><span class="line"><span class="token" style="color: #6f42c1;">@Singleton</span><span class="token">
</span></span><span class="line"><span class="token">interface AppComponent</span><span class="token">: </span><span class="token" style="color: #6f42c1;">AndroidInjector</span><span class="token">&lt;</span><span class="token" style="color: #6f42c1;">ExampleApplication</span><span class="token">&gt; </span><span class="token">{
</span></span><span class="line"><span class="token">
</span></span><span class="line"><span class="token">   </span><span class="token" style="color: #6f42c1;">@Component.Builder</span><span class="token">
</span></span><span class="line"><span class="token">   </span><span class="token">interface Builder </span><span class="token">{
</span></span><span class="line"><span class="token">       </span><span class="token" style="color: #6f42c1;">@BindsInstance</span><span class="token">
</span></span><span class="line"><span class="token">       </span><span class="token">fun application</span><span class="token">(application</span><span class="token">: </span><span class="token" style="color: #6f42c1;">ExampleApplication</span><span class="token">)</span><span class="token">: </span><span class="token" style="color: #6f42c1;">Builder</span><span class="token">
</span></span><span class="line"><span class="token">
</span></span><span class="line"><span class="token">       </span><span class="token">fun build</span><span class="token">()</span><span class="token">: </span><span class="token" style="color: #6f42c1;">AppComponent</span><span class="token">
</span></span><span class="line"><span class="token">   }
</span></span><span class="line"><span class="token">}
</span></span><span class="line"><span class="token">
</span></span><span class="line"><span class="token" style="color: #d73a49;">..</span><span class="token">.
</span></span><span class="line"><span class="token" style="color: #6a737d;">//</span><span class="token" style="color: #6a737d;"> Application class</span><span class="token">
</span></span><span class="line"><span class="token">class ExampleApplication </span><span class="token">: </span><span class="token" style="color: #6f42c1;">DaggerApplication</span><span class="token">() {
</span></span><span class="line"><span class="token">   </span><span class="token" style="color: #d73a49;">override</span><span class="token"> </span><span class="token">fun applicationInjector</span><span class="token">()</span><span class="token">: </span><span class="token" style="color: #6f42c1;">AndroidInjector</span><span class="token">&lt;</span><span class="token" style="color: #6f42c1;">out</span><span class="token"> </span><span class="token" style="color: #6f42c1;">DaggerApplication</span><span class="token">&gt; </span><span class="token">{
</span></span><span class="line"><span class="token">       </span><span class="token" style="color: #d73a49;">return</span><span class="token"> DaggerAppComponent</span><span class="token">.builder</span><span class="token">()</span><span class="token">.application</span><span class="token">(</span><span class="token" style="color: #005cc5;">this</span><span class="token">)</span><span class="token">.build</span><span class="token">()
</span></span><span class="line"><span class="token">   }
</span></span><span class="line"><span class="token">}
</span></span><span class="line"><span class="token">
</span></span></code></pre></div>	</div>

<div
	class="wrapper wrapper__use-simple--true"
	data-id="es-124"
	 data-animation='slideFade' data-animation-target='inner-items'>
		
			<div class="block-paragraph" data-id="es-122">
	<p	class='typography typography--size-16-text-roman js-typography block-paragraph__paragraph'
	data-id='es-123'
	>
	Along with the Application component, it is common practice to also make an Application context module and Fragment and Activity builder modules for your application.</p></div>	</div>

<div
	class="wrapper wrapper__use-simple--true"
	data-id="es-126"
	 data-animation='slideFade' data-animation-target='inner-items'>
		
			<div class="block-code">
	<pre class="phiki language-kotlin github-light" data-language="kotlin" style="background-color: #fff;color: #24292e;"><code><span class="line"><span class="token" style="color: #6f42c1;">@Module</span><span class="token">
</span></span><span class="line"><span class="token">class ApplicationModule </span><span class="token">{
</span></span><span class="line"><span class="token">
</span></span><span class="line"><span class="token">   </span><span class="token" style="color: #6f42c1;">@Provides</span><span class="token">
</span></span><span class="line"><span class="token">   </span><span class="token" style="color: #6f42c1;">@Singleton</span><span class="token">
</span></span><span class="line"><span class="token">   </span><span class="token">fun provideApplicationContext</span><span class="token">(application</span><span class="token">: </span><span class="token" style="color: #6f42c1;">ExampleApplication</span><span class="token">)</span><span class="token">: </span><span class="token" style="color: #6f42c1;">Context</span><span class="token">   
</span></span><span class="line"><span class="token">       </span><span class="token" style="color: #d73a49;">=</span><span class="token"> application.applicationContext
</span></span><span class="line"><span class="token">
</span></span><span class="line"><span class="token">
</span></span><span class="line"><span class="token" style="color: #6f42c1;">@Module</span><span class="token">
</span></span><span class="line"><span class="token">interface FragmentBuilderModule </span><span class="token">{
</span></span><span class="line"><span class="token">
</span></span><span class="line"><span class="token">   </span><span class="token" style="color: #6a737d;">/**</span><span class="token" style="color: #6a737d;">
</span></span><span class="line"><span class="token" style="color: #6a737d;">    * Add bind function for every fragment
</span></span><span class="line"><span class="token" style="color: #6a737d;">    * with @ContributesAndroidInjector annotation
</span></span><span class="line"><span class="token" style="color: #6a737d;">    * so that the fragment can be injected with dependencies
</span></span><span class="line"><span class="token" style="color: #6a737d;">    </span><span class="token" style="color: #6a737d;">*/</span><span class="token">
</span></span><span class="line"><span class="token">   </span><span class="token" style="color: #6f42c1;">@ContributesAndroidInjector</span><span class="token">(modules </span><span class="token" style="color: #d73a49;">=</span><span class="token"> [FriendsModule</span><span class="token">::</span><span class="token" style="color: #6f42c1;">class</span><span class="token">])
</span></span><span class="line"><span class="token">   </span><span class="token">fun bindFriendsFragment</span><span class="token">()</span><span class="token">: </span><span class="token" style="color: #6f42c1;">FriendsFragment</span><span class="token">
</span></span><span class="line"><span class="token">}
</span></span><span class="line"><span class="token">
</span></span></code></pre></div>	</div>

<div
	class="wrapper wrapper__use-simple--true"
	data-id="es-129"
	 data-animation='slideFade' data-animation-target='inner-items'>
		
			<div class="block-paragraph" data-id="es-127">
	<p	class='typography typography--size-16-text-roman js-typography block-paragraph__paragraph'
	data-id='es-128'
	>
	Then you would make a module with specific dependencies for every fragment or activity and so on.</p></div>	</div>

<div
	class="wrapper wrapper__use-simple--true"
	data-id="es-134"
	 data-animation='slideFade' data-animation-target='inner-items'>
		
			<div class="blockquote block-blockquote__blockquote" data-id="es-130">
	
	<div class="blockquote__content">
		<i
	class="icon blockquote__icon icon--size-16 icon--scale-100"
	 aria-hidden='true' data-name='blockquote-24' data-id='es-131'>
	<svg fill='none' height='24' viewBox='0 0 24 24' width='24' xmlns='http://www.w3.org/2000/svg'><path clip-rule='evenodd' d='m12 24c6.6274 0 12-5.3726 12-12 0-2.79685-.9568-5.37021-2.561-7.41062-.581.22951-1.0832.60583-1.5069 1.12898-.5132.60844-.7698 1.41969-.7698 2.43375v.07605h2.5789v5.59004h-5.6197v-5.01962c0-1.11547.154-2.06616.4619-2.85205.3336-.81125.757-1.48307 1.2702-2.01545.528-.52161 1.1175-.92155 1.7687-1.1998-2.0728-1.70651-4.7279-2.73128-7.6223-2.73128-6.62742 0-12 5.37258-12 12 0 6.6274 5.37258 12 12 12zm-3.53811-18.05347c-.30793.78589-.46189 1.73658-.46189 2.85205v5.01962h5.6197v-5.59004h-2.5789v-.07605c0-1.01406.2566-1.82531.7698-2.43375.5389-.63379 1.1804-1.05209 1.9245-1.2549v-2.28164c-.7441.07605-1.4626.25351-2.1555.53238-.6928.27887-1.3086.68449-1.84752 1.21688-.51321.53238-.9366 1.2042-1.27019 2.01545z' fill='currentColor' fill-rule='evenodd'/></svg></i><p	class='typography typography--size-36-text js-typography blockquote__quote'
	data-id='es-132'
	>
	What if I told you that all of this boilerplate code I’ve just written can be replaced with just one annotation?</p>
		<div class="blockquote__caption-wrap">
					</div>
	</div>
</div>	</div>

<div
	class="wrapper wrapper__use-simple--true"
	data-id="es-137"
	 data-animation='slideFade' data-animation-target='inner-items'>
		
			<div class="block-paragraph" data-id="es-135">
	<p	class='typography typography--size-16-text-roman js-typography block-paragraph__paragraph'
	data-id='es-136'
	>
	Well that’s exactly what you can get by using Hilt. All you need to do is to place <code>@HiltAndroidApp</code> annotation above your application class and Hilt will do all the magic for you.</p></div>	</div>

<div
	class="wrapper wrapper__use-simple--true"
	data-id="es-139"
	 data-animation='slideFade' data-animation-target='inner-items'>
		
			<div class="block-code">
	<pre class="phiki language-kotlin github-light" data-language="kotlin" style="background-color: #fff;color: #24292e;"><code><span class="line"><span class="token" style="color: #6f42c1;">@HiltAndroidApp</span><span class="token">
</span></span><span class="line"><span class="token">class ExampleApplication</span><span class="token">: </span><span class="token" style="color: #6f42c1;">Application</span><span class="token">()
</span></span><span class="line"><span class="token">
</span></span></code></pre></div>	</div>

<div
	class="wrapper wrapper__use-simple--true"
	data-id="es-142"
	 data-animation='slideFade' data-animation-target='inner-items'>
		
			<div class="block-paragraph" data-id="es-140">
	<p	class='typography typography--size-16-text-roman js-typography block-paragraph__paragraph'
	data-id='es-141'
	>
	Adding this annotation here will generate:</p></div>	</div>

<div
	class="wrapper wrapper__use-simple--true"
	data-id="es-145"
	 data-animation='slideFade' data-animation-target='inner-items'>
		
			<div class="lists" data-id="es-143">
	<ul	class='typography typography--size-16-text-roman js-typography lists__typography'
	data-id='es-144'
	>
	<li><code>ApplicationComponent</code></li><li><code>ApplicationContextModule</code> – It is included in the <code>ApplicationComponent</code> and it provides context of the application anywhere in the app through the predefined <code>@ApplicationContext</code> annotation.</li><li>Components for each of your application’s entry points: <code>ActivityBuilderComponent</code>, <code>FragmentBuilderComponent</code>, <code>ServiceBuilderComponent</code>, <code>ViewBuilderModule</code> etc.</li></ul></div>	</div>

<div
	class="wrapper wrapper__use-simple--true"
	data-id="es-148"
	 data-animation='slideFade' data-animation-target='inner-items'>
		
			<div class="block-heading" data-id="es-146">
	<h2	class='typography typography--size-52-default js-typography block-heading__heading'
	data-id='es-147'
	>
	Step 2: Annotate your entry points with @AndroidEntryPoint</h2></div>	</div>

<div
	class="wrapper wrapper__use-simple--true"
	data-id="es-151"
	 data-animation='slideFade' data-animation-target='inner-items'>
		
			<div class="block-paragraph" data-id="es-149">
	<p	class='typography typography--size-16-text-roman js-typography block-paragraph__paragraph'
	data-id='es-150'
	>
	You might have noticed that you don’t have to use the <code>@ContributesAndroidInjector</code> annotations anymore. So how does the Hilt generated dependency graph know about your entry points? The answer is – <code>@AndroidEntryPoint</code>.</p></div>	</div>

<div
	class="wrapper wrapper__use-simple--true"
	data-id="es-153"
	 data-animation='slideFade' data-animation-target='inner-items'>
		
			<div class="block-code">
	<pre class="phiki language-kotlin github-light" data-language="kotlin" style="background-color: #fff;color: #24292e;"><code><span class="line"><span class="token" style="color: #6f42c1;">@AndroidEntryPoint</span><span class="token">
</span></span><span class="line"><span class="token">class MainActivity </span><span class="token">: </span><span class="token" style="color: #6f42c1;">AppCompatActivity</span><span class="token">() {
</span></span><span class="line"><span class="token">
</span></span><span class="line"><span class="token">   </span><span class="token" style="color: #6f42c1;">@Inject</span><span class="token">
</span></span><span class="line"><span class="token">   </span><span class="token" style="color: #d73a49;">lateinit</span><span class="token"> </span><span class="token">var </span><span class="token">analytics</span><span class="token">: </span><span class="token" style="color: #6f42c1;">AnalyticsProvider</span><span class="token">
</span></span><span class="line"><span class="token">
</span></span><span class="line"><span class="token">   </span><span class="token" style="color: #d73a49;">override</span><span class="token"> </span><span class="token">fun onCreate</span><span class="token">(savedInstanceState</span><span class="token">: </span><span class="token" style="color: #6f42c1;">Bundle</span><span class="token">?</span><span class="token">) {
</span></span><span class="line"><span class="token">       </span><span class="token" style="color: #005cc5;">super</span><span class="token">.onCreate</span><span class="token">(savedInstanceState)
</span></span><span class="line"><span class="token">       </span><span class="token" style="color: #d73a49;">..</span><span class="token">.
</span></span><span class="line"><span class="token">   }
</span></span><span class="line"><span class="token">}
</span></span><span class="line"><span class="token">
</span></span></code></pre></div>	</div>

<div
	class="wrapper wrapper__use-simple--true"
	data-id="es-156"
	 data-animation='slideFade' data-animation-target='inner-items'>
		
			<div class="block-paragraph" data-id="es-154">
	<p	class='typography typography--size-16-text-roman js-typography block-paragraph__paragraph'
	data-id='es-155'
	>
	Adding this annotation above any of the entry points will automatically add them to builder components generated by <code>@HiltAndroidApp</code>, making them injection ready. Hilt will also provide a predefined <code>@ActivityContext</code> annotation to inject activity context anywhere you need.</p></div>	</div>

<div
	class="wrapper wrapper__use-simple--true"
	data-id="es-159"
	 data-animation='slideFade' data-animation-target='inner-items'>
		
			<div class="block-paragraph" data-id="es-157">
	<p	class='typography typography--size-16-text-roman js-typography block-paragraph__paragraph'
	data-id='es-158'
	>
	Everything available in the main application graph can now be injected to your entry points. If you need some specific dependencies for your entry point, you need to make a component that will only be available for that entry point. However, including your modules in <code>ApplicationComponent</code> or any of the generated components will work just as well.</p></div>	</div>

<div
	class="wrapper wrapper__use-simple--true"
	data-id="es-162"
	 data-animation='slideFade' data-animation-target='inner-items'>
		
			<div class="block-paragraph" data-id="es-160">
	<p	class='typography typography--size-16-text-roman js-typography block-paragraph__paragraph'
	data-id='es-161'
	>
	<code>@AndroidEntryPoint</code> can be placed above:</p></div>	</div>

<div
	class="wrapper wrapper__use-simple--true"
	data-id="es-165"
	 data-animation='slideFade' data-animation-target='inner-items'>
		
			<div class="bullet bullet--left bullet__type--dot bullet__color--black block-bullet__bullet" data-id="es-163">
			<div class="bullet__dot"></div>
		<div class="bullet__content">
		<p	class='typography typography--size-16-text-roman js-typography bullet__paragraph'
	data-id='es-164'
	>
	<code>Activity</code></p>	</div>
</div>	</div>

<div
	class="wrapper wrapper__use-simple--true"
	data-id="es-168"
	 data-animation='slideFade' data-animation-target='inner-items'>
		
			<div class="bullet bullet--left bullet__type--dot bullet__color--black block-bullet__bullet" data-id="es-166">
			<div class="bullet__dot"></div>
		<div class="bullet__content">
		<p	class='typography typography--size-16-text-roman js-typography bullet__paragraph'
	data-id='es-167'
	>
	<code><code>Fragment</code></code></p>	</div>
</div>	</div>

<div
	class="wrapper wrapper__use-simple--true"
	data-id="es-171"
	 data-animation='slideFade' data-animation-target='inner-items'>
		
			<div class="bullet bullet--left bullet__type--dot bullet__color--black block-bullet__bullet" data-id="es-169">
			<div class="bullet__dot"></div>
		<div class="bullet__content">
		<p	class='typography typography--size-16-text-roman js-typography bullet__paragraph'
	data-id='es-170'
	>
	<code><code>View</code></code></p>	</div>
</div>	</div>

<div
	class="wrapper wrapper__use-simple--true"
	data-id="es-174"
	 data-animation='slideFade' data-animation-target='inner-items'>
		
			<div class="bullet bullet--left bullet__type--dot bullet__color--black block-bullet__bullet" data-id="es-172">
			<div class="bullet__dot"></div>
		<div class="bullet__content">
		<p	class='typography typography--size-16-text-roman js-typography bullet__paragraph'
	data-id='es-173'
	>
	<code><code>Service</code></code></p>	</div>
</div>	</div>

<div
	class="wrapper wrapper__use-simple--true"
	data-id="es-177"
	 data-animation='slideFade' data-animation-target='inner-items'>
		
			<div class="bullet bullet--left bullet__type--dot bullet__color--black block-bullet__bullet" data-id="es-175">
			<div class="bullet__dot"></div>
		<div class="bullet__content">
		<p	class='typography typography--size-16-text-roman js-typography bullet__paragraph'
	data-id='es-176'
	>
	<code><code>BroadcastReceiver</code></code></p>	</div>
</div>	</div>

<div
	class="wrapper wrapper__use-simple--true"
	data-id="es-180"
	 data-animation='slideFade' data-animation-target='inner-items'>
		
			<div class="block-heading" data-id="es-178">
	<h2	class='typography typography--size-52-default js-typography block-heading__heading'
	data-id='es-179'
	>
	Step 3: Include your modules in the dependency graph with <code>@InstallIn</code></h2></div>	</div>

<div
	class="wrapper wrapper__use-simple--true"
	data-id="es-183"
	 data-animation='slideFade' data-animation-target='inner-items'>
		
			<div class="block-paragraph" data-id="es-181">
	<p	class='typography typography--size-16-text-roman js-typography block-paragraph__paragraph'
	data-id='es-182'
	>
	Almost every project will have some modules that will provide some dependencies, such as network clients or databases. You can keep your existing modules. All you need to do is to add an <code>@InstallIn</code> annotation to each one.</p></div>	</div>

<div
	class="wrapper wrapper__use-simple--true"
	data-id="es-185"
	 data-animation='slideFade' data-animation-target='inner-items'>
		
			<div class="block-code">
	<pre class="phiki language-kotlin github-light" data-language="kotlin" style="background-color: #fff;color: #24292e;"><code><span class="line"><span class="token" style="color: #6f42c1;">@Module</span><span class="token">
</span></span><span class="line"><span class="token" style="color: #6f42c1;">@InstallIn</span><span class="token">(ApplicationComponent</span><span class="token">::</span><span class="token" style="color: #6f42c1;">class</span><span class="token">)
</span></span><span class="line"><span class="token">class DatabaseModule </span><span class="token">{
</span></span><span class="line"><span class="token">
</span></span><span class="line"><span class="token">   </span><span class="token" style="color: #6f42c1;">@Provides</span><span class="token">
</span></span><span class="line"><span class="token">   </span><span class="token">fun database</span><span class="token">()</span><span class="token">: </span><span class="token" style="color: #6f42c1;">MyDatabase</span><span class="token"> </span><span class="token">{
</span></span><span class="line"><span class="token">       </span><span class="token" style="color: #d73a49;">return</span><span class="token"> MyDatabaseBuilder</span><span class="token">.build</span><span class="token">()
</span></span><span class="line"><span class="token">   }
</span></span><span class="line"><span class="token">}
</span></span><span class="line"><span class="token">
</span></span></code></pre></div>	</div>

<div
	class="wrapper wrapper__use-simple--true"
	data-id="es-188"
	 data-animation='slideFade' data-animation-target='inner-items'>
		
			<div class="block-paragraph" data-id="es-186">
	<p	class='typography typography--size-16-text-roman js-typography block-paragraph__paragraph'
	data-id='es-187'
	>
	The<code>@InstallIn</code> annotation takes the component class as a parameter and you can add any of the generated components and even your own component in case you need to restrict access for some dependency. This must be done for every module, otherwise your app won’t build.</p></div>	</div>

<div
	class="wrapper wrapper__use-simple--true"
	data-id="es-191"
	 data-animation='slideFade' data-animation-target='inner-items'>
		
			<div class="block-paragraph" data-id="es-189">
	<p	class='typography typography--size-16-text-roman js-typography block-paragraph__paragraph'
	data-id='es-190'
	>
	<code>@InstallIn</code> can also be used with <code>@EntryPoint</code> annotated classes. While <code>@AndroidEntryPoint</code> will automatically add your class to globally available components, with <code>@EntryPoint</code> you can define the component it will be installed in. This is a way of restricting some dependencies to specific screens or services.</p></div>	</div>

<div
	class="wrapper wrapper__use-simple--true"
	data-id="es-194"
	 data-animation='slideFade' data-animation-target='inner-items'>
		
			<div class="block-heading" data-id="es-192">
	<h2	class='typography typography--size-52-default js-typography block-heading__heading'
	data-id='es-193'
	>
	Step 4 (Optional): Integrate Hilt with ViewModels</h2></div>	</div>

<div
	class="wrapper wrapper__use-simple--true"
	data-id="es-197"
	 data-animation='slideFade' data-animation-target='inner-items'>
		
			<div class="block-paragraph" data-id="es-195">
	<p	class='typography typography--size-16-text-roman js-typography block-paragraph__paragraph'
	data-id='es-196'
	>
	We have already managed to reduce the boilerplate code amount significantly, but there is more of what you can do with it. Hilt offers a handy solution for <code>ViewModel</code> injection in your android apps. Firstly you need to add some additional dependencies to your app-level <code>build.gradle</code> file.</p></div>	</div>

<div
	class="wrapper wrapper__use-simple--true"
	data-id="es-199"
	 data-animation='slideFade' data-animation-target='inner-items'>
		
			<div class="block-code">
	<pre class="phiki language-groovy github-light" data-language="groovy" style="background-color: #fff;color: #24292e;"><code><span class="line"><span class="token">implementation &amp;#</span><span class="token" style="color: #005cc5;">8217</span><span class="token">;androidx</span><span class="token" style="color: #d73a49;">.</span><span class="token" style="color: #005cc5;">hilt</span><span class="token">:</span><span class="token">hilt</span><span class="token" style="color: #d73a49;">-</span><span class="token">lifecycle</span><span class="token" style="color: #d73a49;">-</span><span class="token" style="color: #005cc5;">viewmodel</span><span class="token">:</span><span class="token" style="color: #005cc5;">1.0</span><span class="token" style="color: #005cc5;">.0</span><span class="token" style="color: #d73a49;">-</span><span class="token">alpha01&amp;#</span><span class="token" style="color: #005cc5;">8217</span><span class="token">;
</span></span><span class="line"><span class="token">kapt &amp;#</span><span class="token" style="color: #005cc5;">8217</span><span class="token">;androidx</span><span class="token" style="color: #d73a49;">.</span><span class="token" style="color: #005cc5;">hilt</span><span class="token">:</span><span class="token">hilt</span><span class="token" style="color: #d73a49;">-</span><span class="token" style="color: #005cc5;">compiler</span><span class="token">:</span><span class="token" style="color: #005cc5;">1.0</span><span class="token" style="color: #005cc5;">.0</span><span class="token" style="color: #d73a49;">-</span><span class="token">alpha01&amp;#</span><span class="token" style="color: #005cc5;">8217</span><span class="token">;
</span></span><span class="line"><span class="token"> 
</span></span></code></pre></div>	</div>

<div
	class="wrapper wrapper__use-simple--true"
	data-id="es-202"
	 data-animation='slideFade' data-animation-target='inner-items'>
		
			<div class="block-paragraph" data-id="es-200">
	<p	class='typography typography--size-16-text-roman js-typography block-paragraph__paragraph'
	data-id='es-201'
	>
	Then you need to add <code>@ViewModelInject</code> to your <code>ViewModel</code>.</p></div>	</div>

<div
	class="wrapper wrapper__use-simple--true"
	data-id="es-204"
	 data-animation='slideFade' data-animation-target='inner-items'>
		
			<div class="block-code">
	<pre class="phiki language-kotlin github-light" data-language="kotlin" style="background-color: #fff;color: #24292e;"><code><span class="line"><span class="token">class FriendsViewModel </span><span class="token" style="color: #6f42c1;">@ViewModelInject</span><span class="token"> </span><span class="token">constructor</span><span class="token">(
</span></span><span class="line"><span class="token">   </span><span class="token" style="color: #d73a49;">private</span><span class="token"> </span><span class="token">val </span><span class="token">repository</span><span class="token">: </span><span class="token" style="color: #6f42c1;">MyRepository</span><span class="token">
</span></span><span class="line"><span class="token">)</span><span class="token">: </span><span class="token" style="color: #6f42c1;">ViewModel</span><span class="token"> </span><span class="token">{
</span></span><span class="line"><span class="token">
</span></span><span class="line"><span class="token">   </span><span class="token">fun getFriendsList</span><span class="token">() {
</span></span><span class="line"><span class="token">       repository</span><span class="token">.getFriends</span><span class="token">()
</span></span><span class="line"><span class="token">       </span><span class="token" style="color: #d73a49;">..</span><span class="token">.
</span></span><span class="line"><span class="token">   }
</span></span><span class="line"><span class="token">}
</span></span><span class="line"><span class="token">
</span></span></code></pre></div>	</div>

<div
	class="wrapper wrapper__use-simple--true"
	data-id="es-207"
	 data-animation='slideFade' data-animation-target='inner-items'>
		
			<div class="block-paragraph" data-id="es-205">
	<p	class='typography typography--size-16-text-roman js-typography block-paragraph__paragraph'
	data-id='es-206'
	>
	And you are all set. Hilt will even go a step further and it will override the default <code>ViewModelProvider</code> factory with the injected <code>ViewModel</code> ‘s so you can use the <code>viewModels</code> property extension to get your <code>ViewModel</code> ‘s.</p></div>	</div>

<div
	class="wrapper wrapper__use-simple--true"
	data-id="es-209"
	 data-animation='slideFade' data-animation-target='inner-items'>
		
			<div class="block-code">
	<pre class="phiki language-kotlin github-light" data-language="kotlin" style="background-color: #fff;color: #24292e;"><code><span class="line"><span class="token" style="color: #6f42c1;">@AndroidEntryPoint</span><span class="token">
</span></span><span class="line"><span class="token">class FriendsFragment </span><span class="token">: </span><span class="token" style="color: #6f42c1;">Fragment</span><span class="token">(</span><span class="token" style="color: #6f42c1;">R</span><span class="token">.</span><span class="token" style="color: #6f42c1;">layout</span><span class="token">.</span><span class="token" style="color: #6f42c1;">fragment_friends</span><span class="token">) </span><span class="token">{
</span></span><span class="line"><span class="token">
</span></span><span class="line"><span class="token">   </span><span class="token">val </span><span class="token">viewModel </span><span class="token" style="color: #d73a49;">by</span><span class="token"> </span><span class="token">viewModels</span><span class="token">&lt;</span><span class="token" style="color: #6f42c1;">FriendsViewModel</span><span class="token">&gt;</span><span class="token">()
</span></span><span class="line"><span class="token">
</span></span><span class="line"><span class="token">   </span><span class="token" style="color: #d73a49;">override</span><span class="token"> </span><span class="token">fun onViewCreated</span><span class="token">(view</span><span class="token">: </span><span class="token" style="color: #6f42c1;">View</span><span class="token">, savedInstanceState</span><span class="token">: </span><span class="token" style="color: #6f42c1;">Bundle</span><span class="token">?</span><span class="token">) {
</span></span><span class="line"><span class="token">       </span><span class="token" style="color: #005cc5;">super</span><span class="token">.onViewCreated</span><span class="token">(view, savedInstanceState)
</span></span><span class="line"><span class="token">       viewModel</span><span class="token">.getFriendsList</span><span class="token">()
</span></span><span class="line"><span class="token">   }
</span></span><span class="line"><span class="token">}
</span></span><span class="line"><span class="token"> 
</span></span></code></pre></div>	</div>

<div
	class="wrapper wrapper__use-simple--true"
	data-id="es-212"
	 data-animation='slideFade' data-animation-target='inner-items'>
		
			<div class="block-heading" data-id="es-210">
	<h2	class='typography typography--size-52-default js-typography block-heading__heading'
	data-id='es-211'
	>
	A promising future</h2></div>	</div>

<div
	class="wrapper wrapper__use-simple--true"
	data-id="es-215"
	 data-animation='slideFade' data-animation-target='inner-items'>
		
			<div class="block-paragraph" data-id="es-213">
	<p	class='typography typography--size-16-text-roman js-typography block-paragraph__paragraph'
	data-id='es-214'
	>
	Hopefully, by going through the steps you can reduce the setup time for dependency injection in your application and make it generally more readable. If you decide to migrate your application to Hilt, I would like to encourage you to keep watching for new updates and changes to Hilt. It is a promising library and plugin, and it could be bringing even more benefits in the future.</p></div>	</div>
</div>
</div>		</div>
	</div><p>The post <a href="https://infinum.com/blog/dagger-hilt/">Stop Holding Dagger by the Blade: Use the Hilt</a> appeared first on <a href="https://infinum.com">Infinum</a>.</p>
]]>
				</content:encoded>
			</item>
		
	</channel>
</rss>