35

My first experience with Kotlin Native

 5 years ago
source link: https://www.tuicool.com/articles/hit/ma2Ezur
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

With the recent release of Kotlin 1.3, I heard about the capability to create multi-platform mobile applications

yA3eyev.png!web

It’s now possible to share code between my android & ios apps ??? What’s the magic behind that ?

Let me show you how I succeed to create my first Android & iOS compatible module, and how to integrate it inside real applications.

Kotlin can compile to ANYTHING !

Kotlin’s last release enforce the concept of a multi-platform language, you already can use Kotlin to produce JVM binaries and JS bundles, it’s now possible to produce Native frameworks !

By native I mean iOS code, but not only ! Following the official documentation , it’s possible ton generate code compatible with a lot of native architecture :

MZruUne.png!web
Kotlin-Native Target Platforms

I will only focus here on the Kotlin-Native iOS compatibility, and demonstrate you how to generate a mobile application sharing kotlin code, compatible with Android & iPhone !

How I created my multi-platform project

Mhhmmm let me guess what would be the perfect IDE, compatible with Gradle and Android

Of course IntelliJ Idea :grin: (you can also use Android Studio)

To create my project, I forked the official Kotlin-Conf app made by JetBrain, and modified the sources !

You can also start from 0 by following the official documentation to configure a multi-plateform project

Here’s my repo :

Here’s the simple app I made, you write the login of a github user, and it displays his complete name, its avatar and his company. It’s not a complicated application, but it contains some Http call, a Json deserialization and can be architectured using MVP

6zuIFfQ.png!web

How a multi-platform project looks like ?

En6zIfv.png!web

This multi-platform project is made by 3 modules :

  • common  : contains kotlin mutualized code
  • android  : contains android application
  • appios  : contains the xcode project

You can define platform-specific implementations directly from these modules, or inside flavors of the common module : here iosMain & jvmMain

Generate .frameworks

Kotlin native use Konan, which is a LLVM compiler, to generate a .framework from our common module. For Android developers :  .framework are the equivalent to  .jar

jMj63uM.png!web
Frameworks location

This framework has to be imported in your XCode Project :

ieU3Ybz.png!web
Import Framework

Understand Kotlin Types

Kotlin has been initially created to produce JVM bytecode.

But using only Kotlin classes : Int, Double, List, Map, etc, it’s now possible to bind theses objects with the platform implementations.

aMr2iir.png!web
How Kotlin types as bounds

You can see the current implementation watching the generated .h of your .framework, using this common class :

Jrqyuei.png!web

It will generate this kind of code inside the common.h file :

qeQJ7nV.png!web

What can I put my common module ?

The response is easy : almost everything that’s not UI or platform-dependant !

I’m working on MVP on my project, I added the Presenter, its view, the repositories, the api and the models inside my common module :

MfQjeea.png!web
Content of the common module
vAJfE3f.png!web
Common module hierarchy

Here the only platform specific layer is the API, let me explain you different ways to have diferents code depending on the platform :

Platform-specific Implementation by library

Using the same logic using in product flavors on Android : all flavors should expose the same public classes (if they’re used by our project), you can specify specifics Gradle dependencies, here for the HTTP Ktor library , used in my GithubAPI :

Q7j6ZnJ.png!web

From my GithubApi, I can use the class HttpClient , which has a different implementation on Android & iOs

eu2ammE.png!web

Note you can use kotlin coroutine to execute asynchronous methods in our common module !

With Ktor I used the kotlinx-serialization lib to decode json as models


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK