2

I'm working on a project with a lot of .xml layout files. I want to organize them in separate folders. It seems like Resource Merging would be the right solution.
http://tools.android.com/tech-docs/new-build-system/resource-merging
I want to split my layout folder in activity, listview, dialog, button, etc.

How do I modify my project and build.gradle file to accomplish this ?

vovahost
  • 34,185
  • 17
  • 113
  • 116
  • I reccomend you use this solution http://stackoverflow.com/questions/4930398/can-the-android-layout-folder-contain-subfolders#answer-22426467 which is easier to use. – vovahost Jan 28 '15 at 19:45

1 Answers1

5

Resource merging isn't really the concept you're looking for here -- that document specifies how if you have multiple build types and flavors, how they combine to provide a single view of the project's resources that will be built into the final result. In your case, you probably have a single build type and flavor, and want to have subdirectories in your resources to help organize them better.

The bad news is that Android isn't very friendly about this. The build system expects resources to be arranged in a rigid format, with all layouts being in a single folder underneath your project root, for example, and it doesn't let you deviate from that. The best thing you can do is to have multiple resource folder trees, which would look like this:

AppModule
  + src
    + main
      + java
      + res
        + drawable
        + layout
        + ...etc...
      + extra-res
        + drawable
        + layout
        + ...etc...

Each resource sub-tree has its subdirectories in the same format. You don't need to have an exhaustive list of subdirectories in there if they're empty; just include the ones that have things you need.

To make this work, you need to have the following in your build.gradle script:

android {
    sourceSets {
        main {
            res.srcDirs = ['src/main/res', 'src/main/extra-res']
        }
    }
}
Scott Barta
  • 79,344
  • 24
  • 180
  • 163
  • Thank you for an exhaustive answer. Do you think that layout resorces can also be grouped by creating subdirectories in layout folder ? For example: *layout/activity/search.xml* – vovahost Jan 27 '15 at 17:53
  • 1
    You can't do that. The format of resource directories is rigid, and you're not allowed to create arbitrary subdirectories underneath something like "layout". – Scott Barta Jan 27 '15 at 17:58
  • Do you guys see any issue about changes are not reflected in next run. I have created layout in separated resource folder and uses some if-else before to set but any new changes in new-res/layout/ file does not reflect on next run. Almost every time I have to clean or uninstall app. Any clue ? Gradle version I'm using `3.4.1`. @ScottBarta – CoDe Jan 07 '20 at 10:50