androidkotlingradledagger-hilt

How to fix this Hilt gradle setup?


Error which I am facing - >

Task :app:hiltJavaCompileDebug FAILED error: ComponentProcessingStep was unable to process 'com.example.todolist.TASKapp_HiltComponents.SingletonC' because 'dagger.hilt.android.internal.lifecycle.DefaultActivityViewModelFactory' could not be resolved.      Dependency trace:       => element (CLASS): androidx.hilt.lifecycle.ViewModelFactoryModules.ActivityModule       => element (METHOD): provideFactory(android.app.Activity,android.app.Application,java.util.Map<java.lang.String,javax.inject.Provider<androidx.hilt.lifecycle.ViewModelAssistedFactory<? extends androidx.lifecycle.ViewModel>>>)       => annotation: @DefaultActivityViewModelFactory       => type (ERROR annotation type): dagger.hilt.android.internal.lifecycle.DefaultActivityViewModelFactory      If type 'dagger.hilt.android.internal.lifecycle.DefaultActivityViewModelFactory' is a generated type, check above for compilation errors that may have prevented the type from being generated. Otherwise, ensure that type 'dagger.hilt.android.internal.lifecycle.DefaultActivityViewModelFactory' is on your classpath. error: ComponentProcessingStep was unable to process 'com.example.todolist.TASKapp_HiltComponents.ActivityRetainedC' because 'dagger.hilt.android.internal.lifecycle.DefaultActivityViewModelFactory' could not be resolved.      Dependency trace:       => element (CLASS): androidx.hilt.lifecycle.ViewModelFactoryModules.ActivityModule       => element (METHOD): provideFactory(android.app.Activity,android.app.Application,java.util.Map<java.lang.String,javax.inject.Provider<androidx.hilt.lifecycle.ViewModelAssistedFactory<? extends androidx.lifecycle.ViewModel>>>)       => annotation: @DefaultActivityViewModelFactory       => type (ERROR annotation type): dagger.hilt.android.internal.lifecycle.DefaultActivityViewModelFactory      If type 'dagger.hilt.android.internal.lifecycle.DefaultActivityViewModelFactory' is a generated type, check above for compilation errors that may have prevented the type from being generated. Otherwise, ensure that type 'dagger.hilt.android.internal.lifecycle.DefaultActivityViewModelFactory' is on your classpath. error: ComponentProcessingStep was unable to process 'com.example.todolist.TASKapp_HiltComponents.ActivityC' because 'dagger.hilt.android.internal.lifecycle.DefaultActivityViewModelFactory' could not be resolved.      Dependency trace:       => element (CLASS): androidx.hilt.lifecycle.ViewModelFactoryModules.ActivityModule       => element (METHOD): provideFactory(android.app.Activity,android.app.Application,java.util.Map<java.lang.String,javax.inject.Provider<androidx.hilt.lifecycle.ViewModelAssistedFactory<? extends androidx.lifecycle.ViewModel>>>)       => annotation: @DefaultActivityViewModelFactory       => type (ERROR annotation type): dagger.hilt.android.internal.lifecycle.DefaultActivityViewModelFactory      If type 'dagger.hilt.android.internal.lifecycle.DefaultActivityViewModelFactory' is a generated type, check above for compilation errors that may have prevented the type from being generated. Otherwise, ensure that type 'dagger.hilt.android.internal.lifecycle.DefaultActivityViewModelFactory' is on your classpath. error: ComponentProcessingStep was unable to process 'com.example.todolist.TASKapp_HiltComponents.FragmentC' because 'dagger.hilt.android.internal.lifecycle.DefaultFragmentViewModelFactory' could not be resolved.      Dependency trace:       => element (CLASS): androidx.hilt.lifecycle.ViewModelFactoryModules.FragmentModule       => element (METHOD): provideFactory(androidx.fragment.app.Fragment,android.app.Application,java.util.Map<java.lang.String,javax.inject.Provider<androidx.hilt.lifecycle.ViewModelAssistedFactory<? extends androidx.lifecycle.ViewModel>>>)       => annotation: @DefaultFragmentViewModelFactory       => type (ERROR annotation type): dagger.hilt.android.internal.lifecycle.DefaultFragmentViewModelFactory      If type 'dagger.hilt.android.internal.lifecycle.DefaultFragmentViewModelFactory' is a generated type, check above for compilation errors that may have prevented the type from being generated. Otherwise, ensure that type 'dagger.hilt.android.internal.lifecycle.DefaultFragmentViewModelFactory' is on your classpath. 4 errors  ComponentProcessingStep was unable to process 'com.example.todolist.TASKapp_HiltComponents.SingletonC' because 'dagger.hilt.android.internal.lifecycle.DefaultActivityViewModelFactory' could not be resolved.  ComponentProcessingStep was unable to process 'com.example.todolist.TASKapp_HiltComponents.ActivityRetainedC' because 'dagger.hilt.android.internal.lifecycle.DefaultActivityViewModelFactory' could not be resolved.  ComponentProcessingStep was unable to process 'com.example.todolist.TASKapp_HiltComponents.ActivityC' because 'dagger.hilt.android.internal.lifecycle.DefaultActivityViewModelFactory' could not be resolved.  ComponentProcessingStep was unable to process 'com.example.todolist.TASKapp_HiltComponents.FragmentC' because 'dagger.hilt.android.internal.lifecycle.DefaultFragmentViewModelFactory' could not be resolved.

