49

Flutter and Kotlin Multiplatform relationship - Kt. Academy

 5 years ago
source link: https://blog.kotlin-academy.com/flutter-and-kotlin-multiplatform-relationship-890616005f57?gi=4c6f518311fc
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.
neoserver,ios ssh client
Image for post
Image for post

Flutter and Kotlin Multiplatform relationship

Kotlin Multiplatform and Flutter are often considered as competitors. It is just the opposite. Their ambitions do not overlap, and they might help each other a lot. All that is needed is Kotlin and Dart being interoperable. As long as they are not, they are competitors. Let’s talk about this hard relationship that will most likely shape the future of Android.

What is Flutter?

Flutter is a front-end library (or a framework) for Android and iOS development (also has experimental support for Web and Desktop). This library offers a single API to define views for all those platforms at once. It also provides a common API commonly used services like database or network. Flutter’s API is designed for Dart — language compiled to C/C++ code with NDK on Android, and LLVM on iOS. Here is a sample Flutter application:

import 'package:flutter/material.dart';void main() => runApp(MyApp());class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Welcome to Flutter',
home: Scaffold(
appBar: AppBar(
title: Text('Welcome to Flutter'),
),
body: Center(
child: Text('Hello World'),
),
),
);
}
}

Kotlin Multiplatform

Kotlin Multiplatform is a Kotlin feature. Kotlin can be compiled to JVM bytecode (like Java), JavaScript or for native LLVM code. This way, we can use it for Android, iOS, Web, Desktop, and Backend development in Java frameworks like Spring.

Related image

We can also have some parts that are shared among those platforms. Such parts are called common modules, and they are used in modules that are platform-specific.

Image for post
Image for post

Platforms can be written in different languages, for instance, we generally use Swift to develop iOS applications in this approach. We can extract how much we want above those platforms and have it implemented once instead of separately. Kotlin Multiplatform is especially interested in extracting common logic — how the application behaves. This logic should be independent on frameworks, and similar or the same for all platforms anyway so it is natural that is should be extracted — more about Multiplatform Kotlin development in this video.

Are they competitors?

The ambitions of both platforms do not really overlap. They are both trying to extract common parts above platform, but Kotlin Multiplatform is interested in logic extraction when Flutter is interested in view definitions extraction. They concentrate on the opposite, and they might highly benefit from working together.

Image for post
Image for post

How can they help each other?

A big Flutter problem is that it is hard to migrate from Android to Flutter. Changing both language and frontend framework is nearly impossible for well-developed applications, and rewriting everything is too expensive. Kotlin Multiplatform situation is different — most Android applications are already in Kotlin anyway (or at least using Kotlin, according to the last Google IO). When there is a good architecture like MVP or MVVM, migration to Kotlin Multiplatform is really cheap as everything is already implemented and only needs to be moved and reused on iOS.

Another problem of Flutter is history — it shows that the average lifetime of frontend libraries is short, and companies do not like reimplementing applications just because they chose framework and language that didn’t stand the test of time. Those are the biggest problems of Flutter:

  • hard to migrate to,
  • hard to predict what will be the long-term future of this solution.

Here Kotlin Multiplatform can come for a rescue! If Kotlin would be interoperable with Dart, typical migration to Flutter could look like that:

  1. Introduce a good architecture like Clean, MVP or MVVM if it is not yet there.
  2. Extract logic and services that are supported into Kotlin common modules (cheap step if we have a proper architecture which demands those parts to be independent on libraries anyway).
  3. Reimplement views in Flutter, and for logic use Kotlin common module.

If this is done correctly, the most essential parts of our applications should be in the common module, and so migration should not be painful. To some degree it will solve both Flutter problems:

  • Migration to Flutter will be much easier as we will only need to reimplement views, not logic.
  • If Flutter would start losing popularity to some other frontend framework, migration will be relatively cheap again because we can assume that most likely language used there will be interoperable with Kotlin.

The list of languages interoperable with Kotlin is impressive, and it grows:

  • Java, Scala, Groovy, Clojure…
  • JavaScript, TypeScript…
  • Swift, C, C++,…

As a result, we will achieve the best of those two worlds:

  • We will write our logic in Kotlin, which is great for that, supports coroutines, and can be reused nearly everywhere, including backend.
  • We will write views in Flutter, once for all platforms, with all great Flutter features like Hot Reload.

Where is the problem now?

The apple of discord in this relation is the Dart. There is a holy war between Kotlin and Dart. There is also a discussion if Kotlin should be used instead of Dart for Flutter development. Kotlin is now the primary language of Android development, and so reusing Android code and moving from Android to Flutter would be much easier. It is also more developed and featured language. This discussion is serious and often fired-up, especially since Kotlin allows DSL creation that would give Flutter shorter and nicer syntax for view definition. This is how Flutter could look like if Kotlin would be supported (it is just my imagination here):

import flutter.material.*fun main() = runApp(MyApp())class MyApp: StatelessWidget {
fun build(context: BuildContext) = materialApp {
title = "Welcome to Flutter"
scaffold {
appBar = AppBar(title = "Welcome to Flutter")
center {
text("Hello World")
}
}
}
}

It would make most of Android community happy to be able to use Kotlin instead of Dart on Flutter, but it wouldn’t be good news for Flutter developers who already invested in learning Dart instead of learning Kotlin. Also, Google prefers Dart because it is their language and they have full control over it. No wonder why Kotlin and Flutter communities have troubles with communication.

I believe that the holy war between Kotlin and Dart is the most important source of conflicts between Kotlin and Flutter developers. I see no solution to that except… eee… proper interoperability between Kotlin and Dart? It would allow us to write whatever Flutter part we want in Kotlin, and whichever we want in Dart, and we would have better code reuse from Android SDK projects.

Image for post
Image for post

Honestly, I am not even sure if such interoperability is possible. And by that I mean if it can be done in a reasonable time (as everything is possible in programming) :P Though I am quite sure that it would need cooperation between Flutter and Kotlin team. It is in the interest of both those teams. Flutter will benefit from that because Kotlin Multiplatform would make it easier to migrate to Flutter, and such migration will be safer. Developers love Kotlin, and lack of Kotlin support blocks many. Kotlin will also benefit a lot from that as Flutter might grow big, and in this scenario, Kotlin will grow with it. It would also make me much happier, as I like Flutter and I love Kotlin, and I would love to use them both at once.


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK