Android Project Structure — alternative way

We all know how android project structure looks like — place all images inside this folder and all layouts inside that folder. But… during project development the number of files grow up rapidly and it becomes hard to navigate and search needed file.

Typical android project structure

Resource folder — per screen

In case if your screens consist of big amount of layouts, drawables, dimensions — it make sense to create separate resource folder for every screen.

Resource folder — per screen android project structure

As you can see on image above we have two root folders inside main folder:

  • res-main contain all common resources which are used on more than one screen.
  • res-screen contain resource folders for every screen e.g. about, chat, event details, event list, home, login

Let’s take a look what we have inside chat screen resource folder.

Resource folder

Chat itself consist of several xml layouts files so we created chat layout folder and moved all those files here. It also has a lot of .png images which are used only on chat screen, so we moved all those images files to chat drawable-hdpi, drawable-xhdpi, drawable-xxhdpi and drawable-xxxhdpi folders.

When times come to implement landscape layout or tablet version of chat we will make layout-land and layout-sw720dp folders inside chat screen resource folder.

How declare screen resource folder?

Open app.gradle file and declare sourceSets inside android section. More about resource merging here.

sourceSets {
main {
res.srcDirs = [
'src/main/res-main',
'src/main/res-screen/about',
'src/main/res-screen/chat',
'src/main/res-screen/event-detail',
'src/main/res-screen/event-list',
'src/main/res-screen/home',
'src/main/res-screen/login',
]
}
}

Instead of declaring all resource files explicitly you can write simple script which will add all sub folders of given folder to srcDirs.

sourceSets {
main {
file('src/main/res-screen')
.listFiles()
.each { res.srcDirs += it.path }
}
}
Note: above works only if you are in Project view

Conclusion

If you have a big project and want to arrange your folders, quickly see which layout, drawables, values, etc. belongs to which screen try to use resource folder — per screen android project structure.

FAQ

I have a following error: “URI is not Registered”

  • In Android Studio / Intellij IDEA click on Menu — File — Invalidate Caches / Restart…
  • Wait until project is indexed