#StackBounty: #kotlin #dependency-injection #dagger-hilt What are different between "Inject dependencies into Android classes&quot…

Bounty: 200

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?


Get this bounty!!!

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.