Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Detect premature use of @Inject properties with Android Lint #3729

Open
jzbrooks opened this issue Feb 4, 2023 · 2 comments
Open

Detect premature use of @Inject properties with Android Lint #3729

jzbrooks opened this issue Feb 4, 2023 · 2 comments

Comments

@jzbrooks
Copy link

jzbrooks commented Feb 4, 2023

Hilt rewrites methods to inject android components at a certain point (early) in their lifecycles, so premature access of @Inject properties will usually result in a kotlin.UninitializedPropertyAccessException or an NRE in Java. https://dagger.dev/hilt/components#component-lifetimes

For example, if a Fragment references an @Inject property in its initializer (or implicitly does so by using it to initialize another property), the app will crash.

@jzbrooks
Copy link
Author

jzbrooks commented Feb 4, 2023

We might put this rule in Faithlife/AndroidLint#18, but thought it would be worth considering upstreaming.

@Chang-Eric
Copy link
Member

Thanks for the suggestion! I think this Lint check is a good idea and I think it would make sense for something like this to be in Hilt. However, due to our current priorities, I'm not sure we're going to have time to take this PR right now, so I think maybe the best thing to do would be to put this rule into your project first and then we can look at upstreaming it into Hilt later in the future?

I'll leave this open as a feature request until then, but just wanted to be upfront about what we're able to take on right now. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants