What are different between "Inject dependencies into Android classes" and "Generated components for Android classes"?

Viewed 221

I'm learning dependency injection by the article.

I'm very confused between "Inject dependencies into Android classes" and "Generated components for Android classes" ? What are different?

For example:

Code A has generated components for Android classes with @InstallIn(ActivityComponent::class), should Code B still add the annotation @AndroidEntryPoint?

Code A

@Module
@InstallIn(ActivityComponent::class)
object AnalyticsModule {

  @Provides
  fun provideAnalyticsService(
    @AuthInterceptorOkHttpClient okHttpClient: OkHttpClient
  ): AnalyticsService {
      return Retrofit.Builder()
               .baseUrl("https://example.com")
               .client(okHttpClient)
               .build()
               .create(AnalyticsService::class.java)
  }
}

Code B

@AndroidEntryPoint
class ExampleActivity : AppCompatActivity() {

  @Inject lateinit var mAnalyticsService: AnalyticsService
  ...
}

Added Content

If I havn't any @Module in my project and add only such as @AndroidEntryPoint before Acitiviy, Hilt automagically creates predetermined Component for me, is it right?

If I have some @Module such as AnalyticsModule in my project and add such as @AndroidEntryPoint before Acitiviy, Hilt automagically creates predetermined Component and my dependency injection provideAnalyticsService for me, is it right?

1 Answers

Inject dependencies into Android classes

This means, that you can inject dependencies (such as AnalyticsService) into android classes (such as Activities, Application or fragments), that are either annotated with @HiltAndroidApp or @AndroidEntryPoint.

If you annotate a fragment or activity with @AndroidEntryPoint, Hilt automagically creates a HiltComponent for this class (the same you do with @Module @InstallIn etc.. just in the background).

The Developers just want to tell you, that you CAN inject something into an android class, BUT you have to annotate them before doing that:

  • Application (by using @HiltAndroidApp)
  • ViewModel (by using @HiltViewModel)
  • Acitiviy (by using @AndroidEntryPoint)
  • Fragment (by using @AndroidEntryPoint)
  • etc

Generated components for Android classes

These are the components, that Hilt provides to YOU in order to inject something to it's corresponding class. For example, if you want to provide a dependency to the entire application (every class can access this dependency), than you would use a SingletonComponent. But if you want to provide let's say a dependency only to a viewmodel (no other class should have access to it), that you would install this dependency inside a ViewModelComponent

Edit

If I havn't any @Module in my project and add only such as @AndroidEntryPoint before Acitiviy, Hilt automagically creates predetermined Component for me, is it right?

Yes that is correct, but it only creates such components that hilt need in order to provide any dependency to an activity.

If I have some @Module such as AnalyticsModule in my project and add such as @AndroidEntryPoint before Acitiviy, Hilt automagically creates predetermined Component and my dependency injection provideAnalyticsService for me, is it right?

Yes that is sorta right: With @AndroidEntryPoint hilt makes sure, that every dependency that you provide via @Module @InstallIn(...) can be injected inside a android class. You should not forget the following: Android is a framework and you as a user never create such classes like Activity or Fragment (you write code in it, but don't create them, the framework does this for you). With this in mind, you as a user have to annotate Android-Classes with @AndroidEntryPoint in order to tell hilt, that it should create it's corresponding modules so that you can inject dependencies inside a android-class.

Related