In this post, I would like to share (what I believe to be) a simple and effective way that I use to deal with resources on my android projects.
First a disclaimer: I am not a researcher; I am not proposing this as the best or only solution to this particular problem; I am however hoping to start a discussion or at least alleviate some of the pain my fellow developers experience every day.
Second Disclaimer: I have written this post in the clearest fashion I can, so that it can be understood by more people, if you are the TL;DR type, just skip to the last section called “The Solution”
Background
As Android developers we are all familiar with the following project structure:
We also know that the bigger the project (and audience) of the app gets, the longer the list of directories get. Unfortunately, I don’t have a way to deal with this. This post is about how to organize the files within these directories. I am going to use the file strings.xml in res/values as an example but this approach can be used with others. (Personally I use it for styles.xml and dimens.xml all the time)
In the beginning
When we start a project the directory structure will look something like this:
And this is great, its simple, clear and easy to use, the files are small and you know where everything is. But add a few months of development or a few more programmers or some last minute “essential” business requirements and these files are suddenly 100’s if not 1000’s of lines long. You might have name collisions inside the file, you might start getting merge conflicts around these files; basically it starts sucking the fun out of a developer’s life.
The Solution
To address the merge conflicts and the long file I suggest that you use multiple files. For me, I name my files related to the module or screen they belong to.
So instead of having:
I would have:
I have considered renaming them <module>_strings.xml and this would keep all the files for a particular module together but for the moment, I am using the types sorting.
To address the name conflicts I use a similar approach instead of just randomly naming stuff like this:
I would name the resources inside the strings.xml also by module like this:
As always, comments. questions and especially improvements will be greatly appreciated.