Multiple resource folders in Android
Android resource organization system is controversial.
It sets clear boundaries between code and resources, such as images or layouts. But on the other hand, these 2 are closely related in some cases, especially the Activities/Fragments with their layouts.
And having those so far apart makes it hard, especially in large projects. When you are working on a class and you want to create a helper class, you place those 2 close together (usually in the same package) because they share the same "context". But you cannot do the same with resource files, because only a single resource folder exists and everything must be placed there.
Actually, there is a way to have multiple resource folders in an Android project. You just need to declare them in your module's build.gradle file.
android {
[...]
sourceSets {
main.res.srcDirs += [
'src/main/java/your/package/name/res',
[...]
]
}
}
You can have as many as you want. Just add a new line for each new resource folder. In the defined root res
folders you can have multiple subfolders depending on the type of resources you have (e.g. only having res/layout
if you only have layouts).
In the above example, we are appending (+=
) the new resource folders to the existing main one. You can remove the main one by just setting ( =
) instead.
The main use-case I use this approach is to have all the relevant resources for a screen close together. So each screen has its own package, and for each screen package a dedicated resource folder.
I hope I made your Android development life a bit easier!