Translation Instructions:
My first experience with Kotlin Native
Original address:medium.com/@champigny.
Original article by Florent Champigny
With the recent release of Kotlin 1.3, I’ve heard about the ability to create multi-platform mobile applications 🤔
Can now share code between my Android and ios apps?? What’s the magic behind this?
Let me show you how I successfully created my first Android and iOS compatible module and how to integrate it into an actual application.
Kotlin can compile to any platform
The latest version of Kotlin enforces the concept of multi-platform languages. You can already generate JVM binaries and JS packages using Kotlin, and now you can generate Native frameworks!
For native I would say iOS code, but not just! According to the official documentation, you can generate code that is compatible with many native architectures:
I’ll focus only on Kotlin-native iOS compatibility and show you how to generate mobile apps that share Kotlin code compatible with Android and iPhone!
How did I create my multi-platform project
Mhhmmm makes me wonder what is the perfect IDE, compatible with Gradle and Android 🤔
Of course IntelliJIdea 😁 (you can also use Android Studio)
To create my project, I copied a branch of the official Kotlin-conf application made by JetBrain and modified the source code!
You can also configure multi-platform projects from 0 according to the official documentation
This is a simple app I made where you write a Github user’s login name and it shows his full name, his avatar and his company. It’s not a complex application, but it contains some Http calls, a Json deserialization, and can be architected using MVP
How about multi-platform projects?
The multi-platform project consists of three modules:
- Common: Contains the Kotlin common code
- Android: Contains Android applications
- Appios: contains the Xcode project
You can define platform-specific implementations directly from within these modules or public modules: here iosMain & jvmMain
Generate the frameworks
Kotlin uses Konan (an LLVM compiler) natively to generate.framework from our generic modules. For Android developers:.framework is equivalent to.jar
You must import this framework in your XCode project:
Understand the Kotlin type
Kotlin was originally created to generate JVM bytecode.
But using only Kotlin classes: Int, Double, List, Map, etc., you can now bind these objects to the platform implementation.
You can see the current implementation looking at your generated.H. framework, using the generic class:
It will generate this code in the common.h file:
What can I put commonly used modules into?
The response is simple: almost anything that doesn’t depend on the UI or platform!
I am developing MVP for my project and I added Presenter, its view, repository, API and model to my public module:
The only platform-specific layer here is the API, and let me explain to you the different ways to get different code depending on the platform:
Platform-specific library implementation
Use the same logic in product styles on Android: All styles should expose the same public classes (if they are used by our project), you can specify specific Gradle dependencies, here is the HTTP Ktor library used in my GithubAPI:
From my GithubApi, I can use the HttpClient class, which has different implementations on Android and iO
Note that you can use the Kotlin coroutine to perform asynchronous methods in our generic module!
Using Ktor, I used the Kotlinx-serialization lib to decode json into a model
Write iPhone code with Kotlin
Kotlin-native allows you to compile generic code into frameworks and bind types to platform types, but it also allows you to write iPhone code directly in Kotlin!
I’m not encouraging you to create all iOS classes in Kotlin, but you can write some platform-specific versions directly in Kotlin. Let me show you an example if I create an interface in a public module for simple storage:
You can provide its iOS implementation directly in the generated.framework by creating a file in the iosMain module and then writing code in Kotlin using the iOS class:
Inject platform-specific implementations
You can interface inside a public module, such as PlatformLogger:
I then created the implementation on each platform, here is the Android App module:
In my XCode project, I created a PlatformLoggerIOS that implements the PlatformLogger protocol:
We can supply these PlatformLoggers directly from the constructor as dependencies on the G ithubRepository. I used this to create a class named DependencyManager, do some lazy initialization, and store it in the Application on Android and in the AppDelegate on iOS:
In my code, I can use GithubPresenter!
Create an Android application
I just need to create a fragment and its layout, and using my DependencyManager I can retrieve my GithubPresenter and listen to the EditText content to download and display the Github user.
Finally, create my XCode application
Using a storyboard and a simple ViewController, I implemented my GithubPresenter view:
Here my GithubController implements GithubView and retrieves the Presenter through my DependencyManager
I can use my presenter to download github users from the contents of my TextField and then easily display it
My view of Kotlin-native
What you can imagine for Kotlin’s native future is amazing!
Imagine when all the libraries we used would be implemented in Core/JVM /ios, we could write all the architectural code in that framework and only develop the UI on each platform!
Today, creating a multiplatform application is not easy, intelligent auto-completion is dangerous, you run into random problems when compiling code, and you can’t easily attach a debugger to your common code. Some Kotlin code does not have their equivalent in Swift, for example I did not succeed in creating an adjoint object for my logger (I tried to recreate concepts like Timber from Jake Wharton)
But don’t forget that this is a very young technology! Not a few months ago!
I impatiently learned about Kotlin-Native and created some new libraries that were compatible with Android and iPhone
I’ll give you the code for my Android /ios app again, don’t hesitate to send me some comments! My lot