This project evaluates the use of Kotlin Multiplatform for Android and iOS. The main reason to use such an approach is to be able to share code such as entities, business logic but also the network connection and database storage.
The project uses gradle as build system. A gradle project consists of at least 2 modules: the project itself and at least one other module.
This project has the following structure:
project
|
|- android
|
|- shared
|
|- ios
Besides the project, there are 3 modules, which encapsulate special logic:
The SharedCode module is the library module, which contains code that can be propagated to Android and iOS.
Within the module itself there are three folders:
...
|- shared
|
|- androidMain
|
|- commonMain
|
|- iosMain
...
Kotlin Multiplatform works as follows:
The commonMain package defines classes which can be used in the android or ios gradle modules. If there is a functionality which contains platform specific code the two packages androidMain & iosMain are used. In this case the commonMain package declares an expect placeholder and the platform-specific packages implement the actual logic.
This project uses this feature.
The android module contains the Android app.
It simply uses the shared code module by implementing it via gradle:
implementation project(':SharedCode')The ios module contains the ios app and can only be opened in Xcode. The app consumes the data provided
by the repository in a ViewModel and provides the data - once there - to the ui. The UI is built with Swift UI
and uses a declarative pattern. The shared module is implemented by the use of cocoapods.
The shared module shares
- a network client (Ktor),
- a SQLite database (SQDelight),
- a Repository class which stores the network data in the database and only routes to the network if explicitly desired or if no data is found in the database.
To be able to work with asynchronous code it makes use of Kotlin Coroutines.
To get started you have to do the following things first:
- Build and setup
cocoapodsfor ios - Prepare the ios project to be able to use sqlite as SQDelight uses a sqlight database.
The project uses the cocoapods gradle plugin to be able to easily integrate the shared code module into ios.
To generate the respective pod run
gradlew :SharedCode:podspecbefore, then - from within the ios module - call pod install before building.
To be able to use the shared database in ios you have to enable sqlight support in Xcode. Therefore,
- Open Xcode
- open the workspace
KotlinIOS - Select the target
KotlinIOS - Select the tab
Build Phases - Under
Link Binary with Librariesaddlibsqlite3.tbd