Yet when it comes to combining all architecture puzzles together into simple client application it is difficult to find open-sourced app sample to follow. For this reason, I decided to build one based, apply and strictly complies with each of the following 5 points:

A single-activity architecture, using the Navigation component to manage fragment operations.

Android architecture components, part of Android Jetpack forgive to project a robust design, testable and maintainable.

Pattern Model-View-ViewModel (MVVM) facilitating separation of development of the graphical user interface.

S.O.L.I.D design principles intended to make software designs more understandable, flexible and maintainable.

Modular app architecture allows being developed features in isolation, independently from other features.

App interaction

The project presents a modern, 2019 approach to Android application development using Kotlin and latest tech-stack.

The goal of the project is to demonstrate best practices, provide a set of guidelines, modular application, scalable, maintainable and testable. This application may look simple, but it has all of these small details that will set the rock-solid foundation of the larger app suitable for bigger teams and long application lifecycle management.

Application structure

One of the key benefits of modularization architecture is supposed to be clear navigation throughout the app and source code. Looking at the root folder of the project, the following structure becomes clear:

Project structure on root folder

Interaction between modules

Between the modules is established a dependency relationship that allows us to make an API request, access to DDBB or use a certain initialized library. Reusing the code in this way and avoiding duplicating. The below graph shows the app dependency between modules:

:app depends on :core and indirectly depends on :features by dynamic-features.

depends on and indirectly depends on by dynamic-features. :features modules depend on :commons , :core, : app and some specific utils :library that will use.

modules depend on , app and some specific utils that will use. :core and :commons only depends on possible utils on :libraries .

and only depends on possible utils on . :libraries don’t have any dependency.

Graph of dependencies between modules

App module

The :app module is a com.android.application, which is needed to create the App Bundle. It is also responsible for initiating the dependency graph, play core and other project global libraries, differentiating especially between different app environments.

Core module

The :core module is a com.android.library for serving network requests or accessing to the DDBB. Providing the data source for the many features that require it.

Features modules

The :features module are a com.android.dynamic-feature is essentially a gradle module which can be downloaded independently from the base application module. It can hold code and resources and include dependencies, just like any other gradle module.

Commons modules

The :commons modules are a com.android.library only contains code and resources which are shared between feature modules. Reusing this way layouts, views, and other components in the different features modules, without the need to duplicate code.

Libraries modules

The :libraries modules are a com.android.library, basically contains different utilities that can be used by the different modules.

Architecture components

Ideally, ViewModels shouldn’t know anything about Android. This improves testability, leak safety and modularity. ViewModels have different scopes than activities or fragments. While a ViewModel is alive and running, an activity can be in any of its lifecycle states. Activities and fragments can be destroyed and created again while the ViewModel is unaware.

Passing a reference of the View (activity or fragment) to the ViewModel is a serious risk. Let's assume the ViewModel requests data from the network and the data comes back sometime later. At that moment, the View reference might be destroyed or might be an old activity that is no longer visible, generating a memory leak and, possibly, a crash.

Observer pattern in the presentation and data layer

The communication between the different layers follows the above diagram using the reactive paradigm, observing changes on components without the need of callbacks avoiding leaks and edge cases related to them.

Configuration files

With App Modularization we want to gain fine-grained dependency control but we also need to make sure we don’t end up maintaining multiple configuration files.

For that we have the following common configuration files:

The following android-dynamic-feature .gradle.kts is applied to every feature module with the following line :

plugins {

id("commons.android-dynamic-feature")

}

Things to consider

Of course, nothing is perfect, especially if it has recently come out. Here are some issues what I found during development:

Navigation component don’t support multiple back-stack for the moment. Exist different workarounds but not officially solution to this. (issue)

Robolectric isn’t compatible with dynamic-features modules for the moment. Test on dynamic-feature throws the following exception. The same test but under android-library , works correctly. (issue)

java.lang.RuntimeException:java.lang.UnsupportedOperationException: libraries not supported yet

Roboelectric throw the following exception on testing apps with data-binding in module. (issue)

java.lang.NoClassDefFoundError:androidx/databinding/DataBinderMapperImpl

MockK with LiveData dependency throws exception when try to obtain value. (issue). The workaround is to use mockito for the moment.

java.lang.ClassCastException:java.lang.Object cannot be cast to java.lang.String

Additional Resources

- Projects

This is project is a sample, to inspire you and should handle most of the common cases, but obviously not all. If you need to take a look at additional resources to find solutions for your project, visit these interesting projects:

iosched (by google) — official Android application from google IO 2019.

plaid (by android) — app which provides design news & inspiration, being an example of implementing material design.

sunflower (by android) — a gardening app illustrating Android development best practices with Android Jetpack.

architecture-components-samples (by android) — collection of samples for Android Architecture Components.

architecture-sample (by android) — collection of samples to discuss and showcase different architectural tools and patterns for Android apps.

android-clean-architecture-boilerplate (by bufferapp) — an android boilerplate project using clean architecture

android-kotlin-clean-architecture (by sanogueralorenzo) — android sample Clean Architecture app written in Kotlin.

modularization-example (by JeroenMols) — easy to understand real-life example of a modularized Android app.

lego-catalog (by Eli-Fox) — app illustrating current Android Architecture state using Android development best practices.

tivi (by chrisbanes) — an app which attempts to use the latest cutting edge libraries and tools.

android-showcase (by igorwojda) — app following best practices: Kotlin, coroutines, Clean Architecture, feature modules, tests, MVVM, static analysis.

- Articles

A collection of very interesting articles related to last android community tendencies and recommendations for starting to take in consideration for your current/next project:

- Libraries

The open-source community create and maintains tons of awesome libraries making your job easier, giving the opportunity to use them in your developments. Here is a very important collection of them:

awesome-android-ui — collection list of awesome Android UI/UX libraries.

awesome-android-libraries — a collection of awesome Kotlin related stuff.

android-arsenal — android developer portal with tools, libraries, and apps.

- Best practices

Avoid reinventing the wheel by following these guidelines:

Summary

There are certain benefits in writing or migrating to Modular App and using Architecture Components with them.

Faster build times.

Fine-grained dependency control.

Improve reusability across other apps.

Improves the ownership & the quality of the codebase.

Stricter boundaries when compared to packages.

Encourages Open Source of the newly created libraries.

Makes Instant Apps & Dynamic Features possible (improving discoverability).

Remember, keeping modules Clean improves testability and eases future refactoring in case it needs to be shared between multiple user-facing features.