Kotlin Multiplatform for Android and iOS


90 points | by andraskindler 221 days ago


  • oropolo 221 days ago

    In essence, JetBrains has caught up to where Xamarin was prior to the announcement of Xamarin.Forms: the ability from one solution to have separate iOS and Android UI applications with a business logic library shared between the two, all written in C# or F# (the two languages supported by the Mono compiler). The main difference is that one of the vendors actually supports the language in question (Kotlin) as a first-class citizen.

    And going the other way, there's been noise for a couple years that Android apps could be written in Swift, achieving the same effect in reverse.

    • the_trapper 221 days ago

      > The main difference is that one of the vendors actually supports the language in question (Kotlin) as a first-class citizen.

      How is this different for Xamarin? C# has first class support on Windows.

      • oropolo 221 days ago

        Windows isn't a mobile platform. They tried 2.5 times and have given up. In mobile it's iOS and Android, and neither Apple nor Google recognize C# as a first-class language. If you can write a compiler that emits code that LLVM can compile to ARM binary with Xcode or package your MSIL with a micro .NET VM on Android, then more power to you but Apple and Google aren't going to help you if you get stuck.

        • pjmlp 221 days ago

          Windows tablets, laptops and 2-1 devices are quite mobile to me.

          • rad_gruchalski 221 days ago

            Prerty difficult to find a good dash mount, no usb charging, bit bright at night and having to plug in an external gps unit is fairly limiting though.

            • pjmlp 221 days ago

              The car computer already takes care of that.

            • 221 days ago
              • oropolo 221 days ago

                And an AS/400 was perfectly "mobile" to Arnold S. back in his prime. :-)

            • tomc1985 221 days ago

              Xamarin's "first class" support for Windows is Windows Store only though?

              • merb 221 days ago


                While still in Preview, Xamarin.Forms actually supports Linux with Gtk#, Windows with WPF and UWP and Mac with Cocoa (actually Cocoa with C# does work really well).

              • avisser 221 days ago

                The context of the article is about iOS & Android apps, so 1st class support on Windows is nice, but not germane.

            • dglass 221 days ago

              The thing that I still don't know about are how would things like data persistence be handled in a cross platform manner?

              I recently built a kotlin Android app for work, and our architecture is tightly coupled to Android's Room database and LiveData objects. Obviously if we build it to be cross platform we wouldn't be able to use a Room database or LiveData objects on an iOS device. Would we have to roll our own persistence layer in a cross platform application? Or would we share business logic but still write a persistence layer using platform specific code?

              It's not just persistence too. Any lower level API provided by the platform would still need to be written twice, right? Accessing the camera, the devices GPS location, or anything hardware related still needs an implementation for both platforms. Or would we eventually see libraries that would abstract this layer out, similar to react native?

              Edit: Another thought...I haven't done much iOS development but the whole async nature of Android apps with the UI thread and background threads also seems like it would be a nightmare to deal with in a cross platform way. Does iOS involve a lot of async threads?

              • abukros 221 days ago

                I think the way is to write libraries for these use-cases. There is already a persistence lib for Kotlin Native called KNArch.db https://github.com/touchlab/knarch.db and it supports Android too.

                We are already using libraries to make easier to use background threads (RxJava, RxSwift) so I don't think it's that hard to implement a cross platform lib for that purpose. JetBrains is already trying to tackle the task with Coroutines for Kotlin Native: https://github.com/Kotlin/kotlinx.coroutines/tree/master/nat...

                • vbezhenar 221 days ago

                  I'd suggest to use SQLite as a persistent storage. It's very fast, convenient and cross-platform.

                  • sidlls 221 days ago

                    Room/LiveData are abstractions over the persistent store but by default/in practice they abstract over SQLite.

                • jaegerpicker 221 days ago

                  Looks great! My biggest concern and one of the reasons I'm not using Swift or C++ for a cross platform library is that most of the dependencies you (at least I need) need don't support those languages (Auth0, Firebase, Crashlytics etc...). So you end up writing more complex code to make that also work. If Kotlin native can bring those types of libraries to be supported it will be huge IMO.

                • hashrate 221 days ago

                  Seems like a gimmick to me.

                  In a nutshell this has the same limitation has Xamarin.

                  The Business Logic is shared , but the UI Logic and the Technical Logic aren't shared or not completely.

                  The Xamarin community has been struggling with this issue for half a decade and they ended up re-writting their own rendering engine[0] (similar to Flutter) in C# on top of Xamarin to obtain truly MVVM Cross-Platform Framework.

                  My point here is very simple , getting Kotlin to run on iOS is great, but it's somewhat a waste of time because of how much time and effort it would talk to create a Runtime or Rendering Engine to normalize UI/UX on differents platforms.


                  • kpgalligan 218 days ago

                    It's similar, but not the same. The details matter. They're different because you don't need to deal with a complex abstraction layer to share logic. As in, you can expose an iOS Framework and call it from swift/Objc just like any other library. That lets you share tested logic and architecture, and do so optionally. It's not all-or-nothing. Xamarin is a different approach. Flutter is a very different approach.

                    Part of the issue when evaluating frameworks is expectations and not understand the plusses/minuses. If you are looking for a framework that will do 1 codebase for all logic and UI, without sacrificing any UX or capabilities, you'll probably be let down. Kotlin Multiplatform will be great for sharing logic and architecture. UI is somewhat of a different case. Why Xamarin wasn't great here is you need to do EVERYTHING in Xamarin, and write custom bridge code when not. It's an entirely different world, which bad IDE support for most of it's history. Jetbrains makes excellent tools, and if you understand that the UI will be "native", then there's a lot of efficiencies you can leverage here. If you get frustrated because you can't make one thing do all things, then yeah, it'll let you down.

                    Anyway, yeah, they're different.

                    • aikah 221 days ago

                      The difference is that Kotlin has first class support on Android. Aside from that I agree, Kotlin is obviously second class on iOS. IMHO for simple multi-platform projects I'd use either plain webtechs or react-native. The impedance mismatch between Kotlin and Swift/ObjC is going to be an hindrance no matter what.

                      • pjmlp 221 days ago

                        JavaScript is also an impedance mismatch.

                        • aikah 220 days ago

                          > JavaScript is also an impedance mismatch.

                          Yet both Android and iOS allow embedding a Javascript engine in an native application with no effort whatsoever.

                          • pjmlp 220 days ago

                            I wouldn't call manually exporting Java methods to the WebWidget "no effort whatsover", plus the performance hit of doing cross-language, inter-process calls, in a dynamic language while the platform languages are static.

                            • aikah 220 days ago

                              > I wouldn't call manually exporting Java methods to the WebWidget "no effort whatsover", plus the performance hit of doing cross-language, inter-process calls, in a dynamic language while the platform languages are static.

                              Well like it or not JS is first class on both platforms as they both have a JS engine at the developer's disposal, Kotlin or C# aren't.

                              • pjmlp 220 days ago

                                I don't know about iOS, but on Android it surely isn't first class, the development experience is even worse than using the NDK.

                                If it was first class, there would be official Android APIs for JavaScript, debugging support on Android Studio, project templates on Android Studio.

                                Instead it is a web widget with its own little island of HTML 5/CSS 3, hardly first class.

                                The support is not much different than getting chromium and compiling it with the NDK.

                                The only mobile platforms where JavaScript is first class, alongside the other platform languages is on ChromeOS and UWP.

                                • aikah 220 days ago

                                  First class because there is no need to deploy your own JS engine on both these platforms, you can argue all you want, both have a webview/js engine API that are part of their respective SDK.

                                  • pjmlp 220 days ago

                                    That is not what first class means.

                                    A language is first class when the complete SDK stack tooling has support for it.

                                    IDE, debugger, docs, project templates, profiler, OS APIs.

                                    • aikah 219 days ago

                                      > That is not what first class means.

                                      That's not what first class means to you. You can't just make up the definition you want just because it's convenient for your. First class means it runs on the platform without any form of external or third party runtime.

                                      • pjmlp 219 days ago

                                        The point is that you want JavaScript to be seen as first class no matter what, because it fits the React Native story, even though it requires manually written FFI for the platform APIs.

                                        Hey, by your definition even web pages are first class on mobile devices, as they don't require any form of external or third party runtime, maybe we can even call them native apps!

                                        If JavaScript doesn't require any form of external or third party runtime, then why does React Native bundle JavaScriptCore?!?


                    • theWheez 221 days ago

                      So, so excited for this. I would love nothing more than to be able to create Model and ViewModel code a domain specific cross platform SDK, and have a View implementation on a platform by platform basis for android, iOS, and web. Possible? I sure hope so!

                      • jaxondu 221 days ago

                        What is needed for more adoption is Kotlin SDK for Firebase & AWS.

                        • masterp 221 days ago

                          Looks promising!

                          • andraskindler 221 days ago

                            Yup! Still not production-ready, but we're getting there.