Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I use additional directories for grouping Android resources

Creating of large Android project will lead to numerous layout files in res folder. Can I just group them by creating subfolders? Is is possible in Android?

For example what I have now:

res/
    /layout/
           /Some large list there

And what I wanted to achieve:

res/
   /layout/
      /subfolder1/
         /layout1.xml
         /view1.xml
         ......
      /subfolder2
         /another_layout.xml
         /another_view.xml

I tried to do this but got error of compilation. Appreciate your help.

like image 388
Artemis Avatar asked Sep 15 '25 20:09

Artemis


1 Answers

The answer is YES, but only if you're using Android Studio and thus the Gradle Build System.

Btw: There's also a Thread on code.google.com discussing this issue (Issue 2018: R.java should recognize resources in sub-directories).
It has been closed by a Project Member with the following statement:

Marking this as released. This is largely possible in the Gradle build system as it exists today, and we won't be implementing this for other build systems.

So how can you realise this in Android Studio? Here's how:

  1. Right Click on your layout-folder --> New --> Directory --> Name it e.g. subfolder1
  2. Right Click on subfolder1 --> New --> Android resource directory (this may not be selectable to you in the first place, see the notes below)
  3. Name it layout, layout-land, layout-sw600dp or whatever you want it just needs to be valid layout resource folder
  4. Create more subfolders or layout-folders in your sub-dirs
  5. Modify your build.gradle file like this:

android {
    // Other stuff
    sourceSets {
        main {
            res.srcDirs =
                    [
                      '/src/main/res/layout/subfolder1',
                      '/src/main/res/layout/subfolder2',
                      '/src/main/res'
                    ]
        }
    }
}

Result would look like this:

sub directories in layout folder

Some things to note:

  • You may need to Sync Gradle in between, otherwise you won't be able to create a "Android resource directory in your subfolder
  • If you're using the Android-View in Android Studio you won't be seeing your subfolders. I consider this as a bug which will be fixed, hopefully.
  • This works for drawables and every other resource type as well
like image 58
reVerse Avatar answered Sep 18 '25 11:09

reVerse