HelloCW
HelloCW

Reputation: 2255

Why does injection Context into ViewModel with Hilt cause the error "could not be resolved"?

I use hilt_version = '2.41' in my project, I have read the article.

I hope to inject Context into ViewModel, so I write Code A based that article.

When I compile the Code A, I get the Result A, how can I fix the problems ?

Code A

@HiltViewModel
class SoundViewModel @Inject constructor(
    @ApplicationContext private val mContext: Context  
): ViewModel() {
   ...
}


@HiltAndroidApp
class UIApp : Application() {

}

implementation "com.google.dagger:hilt-android:$hilt_version"
kapt "com.google.dagger:hilt-android-compiler:$hilt_version"
implementation 'androidx.hilt:hilt-lifecycle-viewmodel:1.0.0-alpha03'
kapt 'androidx.hilt:hilt-compiler:1.0.0'

Result A

Task :app:hiltJavaCompileFreeDebug
: ComponentProcessingStep was unable to process 'info.dodata.soundmeter.presentation.UIApp_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: @dagger.hilt.android.internal.lifecycle.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.
: ComponentProcessingStep was unable to process 'info.dodata.soundmeter.presentation.UIApp_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: @dagger.hilt.android.internal.lifecycle.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.
: ComponentProcessingStep was unable to process 'info.dodata.soundmeter.presentation.UIApp_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: @dagger.hilt.android.internal.lifecycle.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.
: ComponentProcessingStep was unable to process 'info.dodata.soundmeter.presentation.UIApp_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: @dagger.hilt.android.internal.lifecycle.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.

Added Content:

To ladytoky0:

Thanks!

The following are my build.gradle, the project can work well when hilt_version = '2.40',and fails when hilt_version = '2.41'.

App build.gradle

plugins {
    id 'com.android.application'
    id 'kotlin-android'
    id 'kotlin-kapt'
    id 'dagger.hilt.android.plugin'
    id 'kotlin-parcelize'
}

android { 
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
    ...
}


dependencies {
    // Hilt dependencies
    implementation "com.google.dagger:hilt-android:$hilt_version"
    kapt "com.google.dagger:hilt-android-compiler:$hilt_version"
    implementation 'androidx.hilt:hilt-lifecycle-viewmodel:1.0.0-alpha03'
    kapt 'androidx.hilt:hilt-compiler:1.0.0'
    ...
  
}

Project build.gradle

buildscript {
    ext {
        compose_version = '1.1.1'
        hilt_version = '2.40'      
    }
    repositories {
        google()
        mavenCentral()
    }
    dependencies {
        classpath "com.android.tools.build:gradle:7.0.4"
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.6.10"

        classpath "com.google.dagger:hilt-android-gradle-plugin:$hilt_version"
    }
}

Upvotes: 8

Views: 8436

Answers (2)

astroboy
astroboy

Reputation: 1078

I had a similar problem and removing this should help:

implementation 'androidx.hilt:hilt-lifecycle-viewmodel:1.0.0-alpha03'

More info here

Upvotes: 28

ladytoky0
ladytoky0

Reputation: 679

I think the problem is on the dependencies. (adding dependencies)

On your build.gradle

buildscript {
    ...
    dependencies {
        ...
        classpath("com.google.dagger:hilt-android-gradle-plugin:2.38.1")
    }
}

On your app build.gradle

plugins {
    kotlin("kapt")
    id("dagger.hilt.android.plugin")
}

android {
    ...
}

dependencies {
    implementation("com.google.dagger:hilt-android:2.38.1")
    kapt("com.google.dagger:hilt-android-compiler:2.38.1")
}

// Allow references to generated code
kapt {
 correctErrorTypes = true
}

android {
    ...
    compileOptions {
        sourceCompatibility = JavaVersion.VERSION_1_8
        targetCompatibility = JavaVersion.VERSION_1_8
    }
}

Also you can take a look to this codelab android hilt

Hope it works!

Upvotes: 1

Related Questions