My gradle file ->

plugins {    
    alias(libs.plugins.android.application)
    alias(libs.plugins.jetbrains.kotlin.android)
    alias(libs.plugins.hilt)
    id("kotlin-kapt")
    id("androidx.room")
}

android {    
    namespace = "com.example.todolist"   
    compileSdk = 34
    defaultConfig {       
        applicationId = "com.example.todolist"
        minSdk = 24
        targetSdk = 34
        versionCode = 1
        versionName = "1.0"
        testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
        vectorDrawables {
            useSupportLibrary = true        
        }    
    }    
    buildTypes {        
        release {           
            isMinifyEnabled = false            
            proguardFiles(
                getDefaultProguardFile("proguard-android-optimize.txt"),
                "proguard-rules.pro"            )
            }    
    }    
    compileOptions {
        sourceCompatibility = JavaVersion.VERSION_1_8        
        targetCompatibility = JavaVersion.VERSION_1_8    
    }    
    kotlinOptions {        
        jvmTarget = "1.8"    
    }    
    buildFeatures {
        compose = true
    }    
    composeOptions {        
        kotlinCompilerExtensionVersion = "1.5.10"  
     }    
    packaging {        
        resources {           
             excludes += "/META-INF/{AL2.0,LGPL2.1}"      
         }   
     }    
    room {
        schemaDirectory("$projectDir/schemas")
    }
}

dependencies {   
    implementation(libs.androidx.navigation.runtime.ktx)
    val room_version = "2.6.1"
    implementation(libs.androidx.room.runtime)
    annotationProcessor(libs.androidx.room.room.compiler)

    // To use Kotlin annotation processing tool (kapt)
    kapt(libs.androidx.room.room.compiler)
    implementation(libs.androidx.room.ktx)
    implementation(libs.kotlinx.coroutines.android)
    implementation(libs.androidx.core.ktx)
    implementation(libs.androidx.lifecycle.runtime.ktx)
    implementation(libs.androidx.activity.compose)
    implementation(platform(libs.androidx.compose.bom))
    implementation(libs.androidx.ui)
    implementation(libs.androidx.ui.graphics)
    implementation(libs.androidx.ui.tooling.preview)
    implementation(libs.androidx.material3)
    testImplementation(libs.junit)
    androidTestImplementation(libs.androidx.junit)
    androidTestImplementation(libs.androidx.espresso.core)
    androidTestImplementation(platform(libs.androidx.compose.bom))
    androidTestImplementation(libs.androidx.ui.test.junit4)
    debugImplementation(libs.androidx.ui.tooling)
    debugImplementation(libs.androidx.ui.test.manifest)
    implementation(libs.hilt.android)
    kapt(libs.hilt.android.compiler)
    implementation(libs.androidx.hilt.lifecycle.viewmodel)
    kapt(libs.androidx.hilt.compiler.v100)
    implementation(libs.androidx.hilt.navigation.compose)
}

kapt {correctErrorTypes = true}

Second gradle file ->

plugins {    
    alias(libs.plugins.android.application) apply false    
    id("com.google.dagger.hilt.android") version "2.51.1" apply false    
    id("org.jetbrains.kotlin.android") version "1.9.22" apply false     
    id("androidx.room") version "2.6.1" apply false
}  

my libs file ->

[versions]
agp = "8.5.0"
googleHiltAndroid = "2.52"
hiltAndroid = "2.51.1"
hiltAndroidCompiler = "2.52"
hiltAndroidVersion = "2.44"
hiltCompiler = "1.0.0-alpha01"
hiltCompilerVersion = "1.2.0"
hiltLifecycleViewmodel = "1.0.0-alpha03"
hiltWork = "1.2.0"
kotlin = "1.9.22"
coreKtx = "1.13.1"
junit = "4.13.2"
junitVersion = "1.2.1"
espressoCore = "3.6.1"
kotlinxCoroutinesAndroid = "1.9.0"
lifecycleRuntimeKtx = "2.8.6"
activityCompose = "1.9.2"
composeBom = "2024.09.02"
navigationRuntimeKtx = "2.8.1"
roomRuntime = "2.6.1"

