Hilt includes extensions for providing classes from other Jetpack libraries. Hilt currently supports the following Jetpack components:
ViewModel
WorkManager
You must add the Hilt dependencies to take advantage of these integrations. For more information about adding dependencies, see Dependency injection with Hilt.
Inject ViewModel objects with Hilt
Add the following additional dependencies to your Gradle file. Note that in addition to the library, you need to include an additional annotation processor that works on top of the Hilt annotation processor:
app/build.gradle
...
dependencies {
...
implementation 'androidx.hilt:hilt-lifecycle-viewmodel:1.0.0-alpha01'
// When using Kotlin.
kapt 'androidx.hilt:hilt-compiler:1.0.0-alpha01'
// When using Java.
annotationProcessor 'androidx.hilt:hilt-compiler:1.0.0-alpha01'
}
Provide a ViewModel
using the
@ViewModelInject
annotation in the ViewModel
object's constructor. You must
also annotate the SavedStateHandle
dependency with @Assisted
:
Kotlin
class ExampleViewModel @ViewModelInject constructor( private val repository: ExampleRepository, @Assisted private val savedStateHandle: SavedStateHandle ) : ViewModel() { ... }
Java
public class ExampleViewModel extends ViewModel { private final ExampleRepository repository; private final SavedStateHandle savedStateHandle; @ViewModelInject ExampleViewModel( ExampleRepository repository, @Assisted SavedStateHandle savedStateHandle) { this.repository = repository; this.savedStateHandle = savedStateHandle; } ... }
Then, an activity or a fragment that is annotated with @AndroidEntryPoint
can
get the ViewModel
instance as normal using ViewModelProvider
or the
by viewModels()
KTX extensions:
Kotlin
@AndroidEntryPoint class ExampleActivity : AppCompatActivity() { private val exampleViewModel: ExampleViewModel by viewModels() ... }
Java
@AndroidEntryPoint public class ExampleActivity extends AppCompatActivity { private ExampleViewModel exampleViewModel; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); exampleViewModel = new ViewModelProvider(this).get(ExampleViewModel.class); } ... }
Integration with the Jetpack navigation library
If your ViewModel
is scoped to the navigation
graph,
use the defaultViewModelProviderFactory
object that is available to
activities and fragments that are annotated with @AndroidEntryPoint
.
Kotlin
val viewModel: ExampleViewModel by navGraphViewModels(R.id.my_graph) { defaultViewModelProviderFactory }
Java
NavBackStackEntry backStackEntry = navController.getBackStackEntry(R.id.my_graph); ViewModelProvider viewModelProvider = new ViewModelProvider( backStackEntry, getDefaultViewModelProviderFactory() ); ExampleViewModel exampleViewModel = provider.get(exampleViewModel.getClass());
Inject WorkManager with Hilt
Add the following additional dependencies to your Gradle file. Note that in addition to the library, you need to include an additional annotation processor that works on top of the Hilt annotation processor:
app/build.gradle
...
dependencies {
...
implementation 'androidx.hilt:hilt-work:1.0.0-alpha01'
// When using Kotlin.
kapt 'androidx.hilt:hilt-compiler:1.0.0-alpha01'
// When using Java.
annotationProcessor 'androidx.hilt:hilt-compiler:1.0.0-alpha01'
}
Inject a Worker
using the
@WorkerInject
annotation in the Worker
object's constructor. You can use
only @Singleton
or unscoped bindings in Worker
objects. You must also
annotate the Context
and WorkerParameters
dependencies with @Assisted
:
Kotlin
class ExampleWorker @WorkerInject constructor( @Assisted appContext: Context, @Assisted workerParams: WorkerParameters, workerDependency: WorkerDependency ) : Worker(appContext, workerParams) { ... }
Java
public class ExampleWorker extends Worker { private final WorkerDependency workerDependency; @WorkerInject ExampleWorker( @Assisted @NonNull Context context, @Assisted @NonNull WorkerParameters params, WorkerDependency workerDependency ) { super(context, params); this.workerDependency = workerDependency; } ... }
Then, have your Application
class implement the Configuration.Provider
interface, inject an instance of HiltWorkFactory
, and pass it into the
WorkManager
configuration as follows:
Kotlin
@HiltAndroidApp class ExampleApplication : Application(), Configuration.Provider { @Inject lateinit var workerFactory: HiltWorkerFactory override fun getWorkManagerConfiguration() = Configuration.Builder() .setWorkerFactory(workerFactory) .build() }
Java
@HiltAndroidApp public class ExampleApplication extends Application implements Configuration.Provider { @Inject HiltWorkerFactory workerFactory; @Override public Configuration getWorkManagerConfiguration() { return Configuration.Builder() .setWorkerFactory(workerFactory) .build(); } }