Kotlin code sharing among platforms

When you try to deal with the across platform codes, you need to solve 2 things, one is what to share, the other is the architecture of how to share the code.

Different languages have different techniques to the second problem, but in Kotlin, you can use multiplatform projects to share the code.  With kotlin native, you can even expand the support to natively to iOS and Android.

1. Goals

  • share code among JVM, JS, iOS, and Android.
  • enable the use of XCode and Android Studio to edit the native app, while using another IDE to edit the sharing code part. (But you can use Android studio for editing the KN code as well because they use the same language and the setup will be ready.)
    • I saw some setups. They use root folder as a whole gradle project which will affect the Android project structure been displayed in the android studio because it will add some noise there.
  • share more code between iOS and Android.
    • The main hurdle is the platform-specific API. We can make them as `interface` and still shared the logic around them. But here we will use `kotlin multi-platform` approach to make the code more elegant.
    • Think of an API like this `UserService.getAllorNull()`, then your native side just need to bind the result to the UI. Even though the `HTTP` call is different across platform. But all the exception handling and data processing around the http call are the same and could be shared.
  • a clean structure where everything related should reside together.
    • I mean for the gradle phase as well. Such that, `android` folder is only for Android, `ios` folder is only for iOS. The root folder is for all projects. And the gradle settings for `KN-android` should be in the `android` folder as well as the `iOS` side.
  • enable testing the Kotlin native code as well.
    • It’s native project so anything native can be tested via the strategy on that platform. But what about kotlin native part, what about the `common` code? What about the kotlin native platform specific code for `android` and `ios`? Yes, we have that part covered as well here in the example.

1.1 What if there is no need to care about the platform-specific implementation?

If all you need is the API from kotlin-stdlib, and no platform specific code, then you don’t need to use this setup. You can simply remove the platform folder. And let the common just output an iOS framework for the XCode project to consume. For Android, embed the source code in Android studio to make it as dependency, then it’s fine. Same case for JVM and JS as well. And the setup for JVM and JS is as easy as following the official document.

2. Folder structure

  • ios: XCode project
  • android: Android Studio project
  • common: Common code that is about to share across platforms without any change.
  • platforms:
    • ios: platform specific API for iOS
    • js: platform specific API for js
    • jvm: a dependency for both `android app` and `jvmApp`

Very easy to understand. One interesting thing here is in the platforms folder, besides the common folder. What’s the platforms/android and platforms/ios folder for? Well, they are for platform-specific API.

2.1 How to consume the lib

  • JVM and JS just as easy as including the lib as the dependencies in build.gradle.
  • Android related code will be shared via simply including the according module in the android project.
  • iOS related code will be shared as an iOS framework because Kotlin native will generate that. We will then add it to the XCode building phase. You can see more details on my another [blog](http://www.albertgao.xyz/2018/01/14/how-to-create-kotlin-native-ios-project/).

2.2 Background knowledge for Kotlin multi-platform project

Let’s say you want to have an HTTP kotlin class where you can do the normal GET, POST thing, but the HTTP implementation is different across platforms. If you use the old school interface way, you need to declare an IHTTP interface, then implement it twice in android and ios. Then inject it at runtime to make it work.

But via using the Kotlin multi-platform project, you don’t need to inject, the compiler will only pick up the piece of codes depends on your building target. If you build for Android, it will only grab the Android implementation.

It currently works for jvm, js and kotlin native, oh, that’s a nearly-all-platform code sharing tech right? 😀

And it’s very simple.

You need to expect an implementation of your common code.

expect class Platform {
    fun get():String
}

Then implement the actual code in somewhere else, like android.

actual class Platform {
    actual fun get():String {
        return"android"
    }
}

Or in ios:

actual class Platform {
    actual fun get():String {
        return"ios"
    }
}

Then with some proper setup in gradle, it will work flawlessly.

2.3 How to code the `platforms-ios`

Oh well, you may think that old school interface may better in terms of iOS because you need to implement the iOS API specific thing in swift or obj-c then pass it back to kotlin native. But that is not true. Because there is a 1-to-1 API match in kotlin native such that you can implement the iOS specific API in kotlin as well. And it has the same signature which means you can reuse all the existing knowledge. And kotlin and swift are very similar. And from v6, the building phase will link some of the iOS API (you need to build the others) for you if you are on a Mac.
Which means in an ideal world, your swift side just needs to care about the UI part while all other logic will be handled in the Kotlin native side, shared across platform and tested well.

3. About the example

All the code for setup can be found in this repo.

  • `Sample` class is for code that is sharing across platforms (Which means you have to use API from `kotlin-stdlib-common`).
  • `Platform` class is a class which has been implemented twice for different platforms for showing the platform API case.
  • Open `android` folder from the root in Android Studio, run the app, it will show a string from the `:shared-android` project.
  • Open `ios` folder from the root in XCode, run the app, it will show a string from the `:platforms-ios` project.

In fact, all the apps (jvmApp, jsApp, android and iOS) retrieve the string by invoking the method from the Sample class. And the Sample class invokes the method from Platform class to get the string. When you build an iOS framework, the KN compiler will use :platforms-ios to build along with :common. And when you consume it in android project, the setup will use :platforms-android along with :common. No injection, no affection on the code structure, just that simple. Thanks to the Multi-platform project setup from kotlin.
It is a perfect example of showing how to share the code when you have to deal with the platform API.

Hope it helps. Thanks.

Advertisements

2 thoughts on “Kotlin code sharing among platforms”

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s