google / dagger

A fast dependency injector for Android and Java.
https://dagger.dev
Apache License 2.0
17.4k stars 2.01k forks source link

Hilt: Detect @Inject constructors on framework components #3749

Open jzbrooks opened 1 year ago

jzbrooks commented 1 year ago

Fragments, Activities, etc should use @AndroidEntryPoint to perform member injection. @Inject constructs won't work here since the framework instantiates the components and dagger has no opportunity to provide constructor parameters.

Add a lint detector (with a quick fix), which would help onboard engineers new to dagger and avoid easy mistakes that could be shipped in apps.

The hilt jar should provide this functionality if a hilt assumption is baked into the detector and fix.

jzbrooks commented 1 year ago

Similarly to #3729, we may build this in our lint library for now. I see a lot of value in upstreaming, however.

JakeWharton commented 1 year ago

Note that you can constructor-inject activities and fragments with an app component factory and no Hilt: https://developer.android.com/reference/android/app/AppComponentFactory

jzbrooks commented 1 year ago

Yep. That's why I'd rather the fix go into the hilt jar instead of a generic rule set like https://github.com/Faithlife/AndroidLint, that way hilt (love or hate it) use can be assumed.