question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Dagger does not support providing @AssistedInject types

See original GitHub issue

I am using Hilt (v2.32-alpha) dependency injection with viewmodel. I have a class with 2 parameters, the first is context and the second is an optional string so if not provide any value this takes null. To achieve this in Hilt I read below doc: https://dagger.dev/dev-guide/assisted-injection.html

This is my code :

class SharedPreferencesHelper @AssistedInject constructor(
    context: Context,
    @Assisted sharedPreferencesName: String? = null
) { ... }

And Hilt Module :

@Module
@InstallIn(ViewModelComponent::class)
object WebServicesModule {

    @Provides
    fun provideSharedPreferencesHelper(@ApplicationContext context: Context): SharedPreferencesHelper =
        SharedPreferencesHelper(context)
}

AssistedFactory interface:

@AssistedFactory
interface MyAssistedFactory {
    fun create(sharedPreferencesName: String): SharedPreferencesHelper
}

And my viewmodel :

@HiltViewModel
class SplashViewModel @Inject constructor(
    application: Application,
    savedStateHandle: SavedStateHandle,
    private val repository: SplashRepository
) : AndroidViewModel(application) {

    @Inject
    lateinit var assistedFactory: MyAssistedFactory

fun callSplashApi() {

        val preferencesHelper = assistedFactory.create("ss")
...
  }
}

But build fails with this error:

Dagger does not support providing @AssistedInject types.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:10

github_iconTop GitHub Comments

3reactions
bcorsocommented, Feb 21, 2021

@alizarei95, note that #2370 will allow you to provide a qualified @AssistedInject type, but we’re still not allowing an unqualified @AssistedInject type to be provided to avoid confusion.

Thus, you’ll have to provide your default type with a qualifier like, @Default.

You probably also want to provide the default via the factory rather than calling the constructor directly. For example:

@Module
@InstallIn(ViewModelComponent::class)
object WebServicesModule {
    @Provides
    @Default
    fun provideDefault(factory: MyAssistedFactory) = factory.create()
}

@AssistedFactory
interface MyAssistedFactory {
  fun create(sharedPreferencesName: String ?= null): SharedPreferencesHelper
}

**Also, note that the fix for #2370 is not yet included in a release. It should be out with the next Dagger release, 2.33.

0reactions
bcorsocommented, Feb 23, 2021

Closing this as a dupe of #2370

Read more comments on GitHub >

github_iconTop Results From Across the Web

Inject WorkManager with Hilt but getting error "Dagger does ...
The error states that you are trying to @Provides a class with an @AssistedInject constructor. This makes sense: after all, there is no...
Read more >
Assisted Injection - Dagger
@AssistedInject types cannot be injected directly, only the @AssistedFactory type can be injected. This is true even if the constructor does not contain...
Read more >
Inject WorkManager with Hilt but getting error "Dagger does ...
Coding example for the question Inject WorkManager with Hilt but getting error "Dagger does not support providing @AssistedInject types."-kotlin.
Read more >
Inject Workmanager With Hilt But Getting Error ... - ADocLib
... But Getting Error "Dagger Does Not Support Providing @Assistedinject Types." ... Home Dagger Hilt Dagger Tutorial These are some of the questions...
Read more >
Brave New Android World with AssistedInject - ProAndroidDev
'Hey Dagger! Please provide me following component with Dependency1, Dependency2, … and DependencyX BUT be kind and let me provide DependencyX on my...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found