Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Hilt and AssistedInject working together in Hilt v2.28-alpha times - ViewModel version
// AssistedInject and Hilt working together in v2.28-alpha times
// Example of a ViewModel using AssistedInject injected in a Fragment by Hilt
// As AssistedInject isn't part of Dagger yet, we cannot use in
// conjuction with @ViewModelInject
data class MyInitParams(private val neededId: String)
class MyViewModel @AssistedInject constructor(
... // Other dependencies injected by Hilt
// com.squareup.inject.assisted.Assisted annotation
@Assisted private val initParams: MyInitParams
) : ViewModel() {
... // ViewModel logic
@AssistedInject.Factory
interface AssistedFactory {
fun create(initParams: MyInitParams): MyViewModel
}
companion object {
fun provideFactory(
assistedFactory: AssistedFactory,
initParams: MyInitParams
): ViewModelProvider.Factory = object : ViewModelProvider.Factory {
override fun <T : ViewModel?> create(modelClass: Class<T>): T {
return assistedFactory.create(initParams) as T
}
}
}
}
// This code uses "com.squareup.inject:assisted-inject-annotations-dagger2:0.5.2"
// AssistedInject puts all assisted bindings in the same module.
// We need to make a decision about where to install it.
// In this case, as we only need it in fragments, we install it there.
@InstallIn(FragmentComponent::class)
@AssistedModule
@Module(includes = [AssistedInject_AssistedInjectModule::class])
// Needed until AssistedInject is incorporated into Dagger
interface AssistedInjectModule {}
@AndroidEntryPoint // Fragment injected by Hilt
class MyFragment : Fragment() {
private val args: MyFragmentArgs by navArgs()
// Inject the factory generated by AssistedInject
@Inject lateinit var myViewModelAssistedFactory: MyViewModel.AssistedFactory
// Initialize the ViewModel using ViewModelProvider.Factory
private val myViewModel: MyViewModel by viewModels {
MyViewModel.provideFactory(
myViewModelAssistedFactory, MyInitParams(args.neededId)
)
}
... // Fragment logic
}
// To disable checking for the InstallIn annotation in some modules,
// you need to enable the compiler option in your build.gradle file like this:
android {
defaultConfig {
javaCompileOptions {
annotationProcessorOptions {
arguments["dagger.hilt.disableModulesHaveInstallInCheck"] = "true"
}
}
}
}
@vincent-paing

This comment has been minimized.

Copy link

@vincent-paing vincent-paing commented Jun 20, 2020

I found an issue with inline class, if you use inline class as an @Assisted annotated parameter, the processor doesn't detect that and throw error saying that it needs constructor with at least one @Assisted parameter

@afTrolle

This comment has been minimized.

Copy link

@afTrolle afTrolle commented Aug 5, 2020

Thank you for the example, Worked great!

@isfaaghyth

This comment has been minimized.

Copy link

@isfaaghyth isfaaghyth commented Aug 10, 2020

thanks, works like a charm!

@diousk

This comment has been minimized.

Copy link

@diousk diousk commented Oct 10, 2020

From AssistedInject 0.6.0, the generated class AssistedInject_AssistedInjectModule will also be annotated with @InstallIn.
Do we still need @Module(includes = [AssistedInject_AssistedInjectModule::class]) instead of just @Module ?

@Leeonardoo

This comment has been minimized.

Copy link

@Leeonardoo Leeonardoo commented Oct 15, 2020

From AssistedInject 0.6.0, the generated class AssistedInject_AssistedInjectModule will also be annotated with @InstallIn.
Do we still need @Module(includes = [AssistedInject_AssistedInjectModule::class]) instead of just @Module ?

It seems like you can just use @module now. I'm still testing it though

@anilkumar416

This comment has been minimized.

Copy link

@anilkumar416 anilkumar416 commented Nov 27, 2020

Thanks helped me!

@Rubywai

This comment has been minimized.

Copy link

@Rubywai Rubywai commented Dec 24, 2020

Thank I need to coroutine scope inject into viewmodel

@KuanLunTseng

This comment has been minimized.

Copy link

@KuanLunTseng KuanLunTseng commented Apr 16, 2021

Thanks for the code!

@gturedi

This comment has been minimized.

Copy link

@gturedi gturedi commented Jul 18, 2021

i got error like below with hilt 2.35:

ViewModel constructor should be annotated with @Inject instead of @AssistedInject.
[Hilt] Processing did not complete. See error above for details.

@patriotic

This comment has been minimized.

Copy link

@patriotic patriotic commented Aug 4, 2021

@gturedi How did you solve this issue?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment