Add multiple resource folders to an Android App

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.assets.srcDirs = ['...']
        main.res.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.assets.srcDirs += '...'
    main.res.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:

Multiple resource folders
multiple resource folders expanded


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.

14 thoughts on “Add multiple resource folders to an Android App”

  1. 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?

    1. Antonio Leiva

      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

      1. 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

      2. Antonio Leiva

        But you need to create the folder manually. Adding the line at build.gradle is not enough. Did you do it?

  2. 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

  3. 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.

  4. 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.

Comments are closed.