In large projects, it´s very common that the resources folder grows and grows and becomes hard to organize and maintain. There is a very simple way to create more res folders. That way, you´ll be able to divide your drawables in different categories, so it will be easier to keep track of what resources you created.
For instance, imagine you have a project with three different layouts depending on the device, let´s say mobile, TV devices and Android TV. You could create three folders and have those resources divided, and keep the original one for common things.
How to configure multiple resources folders
Gradle is a very powerful tool. It has many default configurations so that we don´t need to rewrite it every time we start a project. For instance, the resources folder is always in the path src/main/res. But we can alter this the way we want very easily from the sourceSets section:
android { ... sourceSets { main.java.srcDirs = ['...'] main.assets.srcDirs = ['...'] main.res.srcDirs = ['...'] test.java.srcDirs = ['...'] } }
As you see, you cannot only define the res directory, but also java, assets or test folders. If instead of configuring a totally different folder, what you want is adding a new one, you can do it this way:
sourceSets { main.java.srcDirs += '...' main.assets.srcDirs += '...' main.res.srcDirs += '...' test.java.srcDirs += '...' }
Simple, it will just add the folder we specify to the sets of folders for that type. How would we then add our three new res folders? You already know how:
sourceSets { main.res.srcDirs += 'src/main/res-mobile' main.res.srcDirs += 'src/main/res-tv' main.res.srcDirs += 'src/main/res-android-tv' }
Or if you want to do it in one line:
sourceSets { main.res.srcDirs += ['src/main/res-mobile', 'src/main/res-tv', 'src/main/res-android-tv'] }
That´s what you´ll get:


Conclusion
The Gradle plugin let us organize our projects as we want, and we can take advantage of it in some situations to overtake the limits we had before we could use this powerful build tool. This is only one example, but you can extend this idea to meet your needs.
Is it possible as well to create subfolders in drawable, layout.. folders somehow?
Not that I know, but maybe there’s a way to get it using Gradle
Yes, see my answer here: http://stackoverflow.com/questions/4930398/can-the-android-layout-folder-contain-subfolders/22426467#22426467
Why this does not work for me ? I added this to the end of the android{} block
sourceSets {
main.res.srcDirs += ‘src/main/res-icons’
}
Any idea?
Seems to be OK. Do you see the res folder icon on that folder after syncing? You know, that kind of golden cylinder that appears on regular res folder
The thing is, the new res folder doesnt even seem to appear after gradle syncing :/ I would really want it to work as my projects get out of hand sometimes. I am going to have another go at it. Cheers
But you need to create the folder manually. Adding the line at build.gradle is not enough. Did you do it?
Sorry for the confusion 😀 I overestimated the power of Gradle. All works now. Thanks for that
How can we test a custom view (with a layout XML defined in, for example, res-mobile) with Robolectric?
I always get the following error:
android.content.res.Resources$NotFoundException: null
at org.robolectric.shadows.ShadowResources.loadXmlResourceParser
Thanks, it works but first we need to create this folder manually ….
can i use this to add mp3 files to the app resource ?
Can I add src from different module?
Thank you! You made my day. I have successfully set up a cross platform setup for desktop / html5 and android targets for my game. This enabled me to keep only copy of my resources and all the targets use the same assets folder.
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.