Package structure in Android apps
Source code package structure in Android apps development is an interesting area of discussion. There are several package structures developers follow like, Android component level packaging, App module level packaging, Any framework level packaging(MVC, MVVM, etc), or putting everything in single package(most common approach by newbies). So lets have a look of all theses different approaches –
Android component level package structure
The term “Component level packaging” means, creating packages for different components like
Activity, Fragment, BroadcastReceiver, ContentProvider. For example, if an app’s base package name would be com.demo then the sub packages are as follows com.demo.activities, com.demo.fragments, com.demo.receivers, com.demo.providers, com.demo.services etc. This approach help developer to group the classes based on their behaviour so that by looking into the packages it will be very clear that where he needs to look into. But the problem with this approach occurs when someone else also start working on the same project and he will start scratching his head as it is very unclear to find a module as a single package and so he has to run around all the packages, classes and to verify their usage across the source code which is really hectic and difficult to maintain. Yours maintenance time will shoot up, chances of errors increase and so the delivery time. So if you are following this similar approach, you might be in trouble in future if not in present.
App module level package structure
“Module level packaging” is self explainable i.e., packaging as a module. For example if you have an app with modules like social share, video recording, chat and your app’s base package name is com.test then your package should look like similar to
com.text.chat and that sub packaged will have child packages for smaller modules. This approach is better than the previous one as by going through the project any developer can understand that every package is individual module and he just need to focus and work on the assigned module instead of looking around the entire project. It has another advantage i.e., If any of the module we need to cut out of the app we just need to remove that child package, respective resources and references from
AndroidManifest.xml and you are done unlike the above one where you have to figure out the classes for that module from all the packages.
Framework level package structure
This is a very popular package structure. If a developer follow MVC framework then mostly he create s the packages like
com.test.model, com.test.view, com.test.controller or something similar. This is good to follow but here is slight problem similar to the first approach I discussed. That is the maintenance will be difficult if working with a team of new developer.
Out of all these if one can mix the last two approaches will be the best. Now you might have question how ? So let me answer this – First of all lets package the classes related to individual module as individual package now you have packaged your app at module level then go to individual module and then package the child classes again like framework level or if your are not familiar with framework package those like Android component level which will also be a better alternative.
Let me know if you have any other better approach to do this.
I hope this will be helpful to create a wonderful app.