How to use Structural search in Android studio

When working on an Android project, you may need to search for items that are not feasible to find using the standard search tool. You might want to identify a code block that follows a certain pattern, or you might want to find items using a regular expression.

As an example, assume you wish to find all generic classes in your codebase, or all variables with custom getters, or all anonymous classes. How would you do that? Seems impossible right? That’s possible.

Meet Structural search

A conventional search process does not take into account the syntax and semantics of the source code. Even if you use regular expressions, Android Studio still treats your code as a regular text. The structural search and replace (SSR) actions let you search for a particular code pattern or grammatical construct in your code considering your code structure.

Suppose we want to locate all generic classes in the codebase.

1. Go to Edit -> Find -> Search Structurally to open the Structural Search dialog.

2. Click on Tools icon (?) and select Existing Templates

3. Under Java->Generics, select Generic classes and click Ok. (You can use same pattern on Kotlin codebase)
4. Click on Find button
5. All Generic classes will be shown in Find window.

There are so many pre-defined templates available. You can explore them and make your development journey easier.

Share with your friends

Leave a Reply

Your email address will not be published. Required fields are marked *