Skip to content
tezvyn:

How would you access a Hilt dependency in a non-injectable ContentProvider?

Source: developer.android.comMediumHow cards are made

How would you access a Hilt dependency in a non-injectable ContentProvider?

Tests Hilt's escape hatch for framework classes. Strong answer: define an EntryPoint in SingletonComponent, expose the dependency, and retrieve it with EntryPointAccessors.fromApplication. Red flag: field injection or manual static singletons.

What's really being asked

This question tests whether you know Hilt's escape hatch for Android framework classes that Hilt cannot inject directly, such as ContentProvider, BroadcastReceiver when not using Hilt's built-in support, or legacy third-party library entry points. It specifically checks your familiarity with the EntryPoints API, your understanding of Hilt component scoping, and your ability to distinguish between objects Hilt manages and objects instantiated by the Android framework. A senior candidate should also demonstrate awareness of context lifecycle mismatches.

The full answer

A good answer hits four things in order. First, state clearly that Hilt cannot perform member injection into a ContentProvider because the framework instantiates it, not Hilt. Second, define an interface annotated with EntryPoint and InstallIn SingletonComponent, since SettingsManager is typically application-scoped. Third, declare an accessor method such as fun settingsManager(): SettingsManager inside that interface. Fourth, inside ContentProvider.onCreate, call EntryPointAccessors.fromApplication(context.applicationContext, YourEntryPoint::class.java).settingsManager() to obtain the instance. Mentioning that the component must match where the dependency is bound shows depth.

The mistakes people make

Red flags to avoid include suggesting field injection with Inject inside the ContentProvider, which Hilt does not support for this class type. Another mistake is using EntryPoints.get with an Activity context instead of the Application context, which will crash or return the wrong component. Some candidates propose creating a manual static singleton or calling the dependency's constructor directly; this breaks Hilt's graph, testability, and scoping guarantees. Finally, installing the entry point in the wrong component, such as ActivityComponent, is an error because a ContentProvider lacks access to activity-scoped objects.

What usually comes next

An interviewer might ask why Hilt supports Activities and Fragments automatically but not ContentProviders; the answer is that Hilt uses bytecode transformation via the Gradle plugin for those classes, whereas ContentProviders are instantiated by the system before the Application's onCreate may even complete. They may also ask about thread safety; retrieving the entry point is just a map lookup, but you should store the dependency in a local field during onCreate to avoid repeated lookups during query calls. Another follow-up is how to test this; you can replace the SingletonComponent bindings in a test module because the entry point still resolves through the same Hilt graph.

A concrete example

Imagine a SettingsManager bound in a SingletonModule. You create interface SettingsEntryPoint annotated with EntryPoint and InstallIn SingletonComponent::class, with a method fun settingsManager(): SettingsManager. In your legacy ContentProvider, inside onCreate, you write val entryPoint = EntryPointAccessors.fromApplication(context, SettingsEntryPoint::class.java) and then val settingsManager = entryPoint.settingsManager(). This gives you a fully Hilt-managed instance without Hilt ever touching the ContentProvider's constructor.

Interview question

You need to use a Hilt-provided SettingsManager inside a legacy ContentProvider. What is the proper Hilt escape hatch?

  • a.Annotate a SettingsManager field with @Inject inside the ContentProvider
  • b.Create an @EntryPoint interface installed in SingletonComponent, expose SettingsManager, and retrieve it via EntryPointAccessors.fromApplication in onCreateCorrect
  • c.Create an @EntryPoint interface installed in ActivityComponent and retrieve it with EntryPointAccessors.fromActivity
  • d.Instantiate SettingsManager manually in onCreate and cache it in a static variable
Why?

The Android framework instantiates ContentProvider, so Hilt cannot perform field injection; you must define an EntryPoint in SingletonComponent and retrieve it with EntryPointAccessors.fromApplication. Option A is wrong because Hilt does not support member injection for framework-instantiated classes like ContentProvider.

Just read this? Test yourself on what you have been reading.

Read the original → developer.android.com

You just looked this up. Could you explain it out loud?

That is the part interviews actually test. Tezvyn takes questions like this one and gives you what the interviewer is really checking, the answer that lands, and the mistake that ends the conversation, in the four minutes before your next meeting.

The iPhone app is on the way

We are building it. Until it lands, nothing here is held back from you: every interview card, your saved cards, streaks and the job board all work in Safari, plus hundreds of free practice quizzes of thirty questions each. Sign in and it all carries over to the app the day it arrives.

Want it as an icon? Tap Share at the bottom of Safari, then Add to Home Screen. It opens full screen and the cards you have read stay available offline.

Get it on Google PlayiPhone app coming soon

We are hiring for this. Open roles that interview on android — each one lists the topics its interview covers.

See open roles