?️Migrating to Compose – Develop Branch ?
An android app built using Kotlin that consumes TMDB API to display current trending, upcoming and popular movies? and tvshows?. It has been built following Clean Architecture Principle, Repository Pattern, MVVM Architecture in the presentation layer as well as jetpack components.
I created this repository for a few reasons:
- To learn the approch of implementing clean architecture and SOLID principles in an android app.
- To learn libraries/tools supported by Google and most of the android development communities.
- To experiment with modularization and dynamic feature modules.
- To learn implementation of Picture-in-Picture.
- Demonstrate best developement practices by utilizing up to date tech-stack .
Table Of Content.
- App Structure
- Tech Stack
- Related Resources
- Other Helpful Resources
- Work In Progress
In order to be able to build the application you’ll need to change the api key in
gradle.properties. First and formost you need to generate your own api key by creating an IMDB account and generating an api key.
Complex architectures like the pure clean architecture can also increase code complexity since decoupling your code also means creating lots of data transformations(mappers) and models,that may end up increasing the learning curve of your code to a point where it would be better to use a simpler architecture like MVVM.
When using dynamic delivery you’ll need a Playstore Developer Account in order to test the dynamic delivery feature However, there is a work around by using GloballyDynamic which provides the same dynamic delivery capabilities as Playstore with other added advantages well suited for testing. Read More.
Dynamic feature modules require use of Android App Bundles which at the moment are not supported by all app distribution platforms and the platforms that support app bundles have diffrent integrations. However, this can be solved by using GloballyDynamic.
So let’s get started …
Dynamic Feature Modules and Dynamic Delivery?
Dynamic feature modules allow separation of certain features and resources from the base module of the app and include them in the app bundle. User can then download and install these modules later when they are required(on demand) even after the app has already been installed.E.g. In Notflix the Favorites feature/module is not installed when the app is first installed since not all user use that feature but later on the can opt to download it when the need arises. These features/modules can also be uninstalled later without installing the entire app
Dynamic Delivery is Google Play’s app serving model that uses Android App Bundles to generate and server optimized APKs for each user’s device configuration so that users download only the feature and resources the need to run the app.
Play Feature Delivery allow certain features of the app to be delivered conditionally (depending on user’s language, location/country, paying or free user etc.) or downloaded on demand.
What is Clean Architecture?
A well planned architecture is extremely important for an app to scale and all architectures have one common goal- to manage complexity of your app. This isn’t something to be worried about in smaller apps however it may prove very useful when working on apps with longer development lifecycle and a bigger team.
The circles represent different layers of your app. Note that:
The center circle is the most abstract, and the outer circle is the most concrete. This is called the Abstraction Principle. The Abstraction Principle specifies that inner circles should contain business logic, and outer circles should contain implementation details.
Another principle of Clean Architecture is the Dependency Inversion. This rule specifies that each circle can depend only on the nearest inward circle ie. low-level modules do not depend on high-level modules but the other way around.
Why Clean Architecture?
- Loose coupling between the code – The code can easily be modified without affecting any or a large part of the app’s codebase.
- Easier to test code.
- Separation of Concern – Different modules have specific responsibilities making it easier for modification and maintenance.
Single Responsibility: Each software component should have only one reason to change – one responsibility.
Open-Closed: You should be able to extend the behavior of a component, without breaking its usage, or modifying its extensions.
Liskov Substitution: If you have a class of one type, and any subclasses of that class, you should be able to represent the base class usage with the subclass, without breaking the app.
Interface Segregation: It’s better to have many smaller interfaces than a large one, to prevent the class from implementing the methods that it doesn’t need.
Dependency Inversion: Components should depend on abstractions rather than concrete implementations. Also higher level modules shouldn’t depend on lower level modules.
This is the core layer of the application. The
domain layer is independent of any other layers thus ] domain models and business logic can be independent from other layers.This means that changes in other layers will have no effect on domain layer eg. screen UI (presentation layer) or changing database (data layer) will not result in any code change withing domain layer.
Components of domain layer include:
Models: Defines the core structure of the data that will be used within the application.
Repositories: Interfaces used by the use cases. Implemented in the data layer.
Use cases/Interactors: They enclose a single action, like getting data from a database or posting to a service. They use the repositories to resolve the action they are supposed to do. They usually override the operator “invoke”, so they can be called as a function.
data layer is responsibile for selecting the proper data source for the domain layer. It contains the implementations of the repositories declared in the domain layer.
Components of data layer include:
–Dto Models: Defines POJO of network responses.
–Entity Models: Defines the schema of SQLite database.
Repositories: Responsible for exposing data to the domain layer.
Mappers: They perform data transformation between
Network: This is responsible for performing network operations eg. defining API endpoints using Retrofit.
Cache: This is responsible for performing caching operations using Room.
Data Source: Responsible for deciding which data source (network or cache) will be used when fetching data.
presentation layer contains components involved in showing information to the user. The main part of this layer are the views and viewModels.
This project uses many of the popular libraries, plugins and tools of the android ecosystem.
Hilt – Dependency Injection library.
- Android KTX – Provide concise, idiomatic Kotlin to Jetpack and Android platform APIs.
- AndroidX – Major improvement to the original Android Support Library, which is no longer maintained.
- Lifecycle – Perform actions in response to a change in the lifecycle status of another component, such as activities and fragments.
- LiveData – Lifecycle-aware, meaning it respects the lifecycle of other app components, such as activities, fragments, or services.
- ViewModel – Designed to store and manage UI-related data in a lifecycle conscious way. The ViewModel class allows data to survive configuration changes such as screen rotations.
- Data Binding – Allows you to bind UI components in your layouts to data sources in your app using a declarative format rather than programmatically.
- Room – Provides an abstraction layer over SQLite used for offline data caching.
- Navigation Component-Component that allows easier implementation of navigation from simple button clicks to more complex patterns.
MotionLayout – Helps create and manage beautiful UI animations.
Retrofit – Type-safe http client
and supports coroutines out of the box.
GSON – JSON Parser,used to parse
requests on the data layer for Entities and understands Kotlin non-nullable
and default parameters.
OkHttp-Logging-Interceptor – Logs HTTP request and response data.
Coroutines – Library Support for coroutines.
Flow – Flows are built on top of coroutines and can provide multiple values. A flow is conceptually a stream of data that can be computed asynchronously.
Timber-Library for easier logging.
Material Design – Build awesome beautiful UIs.
Glide– Image Library from loading images from the database and cacheing in memory.
kotlinx.coroutines – Library Support for coroutines,provides runBlocking coroutine builder used in tests.
Truth – Assertions Library,provides readability as far as assertions are concerned.
MockWebServer – Web server for testing HTTP clients, verify requests and responses on the TMDB API with the retrofit client.
- GitHub Actions – GitHub actions is used in this project to check for syntax correctness using KtLint, execute the unit tests and generate a new package when pushing chanes to the main branch.
- KtLint – The project uses KtLint to check for syntax correctness.
In this section i’ve included some resources ie. articles and GitHub reposirtories that i used to learn about Clean Architecture:
- The clean code blog by Robert C. Martin.
- A detailed guide on developing android apps using clean architecture pattern Medium article.
- Clean Architecture Component Boilerplater GitHub repo .
- The Force GitHub repo by David Odari demonstrating using clean architecture pattern
- Clean architecture tutorial for android article by Raywenderlich which is really beginner friendly.
- Clean architecture in android Medium article.
- Intro to app architecture and Intro to app modularization articles by ProAndroidDev.
Other Helpful Resources.
In this section i’ve included resources that are not related to clean architecture but were really helpful in learning other android components and tools:
- Pokedex GitHub repo by Ronnie Otieno demonstrating how to use various jetpack components.
- Fundamentals of testing from the official android developers site.
- Android MotionLayout Tutorial – Collapsing View blog post on how to create a collapsing view with MotionLayout.
- MotionLayout – Collapsing Toolbar blog post demonstrating how to create a collapsing toolbar using MotionLayout
- Introduction to Github Actions for Android blog by Mindorks on how to set up GitHub actions for an android project.
These are the app’s screenshots: