I am trying to set up basic Hilt dependency injection in a new project. This is a brand-new project with no added files.
Android Studio version is Hedgehog (2023.1.1) Patch 2.
I chose New Project >> Phone and Tablet >> "Empty Activity" as the project type.
I left all defaults (API 26, Android 8.0; Kotlin DSL) as-is except the project name.
Other's solutions for the error below apparently worked in the past, but standards have changed. For example, most solutions recommend using kapt, but kapt has been moved to maintenance mode with a recommendation to use ksp. Yet the KSP API lists Hilt support as 'in progress'. Kapt errors are mentioned below.
Existing solutions did not work for me, or I am overlooking something being new to Hilt. For reference, I've completed two full stack apps in Android Studio coded in Kotlin without using Hilt.
Current Gradle project sync error:
Plugin Repositories (could not resolve plugin artifact 'com.google.dagger.hilt.android.gradle.plugin:com.google.dagger.hilt.android.gradle.plugin.gradle.plugin:2.48')
Searched in the following repositories:
MavenRepo
Gradle Central Plugin Repository
On returning this error the IDE points to the Plugins section of the Project level build.gradle.kts
. Note below that I am using Hilt version 2.48 to reduce possible compatibility conflicts 2.50 may have with other libraries. I have tried 2.40, 2.44, and 2.50 with the same result (aside from the listed version number of course).
Below is the settings.gradle.kts:
pluginManagement {
repositories {
google()
mavenCentral()
gradlePluginPortal()
}
}
dependencyResolutionManagement {
repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
repositories {
google()
mavenCentral()
}
}
rootProject.name = "My Project"
include(":app")
Project level build.gradle.kts:
repositories {
google()
mavenCentral()
}
}
plugins {
id("com.android.application") version "8.2.2" apply false
id("org.jetbrains.kotlin.android") version "1.9.0" apply false
kotlin("kapt") version "1.9.22"
id("com.google.dagger.hilt.android.gradle.plugin") version "2.48" apply false
}
App level build.gradle.kts:
plugins {
// order is important here
id("com.android.application")
id("org.jetbrains.kotlin.android")
id("kotlin-kapt")
id("com.google.dagger.hilt.android")
}
android {
namespace = "com.mydomain.myproject"
compileSdk = 34
defaultConfig {
applicationId = "com.mydomain.myproject"
minSdk = 26
targetSdk = 34
versionCode = 136
versionName = "1.36"
testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
isMinifyEnabled = false
proguardFiles(getDefaultProguardFile("proguard-android-optimize.txt"), "proguard-rules.pro")
}
}
// Enable Jetpack Compose
buildFeatures { compose = true }
// Set the Compose compiler version
composeOptions { kotlinCompilerExtensionVersion = "1.4.0" }
compileOptions {
sourceCompatibility = JavaVersion.VERSION_1_8
targetCompatibility = JavaVersion.VERSION_1_8
}
kotlinOptions {
jvmTarget = "1.8"
}
}
dependencies {
implementation("androidx.core:core-ktx:1.12.0")
testImplementation("junit:junit:4.13.2")
androidTestImplementation("androidx.test.ext:junit:1.1.5")
androidTestImplementation("androidx.test.espresso:espresso-core:3.5.1")
// Jetpack Compose dependencies
implementation("androidx.compose.ui:ui:1.6.1")
implementation("androidx.compose.material:material:1.6.1")
implementation("androidx.compose.ui:ui-tooling-preview:1.6.1")
androidTestImplementation("androidx.compose.ui:ui-test-junit4:1.6.1")
debugImplementation("androidx.compose.ui:ui-tooling:1.6.1")
implementation("androidx.navigation:navigation-compose:2.7.7")
// Viewmodel dependencies
implementation("androidx.lifecycle:lifecycle-viewmodel-compose:2.7.0")
// Hilt
implementation ("com.google.dagger:hilt-android:2.48")
kapt ("com.google.dagger:hilt-compiler:2.48")
// For instrumentation tests
androidTestImplementation ("com.google.dagger:hilt-android-testing:2.48")
kaptAndroidTest ("com.google.dagger:hilt-compiler:2.48")
// For local unit tests
testImplementation ("com.google.dagger:hilt-android-testing:2.48")
kaptTest ("com.google.dagger:hilt-compiler:2.48")
}
kapt {
correctErrorTypes true
}
Note: id("kotlin-kapt")
shows no IDE errors, but all kapt
references in the dependencies section of this file are flagged as Unresolved reference
. These were added on instruction from the Hilt Gradle Build Setup.
Other reference materials:
Maven Repository
Hilt Gradle Build Setup