Skip to content

Kotlin MVVM Modularized Template with Dagger2, Architecture Components, Data Binding, Dynamic Features and Unit/UI Tests.

License

Notifications You must be signed in to change notification settings

zakayothuku/kotlin-modular-mvvm-template

Repository files navigation

Kotlin Modularized MVVM Template

Kotlin Version License

This is a template to get up and started with a modularized Android application with an MVVM Clean Architecture at it's core with a modern 2020 approach to Android application development with Kotlin and the latest tech-stack.

Code style

To maintain the style and quality of the code, are used the bellow static analysis tools. All of them use properly configuration and you find them in the project root directory.

Tools Config file Check command Fix command
detekt /.detekt ./gradlew detekt -
ktlint - ./gradlew ktlint ./gradlew ktlintFormat
spotless /.spotless ./gradlew spotlessCheck ./gradlew spotlessApply
lint /.lint ./gradlew lint -

All these tools are integrated in pre-commit git hook, in order ensure that all static analysis and tests passes before you can commit your changes. To skip them for specific commit add this option at your git command:

git commit --no-verify

The pre-commit git hooks have exactly the same checks as CircleCI and are defined in this script. This step ensures that all commits comply with the established rules. However the continuous integration will ultimately be validated that the changes are correct.

Design

App support different screen sizes and the content has been adapted to fit for mobile devices and tablets. To do that, it has been created a flexible layout using one or more of the following concepts:

Design follows these recommendations android material design, a comprehensive guide for visual, motion, and interaction design across platforms and devices. Granting the project in this way a great user experience (UX) and user interface (UI). For more info about UX best practices visit link.

Architecture

The architecture of the application is based, apply and strictly complies with the following:

Modules

Modules are collection of source files and build settings that allow you to divide a project into discrete units of functionality. In this case apart from dividing by functionality/responsibility, existing the following dependence between them:

The above graph shows the app modularisation:

  • :app depends on :core and indirectly depends on :features by dynamic-features.
  • :features modules depends on :commons, :core, :libraries and :app.
  • :core and :commons only depends for possible utils on :libraries.
  • :libraries don’t have any dependency.

App module

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

Core module

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

Features modules

The :features module are an 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 an com.android.library only contains code and resources which are shared between feature modules. Reusing this way resources, layouts, views, and components in the different features modules, without the need to duplicate code.

Libraries modules

The :libraries modules are an 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. Lets assume the ViewModel requests data from the network and the data comes back some time 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.

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

Best practices

Avoid reinventing the wheel by following these guidelines:

Contributions

All contributions are welcome! Please feel free to post questions, recommendations, ideas, bugs by create new issue following the template or if you want create directly new pull request.

Resources

Projects

This is project is a template, drawing inspiration from these awesome 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.
  • kotlin-sample-app (by VMadalin) - Android Components Architecture in a Modular Word sample project.
  • architecture-sample (by android) - collection of samples to discuss and showcase different architectural tools and patterns for Android apps.

License

    Copyright 2020 Zakayo Thuku.

    Licensed under the Apache License, Version 2.0 (the "License");
    you may not use this file except in compliance with the License.
    You may obtain a copy of the License at

            http://www.apache.org/licenses/LICENSE-2.0

    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.

About

Kotlin MVVM Modularized Template with Dagger2, Architecture Components, Data Binding, Dynamic Features and Unit/UI Tests.

Topics

Resources

License

Stars

Watchers

Forks