🛠️Migrating to KMP - Develop Branch
An android and desktop app built using Kotlin Multiplatforom that consumes TMDB API to display current trending, upcoming and popular movies🍿 and tvshows🎬.
Kotlin Multiplatform is a framework of Kotlin that allows for sharing of a single codebase for business logic across different targets/platforms. Some of the targets supported by kotlin are: Android, iOS, Kotlin/JVM, Kotlin/JS, Android NDK, Windows, Linux, macOS etc.
Compose Multiplatform is a UI framework mabe by Jetbrains that allow for simple and accelerated desktop application and web development using compose. Compose multiplatform also allows sharing compose UI code between diffrent platforms. eg android, desktop(Windows, Linux, MacOS) and web.
- Prerequisite
- Disclaimer
- Pros of KMP/KMM
- App Structure
- Libraries
- Extras
- Demo
- Android App
- Desktop
- Related Resources
- Other Helpful Resources
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 a TMDB account and generating an api key.
-
Koltin Multiplatform is still in alpha phase at the time of writting this however it's still being used by some well known companies around the world.
-
Kotlin multiplatform and Compose multiplatform are two diffrent concepts that aim to so achieve multiplatform programming. Kotlin multiplatform is all about sharing business logic(at times presentation logic too but only "viewmodels") across diffrent platforms while compose multiplatform is about using/sharing compose UI across diffrent platforms.
-
When starting out a KMP/KMM project some knownledge of Clean Architecture or similar design patterns, SOLID principles, separation of concern and modularization might go a long way into reducing the learning curve. i have discussed the usage of above concepts in a previous iteration of this project.
-
When using KMP/KMM some knowledge of other programming language such as Swift or Javascript is still required since only the business logic is shared but the UI code is in the native programming language of the targetted platform.
-
Kotlin multiplatform and compose multiplatform have limited learning resources and a relatively small community at the time of writting. However learning resources have been attached at the end of this doc and you can find a fast diverse Kotlin community on Kotlin Official slack or Android254 twitter.
- Faster development since you only need to write most of the businness logic and tests only once.
- Improve code quality.
- Highly maintainable and testable code due to separation of concern in the codebase.
The project currently has 3 main modules:
1. androidApp
This module contains the android application's UI built using Jetpack compose.
2. desktopApp
This module contains the desktop application's UI built using Compose Multiplatform.
3. shared
This module contains shared code that holds the domain and data layers and some part of the presentation logic ie.shared viewmodels
This is the core layer of the application. The domain
layer is independent of any 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.
The 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:
-
Models
-Dto Models: Defines POJO of network responses.
-Entity Models: Defines the schema of the database.
-
Mappers: They perform data transformation between
domain
,dto
andentity
models. -
Network: This is responsible for performing network operations eg. defining API endpoints using Ktor.
-
Cache: This is responsible for performing caching operations using Realm.
-
Data Sources: Responsible for deciding which data source (network or cache) will be used when fetching data and presenting the data to viewmodels.
The presentation
package contains shared viewmodels.
- Koin - Kotin dependency injection library with multiplatform support.
- Ktor - Provides multiplatform libraries required to make network calls to the REST API.
- Realm - Caching of application data from network responses.
- Multiplatform Settings - This is a Kotlin library for Multiplatform apps, so that common code can persist key-value data.
- kotlinx.coroutines - Library support for Kotlin coroutines with multiplatform support.
- kotlinx.serialization - Provides sets of libraries for various serialization formats eg. JSON, protocol buffers, CBOR etc.
- kotlinx.datetime - A multiplatform Kotlin library for working with date and time.
- Napier - Logger library for Kotlin Multiplatform.
- Mockk - Library for creating mocks for tests.
- Jetpack Compose - Modern toolkit for building native UI.
- Coil - An image loading library for Android backed by kotlin coroutines.
- Splash Screen API - Splash screen API reduces boilerplate code required to create a splash screen.
- Accompanist Navigation Animation - Add animation support when navigating between screens using Compose navigation component.
- Accompanist Pager - A library which provides paging layouts for Jetpack Compose. If you've used Android's ViewPager before, it has similar properties.
- Accompanist Insets
- Accompanist System UI Controller - A library that provides easy-to-use utilities for updating the System UI bar colors within Jetpack Compose.
- Accompanist Material Placeholder -
- Accompanist Pager Indicator - A horizontally laid out indicator for a HorizontalPager or VerticalPager, representing the currently active page and total pages drawn using shape.
- GitHub Actions -The project uses GitHub actions for CI/CD operations such as running automated builds, tests and deploying applications.
- 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 kotlin mutltiplatform mobile:
- Your First Kotlin Multiplatform Mobile App Tutorial
- Sharing Code between iOS and Android with Kotlin #1
- Kotlin Multiplatform Hands-on: Networking and Data Storage
- KaMPKit General Architecture
- Using Koin in a Kotlin Multiplatform Project
- Create your first cross-platform mobile app – tutorial - Learn how to create and run your first Kotlin Multiplatform Mobile application.
- Kotlin Multiplatform. Very beginner’s guide (part 1-3)
- Using Realm persistence library in a Kotlin Multiplatform project
- KMM Sample - A sample project for experiments with Kotlin Multiplatform mobile.
- Fantasy Premier League - Kotlin Multiplatform project with Jetpack Compose, Compose for Desktop and SwiftUI clients (and using Ktor for remote API requests and Realm for persistence).
- KaMPKit
- People In Space - Minimal Kotlin Multiplatform project with SwiftUI, Jetpack Compose, Compose for Wear OS, Compose for Desktop, Compose for Web, and Kotlin/JS + React clients along with Ktor backend.
In this section I've included resources that are not related to kotin multiplatform mobile but were really helpful in learning other android components and tools:
- ToDo
- Introduction to Github Actions for Android blog by Mindorks on how to set up GitHub actions for an android project.