You are on page 1of 1

Hilt & Dagger Annotations

v2.33 - @AndroidDev

Annotation Usage Code Sample

Kicks off Hilt code generation. @HiltAndroidApp


@HiltAndroidApp Must annotate the Application class. class MyApplication : Application() { ... }

Adds a DI container to the Android


@AndroidEntryPoint
@AndroidEntryPoint class annotated with it.
class MyActivity : AppCompatActivity() { ... }
This requires using Hilt’s Gradle Plugin.

Constructor Injection. Tells which class AnalyticsAdapter @Inject constructor(


constructor to use to provide instances private val service: AnalyticsService
and which dependencies the type has. ) { ... }

@Inject @AndroidEntryPoint
Field injection. Populates fields in
class MyActivity : AppCompatActivity() {
@AndroidEntryPoint annotated classes.
@Inject lateinit var adapter: AnalyticsAdapter
Fields cannot be private. ...
}

@HiltViewModel
class MyViewModel @Inject constructor(
Tells Hilt how to provide instances of an
@HiltViewModel private val adapter: AnalyticsAdapter,
Architecture Component ViewModel.
private val state: SavedStateHandle
): ViewModel() { ... }

@InstallIn(SingletonComponent::class)
Class in which you can add bindings for
@Module types that cannot be constructor injected.
@Module
class AnalyticsModule { ... }

Indicates in which Hilt-generated DI @InstallIn(SingletonComponent::class)


@InstallIn containers (SingletonComponent in the @Module
code) module bindings must be available. class AnalyticsModule { ... }

@InstallIn(SingletonComponent::class)
@Module
class AnalyticsModule {

Adds a binding for a type that cannot be @Provides


constructor injected: fun providesAnalyticsService(
converterFactory: GsonConverterFactory
- Return type is the binding type.
@Provides - Parameters are dependencies.
): AnalyticsService {
- Every time an instance is needed, the return Retrofit.Builder()
function body is executed if the type is .baseUrl("https://example.com")
not scoped. .addConverterFactory(converterFactory)
.build()
.create(AnalyticsService::class.java)
}
}

@InstallIn(SingletonComponent::class)
Shorthand for binding an interface type: @Module
abstract class AnalyticsModule {
- Methods must be in a module.
@Binds - @Binds annotated methods must be @Binds
abstract. abstract fun bindsAnalyticsService(
- Return type is the binding type.
analyticsServiceImpl: AnalyticsServiceImpl
- Parameter is the implementation type.
): AnalyticsService
}

Scope Annotations: Scoping object to a container.


@Singleton
The same instance of a type will be class AnalyticsAdapter @Inject constructor(
@Singleton provided by a container when using that
private val service: AnalyticsService
type as a dependency, for field injection,
@ActivityScoped or when needed by containers below in
) { ... }
… the hierarchy.

Qualifiers for Predefined bindings you can use as @Singleton


predefined Bindings: dependencies in the corresponding class AnalyticsAdapter @Inject constructor(
container. @ApplicationContext val context: Context
@ApplicationContext private val service: AnalyticsService
@ActivityContext These are qualifier annotations. ) { ... }

class MyContentProvider(): ContentProvider {

@InstallIn(SingletonComponent::class)
Obtain dependencies in classes that are @EntryPoint
either not supported directly by Hilt or interface MyContentProviderEntryPoint {
cannot use Hilt. fun analyticsService(): AnalyticsService
}
The interface annotated with @EntryPoint
must also be annotated with @InstallIn override fun query(...): Cursor {
passing in the Hilt predefined component val appContext =
@Entry Point from which that dependency is taken context?.applicationContext
from. ?: throw IllegalStateException()
val entryPoint =
Access the bindings using the appropriate
EntryPointAccessors.fromApplication(
static method from EntryPointAccessors
appContext,
passing in an instance of the class with
the DI container (which matches the value MyContentProviderEntryPoint::class.java)
in the @InstallIn annotation) and the entry val analyticsService =
point interface class. entryPoint.analyticsService()
...
}
}

For more information about DI, Hilt and Dagger: https://d.android.com/dependency-injection

You might also like