· 2 min read

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

Multiple resource folders

multiple resource folders expanded

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.

    Share:
    Back to Blog