Dagger-Hilt Add Dependencies and Setup Project #1
Hello, in this article I will tell you how to integrate Hilt into your newly created Android project.
1. Add dependencies
The codes seen below should be added to the relevant fields in the build.gradle(:app).
plugins {
id 'com.android.application'
id 'org.jetbrains.kotlin.android'
id 'kotlin-kapt'
id 'com.google.dagger.hilt.android'
}
dependencies {
.
.
// Dagger-Hilt
implementation "com.google.dagger:hilt-android:2.46.1"
kapt "com.google.dagger:hilt-compiler:2.44"
//hilt work dependency
implementation "androidx.hilt:hilt-work:1.0.0"
kapt "androidx.hilt:hilt-compiler:1.0.0"
// For instrumentation tests
androidTestImplementation "com.google.dagger:hilt-android-testing:2.46.1"
kaptAndroidTest "com.google.dagger:hilt-compiler:2.44"
// For local unit tests
testImplementation "com.google.dagger:hilt-android-testing:2.46.1"
kaptTest "com.google.dagger:hilt-compiler:2.44"
.
.
.
The codes seen below should be added to the relevant fields in the build.gradle(:yourappname).
plugins {
.
.
id 'com.google.dagger.hilt.android' version '2.44' apply false
}
2. Create HiltAndroidApp class
This annotation is used to mark your application class as the entry point for Hilt. It generates the necessary code to set up the dependency graph for your app. In order to apply Hilt in the project, we need to have a class that inherit Application(), which covers the entire application. You can name this class whatever you want.
@HiltAndroidApp
class App : Application()
3. Add “android:name” field
At this stage, the <application…> </application> tag is located under the AndroidManifest.xml file in the application. These tags include all activities, providers and services in the application. We define android:name=”.App” in this tag. The app name represents the class with @HiltAndroidApp annotation.
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" >
<uses-permission android:name="android.permission.INTERNET"/>
<application
android:name=".App"
android:allowBackup="true"
android:dataExtractionRules="@xml/data_extraction_rules"
android:fullBackupContent="@xml/backup_rules"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.HiltTutorialApp"
tools:targetApi="31" >
<activity
android:name=".view.MainActivity"
android:exported="true" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
3. Add Activity annotation
This annotation is used to mark Android classes such as Activities, Fragments, Services, or BroadcastReceivers as entry points for Hilt. It enables field injection and dependency retrieval for these classes.
@AndroidEntryPoint
class MainActivity : AppCompatActivity() {
.
.
.
I hope it was useful. You can follow me on my social media accounts.