Last active
January 1, 2023 17:05
-
-
Save manuelvicnt/437668cda3a891d347e134b1de29aee1 to your computer and use it in GitHub Desktop.
Hilt and AssistedInject working together in Hilt v2.28-alpha times - ViewModel version
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// IMPORTANT! READ THIS FIRST | |
// Assisted Injection doesn't work with @HiltViewModel or @ViewModelInject | |
// Read more about the issue here: https://github.com/google/dagger/issues/2287 | |
// | |
// | |
// 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" | |
} | |
} | |
} | |
} |
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.
@gturedi How did you solve this issue?
How can I inject headers at runtime in retrofit following this?
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks for the code!