Skip to content

Instantly share code, notes, and snippets.

View Nyame123's full-sized avatar

Nyame Bismark Nyame123

View GitHub Profile
Components -- - Created at - - - Destroyed at
SingletonComponents - - Application#Oncreate() - - - Application#Destroyed()
ActivityRetainedComponents - Activity#Oncreate() - Activity#OnDestroy()
ViewModelComponents - - ViewModel create - - ViewModel destroy
ActivityComponents- - Activity#Oncreate() - -Activity#OnDestroy()
FragmentComponents - - Fragment#OnAttached() - - Fragment#OnDettached()
ViewComponents - - View#super() - - - - - View destroy
ViewWithFragmentComponent - - View#super() - -- View destroy
ServiceComponent - - Service#OnCreate()- - Service#OnDestroy()
// As a dependency of a constructor-injected class.
class ExampleServiceImpl @Inject constructor(
@AuthInterceptorOkHttpClient private val okHttpClient: OkHttpClient
) : ...
// At field injection.
@AndroidEntryPoint
class ExampleActivity: AppCompatActivity() {
@AuthInterceptorOkHttpClient
@Module
@InstallIn(ActivityComponent::class)
object AnalyticsModule {
@Provides
fun provideAnalyticsService(
@AuthInterceptorOkHttpClient okHttpClient: OkHttpClient
): AnalyticsService {
return Retrofit.Builder()
.baseUrl("https://example.com")
@Module
@InstallIn(SingletonComponent::class)
object NetworkModule {
@AuthInterceptorOkHttpClient
@Provides
fun provideAuthInterceptorOkHttpClient(
authInterceptor: AuthInterceptor
): OkHttpClient {
return OkHttpClient.Builder()
@Qualifier
@Retention(AnnotationRetention.BINARY)
annotation class AuthInterceptorOkHttpClient
@Qualifier
@Retention(AnnotationRetention.BINARY)
annotation class OtherInterceptorOkHttpClient
interface AppNavigation{
fun navigateTo(screens: Screens)
}
class AppNavigationImpl @Inject constructor(
...
) : AppNavigation{ ... }
@Module
@InstallIn(ActivityComponent::class)
object AnalyticsModule {
@Provides
fun provideAnalyticsService(
// Potential dependencies of this type
): AnalyticsService {
return Retrofit.Builder()
.baseUrl("https://example.com")
@AndroidEntryPoint
class ExampleActivity : AppCompatActivity() { ... }
android {
...
compileOptions {
sourceCompatibility = JavaVersion.VERSION_1_8
targetCompatibility = JavaVersion.VERSION_1_8
}
}
plugins {
kotlin("kapt")
id("dagger.hilt.android.plugin")
}
android {
...
}
dependencies {