[libraries]
androidx-core-ktx = { group = "androidx.core", name = "core-ktx", version.ref = "coreKtx" }
androidx-hilt-compiler = { module = "androidx.hilt:hilt-compiler", version.ref = "hiltWork" }
androidx-hilt-compiler-v100 = { module = "androidx.hilt:hilt-compiler", version.ref = "hiltCompilerVersion" }
androidx-hilt-compiler-v100alpha01 = { module = "androidx.hilt:hilt-compiler", version.ref = "hiltCompiler" }
androidx-hilt-lifecycle-viewmodel = { module = "androidx.hilt:hilt-lifecycle-viewmodel", version.ref = "hiltLifecycleViewmodel" }
androidx-hilt-navigation-compose = { module = "androidx.hilt:hilt-navigation-compose", version.ref = "hiltCompilerVersion" }
androidx-hilt-work = { module = "androidx.hilt:hilt-work", version.ref = "hiltWork" }
androidx-room-ktx = { module = "androidx.room:room-ktx", version.ref = "roomRuntime" }
androidx-room-room-compiler = { module = "androidx.room:room-compiler", version.ref = "roomRuntime" }
androidx-room-runtime = { module = "androidx.room:room-runtime", version.ref = "roomRuntime" }
hilt-android = { module = "com.google.dagger:hilt-android", version.ref = "hiltAndroid" }
hilt-android-compiler = { module = "com.google.dagger:hilt-android-compiler", version.ref = "hiltAndroid" }
#noinspection SimilarGradleDependencyhilt-android-compiler-v252 = { module = "com.google.dagger:hilt-android-compiler", version.ref = "hiltAndroidCompiler" }
#noinspection SimilarGradleDependencyhilt-android-v244 = { module = "com.google.dagger:hilt-android", version.ref = "hiltAndroidVersion" }
#noinspection SimilarGradleDependencyhilt-android-v252 = { module = "com.google.dagger:hilt-android", version.ref = "googleHiltAndroid" }
hilt-compiler = { module = "com.google.dagger:hilt-compiler", version.ref = "hiltAndroidVersion" }
junit = { group = "junit", name = "junit", version.ref = "junit" }
androidx-junit = { group = "androidx.test.ext", name = "junit", version.ref = "junitVersion" }
androidx-espresso-core = { group = "androidx.test.espresso", name = "espresso-core", version.ref = "espressoCore" }
androidx-lifecycle-runtime-ktx = { group = "androidx.lifecycle", name = "lifecycle-runtime-ktx", version.ref = "lifecycleRuntimeKtx" }
androidx-activity-compose = { group = "androidx.activity", name = "activity-compose", version.ref = "activityCompose" }
androidx-compose-bom = { group = "androidx.compose", name = "compose-bom", version.ref = "composeBom" }
androidx-ui = { group = "androidx.compose.ui", name = "ui" }
androidx-ui-graphics = { group = "androidx.compose.ui", name = "ui-graphics" }
androidx-ui-tooling = { group = "androidx.compose.ui", name = "ui-tooling" }
androidx-ui-tooling-preview = { group = "androidx.compose.ui", name = "ui-tooling-preview" }
androidx-ui-test-manifest = { group = "androidx.compose.ui", name = "ui-test-manifest" }
androidx-ui-test-junit4 = { group = "androidx.compose.ui", name = "ui-test-junit4" }
androidx-material3 = { group = "androidx.compose.material3", name = "material3" }
androidx-navigation-runtime-ktx = { group = "androidx.navigation", name = "navigation-runtime-ktx", version.ref = "navigationRuntimeKtx" }
kotlinx-coroutines-android = { module = "org.jetbrains.kotlinx:kotlinx-coroutines-android", version.ref = "kotlinxCoroutinesAndroid" }

[plugins]
android-application = { id = "com.android.application", version.ref = "agp" }
jetbrains-kotlin-android = { id = "org.jetbrains.kotlin.android", version.ref = "kotlin" }
hilt = { id = "com.google.dagger.hilt.android", version.ref = "hiltAndroid" }

I tried updating some dependencies and corrected the versions for hilt compiler and kotlin but it didn't work.What am I missing and here. I am trying to create an application which utilizes room here.


Solution

  • You mixed a lot of different, incompatible artifacts. To clean it up do the following:

    1. libs.versions.toml:

      • Remove the following versions:
        googleHiltAndroid
        hiltAndroidCompiler
        hiltAndroidVersion
        hiltCompiler
        hiltLifecycleViewmodel
        hiltWork
        
      • Rename the version hiltCompilerVersion to hiltNavigationCompose
      • Remove the following libraries:
        androidx-hilt-compiler
        androidx-hilt-compiler-v100
        androidx-hilt-compiler-v100alpha01
        androidx-hilt-lifecycle-viewmodel
        androidx-hilt-work
        SimilarGradleDependencyhilt-android-compiler-v252
        SimilarGradleDependencyhilt-android-v244
        SimilarGradleDependencyhilt-android-v252
        hilt-compiler
        
      • Make sure androidx-hilt-navigation-compose uses the version hiltNavigationCompose
    2. Top-level build.gradle.kts:

      Replace

      id("com.google.dagger.hilt.android") version "2.51.1" apply false
      

      with

      alias(libs.plugins.hilt) apply false
      
    3. Module-level build.gradle.kts:

      Remove the following lines:

      implementation(libs.androidx.hilt.lifecycle.viewmodel)
      kapt(libs.androidx.hilt.compiler.v100)
      

    That's it. After a gradle sync everything should work again.