Kotlin Native iOS Development and Multiplatform Project

Initially Kotlin was started for JVM but after some time Kotlin evolved as modern statically typed programming language and Jet Brains team decided to allow Multi-platform project development in Kotlin.

Multi-platform means write code for your Android, iOS, Web application and Backend in a single project using Kotlin (Language) Native.

This article cover following topics.

  1. What is Multi-platform project in Kotlin ?
  2. What is Kotlin Native Development ?
  3. How to setup Multi-platform project structure in Kotlin ?
  4. Kotlin Native platform specific modules.
  5. Kotlin target platforms.
  6. iOS Application Development in Kotlin Native

The idea of kotlin Native is to allow Multi-platform development introduced in Kotlin 1.2.

Kotlin Native compiles code written in kotlin to different binaries for different platforms  (so kotlin can be used for iOS Development, Server Side Development, Web Application Development,  Android Development ) using LLVM.

Means Kotlin Native allows code re usability between different platforms. These native exectuables run without any virtual machine.

Kotlin Native currently provide support for these platforms

  • Windows (x86_64 only at the moment)
  • Linux (x86_64, arm32, MIPS, MIPS little endian)
  • MacOS (x86_64)
  • iOS (arm64 only)
  • Android (arm32 and arm64)
  • WebAssembly (wasm32 only)

Shared Code in Kotlin Native

Following Business logic related things can be shared in kotlin Native Multi-platform project

  • Data Validation
  • Data Models
  • Network client code
  • HTML generation

Which part of code cannot be shared in Kotlin Native

  • Platform specific/dependent Libraries.
  • UI

Because Kotlin allows developers to reuse Native User Interface controls and platform specific code.

IDE Support for Kotlin Native

Kotlin Native projects can be developed in CLion IDE by Jet Brains.

You have to install two plugins in CLion, Kotlin and Kotlin Native.

Kotlin Native Plugin
Install Kotlin Native Plugin

Creating Hello World Kotlin Native Project

Kotlin Native ClioN IDE
Kotlin Native Create new project in CLion IDE

Kotlin Multiplatform project Modules

Kotlin Native Modules
Types of Kotlin Native Modules

Each Kotlin Multiplatform has two types of modules.

  1. Common Modules
  2. Platform specific Modules

Platform module will have access to all libraries and code for that platform, whereas common modules can use common libraries and classes.

In this article we will also explore source code of kotlin spinner Application available at Github.

And we will see how the code is structured for Server, IOS, Android and web Application.

Multiplatform project in Kotlin 1.2 is experimental. So all the tools and features are subject to change in future.

Multiplatform project in Kotlin have to be built using Gradle. No other build system is supported.

Kotlin Native supports for interoperability. For platform libraries corresponding libraries are available. For other libraries kotlin provides a  tool to generate an interop library .

Kotlin Native can call C functions and get/set data to and from C functions. At build time developer can generate Kotlin header and can get type-safe to Native API of the target platform (iOS, JS,JVM)

Kotlin/Native provides different Memory managment solutions for different platforms.

We will be discussing Kotlin Spinner Application proposed to exhibit capabilities of Kotlin Native.

All of the modules of the Application are written using Kotlin Native and it consist of following components:-

  • Backend (Server Side), Backend is developed using the microhttpd framework.
  • Mobile Clients (Kotlin Native Android and Kotlin Native iOS)
  • Web Application Development using Kotlin Native -> Web Assembly front end which can fetch and display game stats in browser.

Website is developed using WebAssembly.

Website is developed using Kotlin/JS and React framework.

Another multiplatform project KotlinConf. Its website is devloped using Kotlin/JS and React. And its Backend is developed using Ktor framework.

Kotlin Native Multiplatform Project Structure

As You can see in above image. Multiplatform project in Kotlin is categorized into three main modules.

  1. Common Module

Contains shared code and is not specific to any platform. It only contains declarations without platform specific implementation.  Common code depends on platform specific implementation.

For example:-

If we need a Date object in our Android, iOS and Web application in Kotlin Native project. All platforms have different implantation for getting Date, Time. So we can define our own class Date in our project and its implementation will be in Platform specific module.

  1. Platform specific Module

Since we are targeting Android, iOS, web and server in Kotlin Native. So we have modules for each of these. Each platform specific module will have platform dependent code. Also a platform module is always implementation of single common module.

  1. Regular Module targets a specific platform.

Each module will have its own gradle build file.

Now Let me show you iOS module structure in Kotlin Native.

kotlin ios module
kotlin native iOS module

iOS libraries used in project are located in Frameworks folder.

Images and Storyboard files for iOS project are located in Konan folder.

Future of Kotlin Native

We will continuously update this article as Kotlin/Native progresses.

Subscribe now to get latest updates on Kotlin Native.

Recommended Reading :-

Source code can be found at : https://github.com/jetbrains/kotlinconf-spinner

Refrence : https://kotlinlang.org/docs/reference/multiplatform.html

6 thoughts on “Kotlin Native iOS Development and Multiplatform Project”

  1. While installing Clion after installing Kotlin plugins we are facing Toolchains are not configured Configure error.

Leave a Reply

Your email address will not be published. Required fields are marked *