Suggestion for the organisation of Android Resources

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:

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

resources-values

 

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.



Loading Facebook Comments ...

4 thoughts on “Suggestion for the organisation of Android Resources

    • Michal,

      Thanks! I was a Java developer before doing Android and I once read that there is a very minor performance improvement when classes that interact are in the same package. For this reason I have changed from my old style which was like this to a newer style that has all the related classes together, something like this.

  • Thanks for the post.
    We used such structure with multiple string files, it was not so nice as we expected. You would have to search for the string in all files, often not aware where the string is located. Some files only contained several strings. Also, translation tools were not smart enough to handle bunch of files (it was long ago). However, adding some prefixes to the strings it a truly good idea. We end up with single strings.xml with prefixed strings, logically divided in some sections by xml comments.