0% found this document useful (0 votes)
147 views

Hilt & Dagger Annotations: V1.0 - @androiddev

This document summarizes annotations used in Hilt for dependency injection in Android applications. Key annotations include @HiltAndroidApp, @AndroidEntryPoint, @Inject, @Module, @Provides, @Binds, and @EntryPoint. These annotations allow constructor injection, field injection, module definitions, binding declarations, and accessing dependencies in non-Hilt classes. Scoping annotations like @Singleton are also covered.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
147 views

Hilt & Dagger Annotations: V1.0 - @androiddev

This document summarizes annotations used in Hilt for dependency injection in Android applications. Key annotations include @HiltAndroidApp, @AndroidEntryPoint, @Inject, @Module, @Provides, @Binds, and @EntryPoint. These annotations allow constructor injection, field injection, module definitions, binding declarations, and accessing dependencies in non-Hilt classes. Scoping annotations like @Singleton are also covered.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1

Hilt & Dagger Annotations

v1.0 - @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() { ... }

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. ...
}

class MyViewModel @ViewModelInject constructor(


Tells Hilt how to provide instances of an private val adapter: AnalyticsAdapter,
@ViewModelInject ** Architecture Component ViewModel. @Assisted private val state: SavedStateHandle
): ViewModel() { ... }

@InstallIn(ApplicationComponent::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(ApplicationComponent::class)


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

@InstallIn(ApplicationComponent::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(ApplicationComponent::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(ApplicationComponent::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()
...
}
}

* The code example for this annotation assumes you’re using the Gradle plugin ** This annotation is avaiable using the androidx.hilt.hilt-lifecycle-viewmodel library

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

You might also like