How do you inject two different OkHttpClient instances with Hilt?

Tests Hilt qualifiers for same-type bindings. Answer: define a custom @Qualifier (or @Named), annotate two @Provides methods and the injection site. Red flag: manual client creation or subclassing OkHttpClient instead of qualifying the binding.
WHAT THIS TESTS: Your practical knowledge of Hilt and Dagger qualifiers when multiple dependencies share the same type. The interviewer wants to see that you can disambiguate bindings cleanly without resorting to runtime logic, manual construction, or factory methods that leak creation details into consumers. They are also checking that you understand the difference between a qualifier and scoping, since the two concepts are orthogonal.
A GOOD ANSWER COVERS: Four things in order. First, declare a custom qualifier annotation with @Qualifier and @Retention(AnnotationRetention.BINARY) to distinguish the variants. Second, provide two @Provides methods in a @Module, one returning the logging client and one returning the plain client, each annotated with the matching qualifier. Third, at the injection site, annotate the OkHttpClient parameter or property with the same qualifier to tell Hilt which instance to satisfy. Fourth, mention that custom qualifiers are preferred over @Named because they are type-safe, survive refactoring, and avoid string-constant duplication across modules.
COMMON WRONG ANSWERS: Several anti-patterns show up here. Some candidates suggest subclassing OkHttpClient or creating wrapper types, which pollutes the domain model to solve a DI framework concern. Others propose using @Binds, which only works when you already have an instance of the concrete type and cannot run builder logic like addInterceptor. Another red flag is manual instantiation inside a ViewModel or Repository, which defeats the purpose of dependency injection and complicates unit testing. Finally, using @Named with string literals is acceptable but inferior because it is brittle and offers no compile-time verification or IDE navigation.
LIKELY FOLLOW-UPS: The interviewer may ask why a custom qualifier beats @Named, or how you would handle the case if both clients are needed in the same class. They might also probe scoping, asking whether these clients should be @Singleton or unscoped, and why sharing a single OkHttpClient pool matters for connection limits and thread safety.
ONE CONCRETE EXAMPLE: Define annotation class LoggingClient with @Qualifier. In a NetworkModule, write @Provides @LoggingClient fun provideLoggingOkHttpClient(): OkHttpClient = OkHttpClient.Builder().addInterceptor(HttpLoggingInterceptor()).build(). Write a second @Provides method annotated with @NonLoggingClient that returns a plain builder build. In your repository constructor, request the specific one with @LoggingClient okHttpClient: OkHttpClient.
Source: developer.android.com - Dependency injection with Hilt
Read the original → developer.android.com
- #android
- #hilt
- #dependency-injection
- #okhttp
- #dagger
Get five bites like this every day.
Tezvyn delivers a daily feed of 60-second tech bites with quizzes to lock in what you learn.