Structural Search to Find Hilt-Injected Fields

If you allow Android Studio to format your Hilt @Inject annotations to be on their own line, as is standard, you might later find it challenging to search your codebase for all Hilt-injected class fields doing a text-based find. This is where structural search shines. I'm finding the learning curve to be pretty steep, but I did come up with a structural search that finds all the injected class vars.

Here's a screenshot of the Structural Search dialog:

To get this to work, I first went into Settings -> Plugins and installed the Structural Search for Kotlin plugin. Then I opened the Structural Search dialog (Edit -> Find -> Search Structurally) and created my template, shown above: class $Class$ {
@Inject lateinit var $Field$

As I have it here, it only finds injected fields in classes that have the @AndoidEntryPoint annotation. You can remove that if you want to find @Inject fields in all your classes.

Hope you find this helpful!

comments powered by Disqus