87

I have an Android Studio project in which I have multiple modules, each of those module depending on a share module. And let's say that this share module has an xml file call sample.xml

When I search to open file with Navigate -> Files... and type "sample.xml", I'll get

  1. Share/src/main/res/values/sample.xml
  2. ModuleA/build/intermediates/exploded-arr/.../res/values/sample.xml
  3. ModuleB/build/intermediates/exploded-arr/.../res/values/sample.xml
  4. ModuleC/build/intermediates/exploded-arr/.../res/values/sample.xml
  5. ModuleD/build/intermediates/exploded-arr/.../res/values/sample.xml ...

Since the files in build folder are generated and we shouldn't edit them, there is no reason why I want to include them in my search result. Is there anyway I can exclude them?

yoAlex5
  • 29,217
  • 8
  • 193
  • 205
Tar_Tw45
  • 3,122
  • 6
  • 35
  • 58
  • Possible duplicate of [Ignore R.java fies in Find results](http://stackoverflow.com/questions/22973939/ignore-r-java-fies-in-find-results) – CrandellWS Jan 02 '17 at 09:17
  • Possible duplicate of http://stackoverflow.com/questions/24743242/how-do-i-get-android-studio-to-stop-returning-generated-code-in-search-results – CrandellWS Mar 07 '17 at 18:40
  • Possible duplicate of [How do I get Android Studio to stop returning generated code in search results?](http://stackoverflow.com/questions/24743242/how-do-i-get-android-studio-to-stop-returning-generated-code-in-search-results) – CrandellWS Mar 07 '17 at 18:40
  • 5
    @CrandellWS the topic you are referring is talking about "Find in Path" while mine is talking about "Navigate > File...". With Find in Path you can specify custom scope and save it but you can't do that with "Navigate > FIle...". – Tar_Tw45 Mar 13 '17 at 05:38
  • I see you are correct my mistake – CrandellWS Mar 13 '17 at 09:29
  • 1
    WTH, why the hell doesn't android studio have "mark directory as excluded" like pretty much every other JetBrains IDE? – ThiefMaster Apr 02 '17 at 12:12
  • @ThiefMaster: Because they've delegated the responsibility to Gradle in order to avoid conflicts. AS is much tighter integrated with Gradle than IDEA for instance. See: https://stackoverflow.com/a/46036948/507339 – Nilzor Sep 04 '17 at 12:23

8 Answers8

82

To put it simply, and to actually exclude build paths from your search, you need to follow Frank's answer point 1 and 2, then because there's no suitable scope that actually exclude the build folder, simply do this:

Inside the "Find in Path" dialog (CTRL+SHIFT+F):

  1. tap the ... to configure a scope
  2. click the + button, choose local scope
  3. give your new scope a name
  4. enter !file:build//* in the pattern
  5. tap OK and test your new scope

enter image description here If you further want to have a scope for a single module, create a scope for your module (including recursively your module path), then add this at the beginning of your pattern:

!file:build//*&&

For example, a scope for 2 modules:

!file:build//*&&file[MODULE1_DIRECTORY_NAME]:*/||file[MODULE2_DIRECTORY_NAME]:*/

Only got the full answer reading Frank's answer and post #7 from issue reported here: http://code.google.com/p/android/issues/detail?id=61488

soshial
  • 5,906
  • 6
  • 32
  • 40
3c71
  • 4,313
  • 31
  • 43
  • 9
    Are you sure this answers his question? He asked about filtering CTRL+SHIFT+N, you gave him the CTRL+SHIF+F answer. I need the Navigate > File solution too. Anybody knows how to? – TatiOverflow Feb 01 '17 at 21:58
  • 1
    The two module example is missing parentheses: it should be `!file:build//*&&(file[MODULE1_DIRECTORY_NAME]:*/||file[MODULE2_DIRECTORY_NAME]:*/)`, otherwise the `build` subfolder of module2 is not excluded. – Pierre-Luc Paour Mar 10 '17 at 09:30
  • 5
    People aren’t reading correctly. Find in Path (and similar) are *not* the same as Navigate -> Class / File / Symbol, etc. The former supports scopes, the later, mysteriously, doesn’t. – Martin Marconcini Aug 01 '17 at 21:27
  • For some reason this still gives me itermediates and generated files, even though the build folder is their parent. This seems to get rid of all unwanted results: "!file:build//*&&!file:*intermediates*/&&!file:*generated*/". However, the original question is regarding a file name search (not file content search), which I also have not found an answer for. – OldSchool4664 Jan 16 '18 at 23:26
  • I use `!file:build//*&&!file:app//build//*&&!lib:*..*` pattern to exclude build and lib folders – Borzh Mar 08 '18 at 15:01
  • error with "package pattern expected at position 17" this might be out of date... – StarWind0 May 16 '20 at 21:28
25

Android Studio -> Appearance & Behavior -> Scopes -> + -> add scope -> local -> set a custom name & set Pattern

!file:*intermediates*/&&!file:*generated*/&&!file:R.java

enter image description here

RUL
  • 268
  • 2
  • 12
Martino
  • 251
  • 3
  • 3
15

You can create a Custom Scope which defines the set files that you want to search and allows you to exclude those that you do not want to search.

  1. CTRL+SHIFT+F to present the Find in Path dialog.
  2. Under Scope select Custom
  3. If one of the Scopes that are present in the drop down list does not restrict the file search according to your needs you can create your own Custom Scope. To do this click on the ... button.
  4. Then Click on the + button and select Local
  5. In the pane on the right you can Include and Exclude individual files and Recursively include or exclude all files beneath a folder.

You can then use your Custom Scope to constrain the files that are searched when you do Find in Path.

Frank
  • 12,010
  • 8
  • 61
  • 78
10

Had the same issue. Found that these 'build' folders were marked as sourceFolder in the module's .iml file.

Removing all such entries fixed the problem

<sourceFolder url="file://$MODULE_DIR$/build/..." ..../>
Corwin
  • 101
  • 1
  • 3
  • 5
    When i was restart AndroidStudio,The module's iml will restore...How can i do – H3c Apr 05 '16 at 06:46
8

This problem seems to be fixed in later versions of Android Studio, but should it reoccur or you have a special folder setup:

You need to modify the exclusion list through gradle with the help of the idea plugin

Example:

apply plugin: 'idea'

idea.module {
    excludeDirs += file('build/')
}

Then run task ideaModule to regenerate the .iml file with the exclusion line described in Corwin's answer

Nilzor
  • 18,082
  • 22
  • 100
  • 167
  • 1
    Works great! I wanted to exclude my `node_modules` directory from the file find so I just added `excludeDirs += file('node_modules/')`. Thanks! – Joshua Pinter Nov 12 '17 at 02:15
8

To exclude all build folders from all modules use this pattern:

!file:*build*/

If you want exclude also all libraries, simply use this pattern:

!file:*build*/&&!lib:*..*

Detailed description:

  1. In the Find window click on the Scope tab, then on three dots button:

enter image description here

  1. Plus button -> Local -> Enter name

enter image description here

  1. Add the desired pattern:

Search scopes

  1. Save and enjoy!
Tomas
  • 4,652
  • 6
  • 31
  • 37
1

None of the patterns in these answers worked for me. This one did: !file:*/build/intermediates//*

miguel
  • 16,205
  • 4
  • 53
  • 64
0

You can use exclude pattern like

!file:build//*&&

[How to add a new pattern follow and skip tests pattern]
[Skip generated files pattern]

yoAlex5
  • 29,217
  • 8
  • 193
  • 205