295 points · by doppp · 6y ago · 110 comments
For me it makes perfect sense to e.g create an internal API client using this. You use platform specific frameworks for making the actual HTTP requests[1], but can write all higher level business logic in Kotlin and expose that. Adding a new REST route or change data validation would be done only once and be in sync on all platforms.
But I'm guessing that you have to pretty knowledgeable about the underlying platforms to know how (and if it's even possible) to create a good abstraction above the platform code. For something like audio/video playback or BLE, I'm guessing that there might be enough differences between Android and iOS that an abstraction that fit both is really unwieldy. And even if you create something resonable today, one of the platforms might introduce new features which destroys your abstraction.
[1]: I noticed that they even have some kind of built-in HTTP client abstraction: https://kotlinlang.org/docs/mobile/use-ktor-for-networking.h...
It goes one step further, you can have platform-neutral code to parse the API returns into Kotlin models.
https://blog.jetbrains.com/kotlin/2020/10/kotlinx-serializat...
A lot of business logic is shared in the domain layer on each platform client (and possibly on the server), and I think re-use of that is/will be Kotlin's main selling point.
Add SQLDelight for shared data access code on clients, and you can cover a lot of ground with one codebase.
As someone who has had to maintain apps across web, iOS and Android, the answer to that is most definitely no.
source: have worked on apps you likely use every day
At what size and level of expertise/experience does it make sense to consider it?
What are the prerequisite knowledge/skills/abilities that a two pizza team needs to keep the learning curve practical?
Just my personal experience, but if you have a good Android/Java "two-pizza team", you probably already have the makings of a good Kotlin team, given the appropriate time for folks to pick it up.
For a two pizza team, I'd recommend not overthinking it and to just give it a try if it makes sense for you or not if it doesn't. Hard to tell; it all depends on your requirements and how good your people are.
So the platform teams need to have shared prior knowledge of how to build with the same architectural patterns, and they need to be sold on the possibility and merits of using those same patterns and coordinating on their plans of implementation.
Aside from Kotlin I also think Rust with C FFI or WASM are good potential avenues to explore here. In the WASM case generic business logic can be written in Rust and compiled to WASM, platform capabilities such as networking/GPS etc can be exposed to the WASM VM via functions provided by the host. Further, with WASM it would also be possible to run the same code across web, native clients, and the server.
For this reason, I’m hesitant to make use of a shared codebase for the networking parts of an app. By using your own stack, you’re throwing out a ton of device-specific optimizations and risk things like keeping the user’s cell antenna activated for longer than needed or not gracefully handling transitions between connection types.
For most mobile apps, I think the only part that really makes sense to share between platforms are those to do with pure logic and data manipulation, since there’s not as much benefit from doing that full native, and any processing optimizations are typically surfaced in a way that’s accessible to lower level languages (and thus third party stuff like Kotlin Multiplatform).
An added bonus of this approach is that it forces you to have a clear separation between your user interface code, and your business logic. A lot of mobile (and front end) apps out there have no architecture, putting API calls, state management and UI right next to each other in the same modules. Our approach forces you to keep UI code just focused on UI, while keeping all logic somewhere else, where it can isolated and as a benefit much more easily unit tested.
You would be surprised just how large of a share of your codebase can be moved into platform agnostic code if you have a solid architecture. Our UIs are "dumb" and render themselves from view models which have exactly the data needed to render the view. All the logic and state management to construct these view models can be written in the cross platform repo and shared across the Android and iOS apps. For example, to render a list, you will need to use the native UITableView or RecyclerView separately, but all the code that manages how many rows are in the list, the data each row needs, as well as updating the data when the list is reordered can be shared across platforms.
If you're business logic needs these platform specific functionalities (which will definitely be the case as you've stated) then you can abstract out interfaces and let each platform handle them natively.
> Almost 50% of the production code in our Android and iOS apps is decoupled from the underlying platform.
Kotlin multi platform as described here has no UI system, it’s a way of compiling Kotlin to binaries that are easily linked to both Android and iOS apps, with some bridging back and forth conversions into the native systems in each case. You’ll still need two separate code bases to call the common binary and handle UI.
Once you have that setup you can then add a different platform "web/iOS". The base module would have setup certain contracts that new "platforms" would have to implement.
Try spending one or two evenings with Flutter writing a simple app and build it for two or three platforms to see what the few issues are.
Naturally it isn't on Google's and JetBrains interest to talk about them.
What you need to do is analyze your situation and make a decision on whether you have significant non-UX code which will be a bear to maintain long-term. Most people do not, so maintaining a platform-agnostic layer is unrealistic.
To help with this, let's look at an older example: Microsoft. In https://channel9.msdn.com/Events/CPP/C-PP-Con-2014/024-Micro... they describe how they are using C/C++ as the basis for their Office applications. Why? They have a load of legacy code that needs to be reused and they needed the exact behavior on all platforms. This code runs on the client and can't be moved off to the server-side which would be the preference when feasible.
Since iOS and Android can consume C/C++ code it makes sense to maintain as much of the application in C/C++ as possible for sharing while creating native apps which are as thin as reasonable. They can do iOS and Android specific things where it brings value, but centrally, they can decide if it should be something available across all platforms, or even architect it partly in C/C++, and partly in the native API for the mobile platform. This allows them to bring it quickly from Android to iOS in a future release.
Microsoft can easily afford to do everything described above. If we look at Netflix they are in a similar position, but don't have as much legacy C/C++ code. Kotlin MP feels like a nice alternative given Netflix's situation.
Do you feel like you are in a similar situation? Few developers are in this situation, but it's attractive to architecture astronauts. While somewhat unpleasant, duplicating functionality in native iOS and Android code bases might be the right thing for your apps now. If not, there are in-between options such as moving to Flutter for all your UI code and Dart becomes your main client language. You have to be able to live with any compromises (I'm more familiar with Xamarin which is an aging, but similar trade-off.) That seems like one of the better balances today with good momentum behind the platform and strong implementation.
It sounds like this tool is used internally. Given that, was it not possible to issue tablets of a single platform to users instead? Or are users using their personal devices?
For starters good luck turning on / off features, handling UI state, and tying in the business logic to that state. And good luck mangling dependencies and build times with a new added Kotlin dependency :-)
I assume this has nothing todo with video as its on a phone, but does anyone in the TV business know what this app actually does or could do - time management/planning/todo/contacts/legal/safety guidelines repository etc?
That's how I wrote multiplatform code 7 years ago, using hidden webviews in the background.
Unfortunately, Android or iOS to this day have not supported JS (or really Node/V8) as a first-class citizen. The best approach is still just to use webview, which has a performance cost, amongst other complexities.
Time will tell if Kotlin Multiplatform is light enough and has the minimum number dependencies to warrant its inclusion in mainstream, high-traffic applications.
Also Kotlin Multiplatform and Flutter are very, very different use cases.
For anyone who isn't aware:
Kotlin Multiplatform lets you share business logic. Think "make a library that can be used on iOS and Android". You still need to write the UI and platform-specific plumbing separately for each.
Flutter includes the UI and can give you apps running on both platforms (and web and desktop, too!) with 100% shared code†.
†In practice, though, you may need to get your hands dirty with platform-native code for functionality not already covered by the plugin ecosystem, though that need diminishes over time.
As a native Android dev, my arm-chair hypothesis is that currently both of those (kotlin, flutter) are targeting different developers and until either one starts including the other developers then I don't see any one winning over.
Kotlin - loved by native devs Flutter/Dart - web devs (previous experience with JS)
*I'm biased as a Native android dev but from my experience this is the case. Also Kotlin is really a great/fun(ctional)/expressive language
To be honest, I’m kinda surprised they don’t just go Cordova, one platform for all.
It's usually pretty easy to tell Cordova apps from native apps. The quality is wildly different and they tend to "feel" wrong, even to users who aren't super technical.
I would never recommend that route unless you're on a shoestring (<$10k) budget.
Even then I'd recommend Flutter first.
https://blog.jetbrains.com/kotlin/2020/07/kotlin-native-memo...
Why is this not a problem for Netflix?
[^1] https://github.com/apple/swift/blob/main/docs/Android.md
On the other hand, if you can lower Kotlin to native code, it may be more natural to interact with (of course, there are still these nonsense of Java's default UTF-16 string <-> Swift's native UTF-8 string conversion).
Interoperability between high level languages are just hard. I am skeptical of either way, but I can see how it is more appealing to go from Kotlin to Swift direction.
On the other hand, data modeling and high-level data transformation doesn't require all language features. It is possible to do such with a limited DSL that supports product / sum types, some filter / map / flatmap / zip, and boom, you now have your view models for UI rendering. As long as we've made up our mind on string UTF-16 / 8 or have some performant library to do lazy transformation on these. We can go a long way on standardize business logic cross iOS / Android platform.
Good to know it’s possible though. Just maybe with more tooling...