I have a custom WebView that I add to my layout xml:
<my.company.ui.ExtendedWebView />
It extends the native WebView:
class ExtendedWebView @JvmOverloads constructor(context: Context,
attrs: AttributeSet? = null, defStyle: Int = 0)
: WebView(context, attrs, defStyle) {
// ...
}
How can I use Hilt to inject a @Singelton
class in to the class above? Property injection? How should I annotate the class?
What I have found is that the
@AndroidEntryPoint
annotation needs to be on the View, the Fragment (if in a Fragment) AND the Activity. Because Annotations.So consider you have your DI set up as follows:
And my Application is properly set up:
Now if I have a View with an injected dependency:
I will get the error:
So I added the
@AndroidEntryPoint
annotation to the Fragment containing the View:And then we get hit with the next error:
And so I've learned the annotations need to bubble all the way up, from View to (if in a Fragment) Fragment, to Activity: