116 points · by mevorah · 7y ago · 117 comments
My team owns an Android (Kotlin) and iOS (Swift) app. The app is responsible for processing data, submitting to ML models, and then displaying results in a UI. We chose to do a lot of that processing in a shared Rust component. What we have found is a high overhead of bridging Kotlin/Swift to Rust. Some core logic has also seeped into our bridging code resulting in some duplication (exactly what we were trying to avoid).
With that, those of you who have found yourselves in similar situations, which language would you choose to be shared between your Android and iOS app (if any)?
Share your design specs only, and let each platform team write the implementation in the language that they think best.
Yes, you'll have two teams spending time on doing the same thing. However, you'll get important benefits.
Each team will be able to implement appropriately for their platform. While iPhones and Android phones are rather similar in terms of capability, the interaction models have a lot of differences, and forcing a common component often results in non-native feeling apps. Users will be at best confused, and at worst insulted by this.
Additionally, when you have two teams working on similar problems, they can help each other. Training on this interaction with things known to be pretty much the same helps people realize they can do it with things that aren't obvious. Meeting on a regular basis to discuss things that worked/didn't on the various platforms will help them all.
There are lots of internals that could/should be shared between apps. Recently relevant examples: a customized WebRTC stack, a websocket client with extra authentication and delivery rules.
FWIW, my take on this has been to embrace Conway's law: if you have enough shared work to form a team around it, then break off a c++/whatever library and a team to maintain it. But if the hope is just to save 200LOC here and there, just bite the bullet and write it twice.
Big thanks to whoever pointed out Kotlin Native: seems interesting enough to poke around with.
Also the open source C++ ecosystem has advanced quite a bit since when they did there project. You would be able to spend much more time on the problem domain, and zero time on writing a JSON library for example.
I’d agree optimizing user experience by developing in native code might be important if you have a game or social media app where the worth of your company is directly correlated with your app install metrics.
Do you have any anecdotes or data that illustrates that users will be “confused” or “insulted” by non native apps?
> The app is responsible for processing data, submitting to ML models, and then displaying results in a UI
There is not need to handle this logic seperately. And lots of reasons not to.
Attempting to “share code” between Swift/Kotlin is folly at this point. I’m not going to close the door on Kotlin Native some day. But for now just say, “No”.
However... Don't assume that a user has an exclusive affinity with one platform at a time. Users will load the app on different phones, sometimes at the same time, so if there are any differences in the way the apps handle data you'll soon see things getting corrupted. Make sure you have lots, and lots, and LOTS of tests.
It is part of both SDKs, you have direct support for mixed mode debugging on the IDEs.
No need of extra tooling.
On Android JNI is always going to be a pain, but at least with C++ Studio gives you an hand on accessing JNI APIs.
Using Dropbox Djinni (https://github.com/dropbox/djinni) also helped with the horrible pain of JNI... although it's unmaintained these days. I guess people use SWiG now.
I don't think it's worth staying there - especially since C++ is a first party supported language on pretty much all platforms.
I would never advise C unless the platform is lacking of a C++ compiler.
Just use extern "C" at the boundaries.
I would like to integrate C++ code into a SwiftUI app but am not sure how to do it. Are there some iOS C++ APIs I'm overlooking?
How so? Objective-C++ drives a number of Apple projects, so I don't see how they're deprecating it.
I’d be very surprised if this was true. Apple’s own frameworks are all C++ under the hood, e.g. CoreAnimation, AVFoundation, AutoLayout (part of UIKit and AppKit), Metal, etc. The meat of these frameworks is in C++ with an Objective-C++ layer on top, exposing an Objective-C (and thus Swift) API to developers.
In any case it isn't as if extern "C" isn't part of the language.
My answer regards using only SDK available languages, not that you write 100%bof the app in one of them
https://kotlinlang.org/docs/reference/multiplatform.html
I've also done multi platform recently.
I've taken a contract first approach. Swagger / openapi for rest clients. Outside of that I use vertx which has bindings for almost every platform. With those two I can account for interoperability across all platforms.
Big hitches
* No ts.d defintions at this time. But: https://github.com/vojtechhabarta/typescript-generator
* Dukat for automatic typescript definition importing (beta) https://github.com/Kotlin/dukat
* I've not done LLVM iOS. But I've read of some generic issues.
They are improving dependency management to handle multi platform. I can't find a link but it was on their blog
Javascriptcore is a lightweight way to execute JavaScript on iOS and you can use V8 context or duktape on Android. This way you can also update code in your app without an app store update.
With C++ in both Android Studio and X-Code you can put break points in the same IDE.
With JS you would use the browser to debug JS code, and Android Studio and X-Code to debug the native code. That split can be annoying.
I guess if the team that supports the JS engine is sufficiently large, i guess you can get more out shared JS code. The majority of the devs should be able to focus on JS development only. And a couple of specialists deal with "JS engine issues".
Edit: Sorry for being unclear, was only referring to the updating JS on the fly and circumventing App Store part. Obviously (facepalm) ... ️
Perhaps your thinking about downloading such JS code at runtime from your servers, thus escaping Apple's control on the app, which was indeed forbidden (it is now ok as long as you don't drastically change the app)
Bundling JS in the app and running it has always been just fine.
2.5.2 "Apps should be self-contained in their bundles, and may not read or write data outside the designated container area, nor may they download, install, or execute code which introduces or changes features or functionality of the app, including other apps."
and
4.7 " your app must use WebKit and JavaScript Core to run third-party software and should not attempt to extend or expose native platform APIs to third-party software"
This won't suit your needs for processing-heavy apps (though can WebWorkers help), but will work well for many apps (https://csform.com/top-10-apps-built-with-ionic-framework/ for a "top 10" list for examples).
I noticed after submitting that you mentioned machine-learning. Any reason you can't offload that to a web API and take the load off the client?
Just use this : A language that is (at best) fringe on the platform you are using. Here's a toolset nobody has ever heard of. Now build your mission critical bit of infrastructure on this house of cards, wait for the toolset maintainers to be outpaced and under-resourced by underlying OS/platform changes and then have to rewrite the whole thing which if it doesn't kill your startup, will certainly massively impact its resources.
Go! (THIS IS A JOKE)
If you always optimize for whichever company is employing you currently, you're doing yourself a disservice and essentially "leaving money on the table" :)
A bit of nowadays tech scaled out from pet projects.
Main advantages are
* kotlin is a nice language with good type support
* has good swift bindings out of the box
* also compiles to js and cpp so we can use it in our Web product as well
Pros:
1. Sharing native APIs with JS runtime and vice versa, was well supported & trivial to implement.
2. Easy to find skill-set.
3. We can run the same code in browsers as well.
Cons:
1. The app will be slightly heavier in Android (as V8 has to be bundled with the app. iOS allows using native JsCore runtime.)
2. The shared code runs slower (Obvious, because dynamic language).
Cant you make use of Webview somehow? It has the benefit of being updated from the market if a security issue is found.
Our requirements are probably a bit different than yours, though, because we're shipping a tool for app developers rather than the app itself. In particular, Objective-C was chosen over Swift because we didn't want to risk potential conflicts if our customers were using a different version of Swift and/or have to bundle a Swift runtime for older iOS versions.
with the Adobe AIR runtime you will be able to publish to iOS, Android, Windows and macOS, see "Adobe AIR" [0]
ActionScript is something similar to TypeScript
you will have access to the whole Flash stack and more, see "Building Adobe AIR Applications" [1]
with ActionScript Native Extension (ANE) you will be able to add your native code with an easy to use AS3 interface, see "Extending Adobe AIR" [2] and "Using native extensions for Adobe AIR" [3]
Note, the latest Adobe AIR SDK are now maintained by Harman [4]
[0] https://www.adobe.com/products/air.html
[1] https://help.adobe.com/en_US/air/build/index.html
[2] https://www.adobe.com/devnet/air/articles/extending-air.html
[3] https://help.adobe.com/en_US/air/build/WS597e5dadb9cc1e0253f...
Flutter makes cross platform life so much easier - why do anything else?
It comes with some restrictions and is still deemed experimental. But it could be a viable choice in the days to come.
Shameless plug: If you like Go and immediate mode UIs in particular, Gio[0] runs the same Go code across desktop and mobile alike. No bridging or native code required.
If you like a more mature framework and don't mind Dart, I think Flutter is an attractive toolkit. I started Gio wanting to write a Flutter clone in Go before going for the immediate mode design.
Your business logic should be, to the maximum extent, implemented once on the server. Your client implementations should simply display what the server has computed, once. Caching it is fine, rendering it locally is fine for the most part, and you get a clean, well-fitted, elegant UI for it.
Any limited amounts of business logic that must be correct and truthful should be implemented as a Rust (or if you insist, a limited, well-defined subset of C++) library with C linkages and idiomatic wrappers for iOS and Android. If this can be shared with the server and/or web front-end (via emscripten for instance), it should be. It's usually painful to do this, but there's usually only a small part of your codebase that should fall under this section.
Make your core logic in C++ and write bridges for Kotlin and Swift, where only the UI logic is involved. C++ has the better ecosystem so far and the very modern standard (C++17) let you write robust code. There are also so many tools that help you catch errors and typos, plus thousands of libraries available.
As far as android support this guy is working on adding support to android via Dalvik assembly. The usual approach to get nim to work with android would be JNI but that might cause problems for example with x86 android machines if I am not mistaken. https://github.com/akavel/dali
https://academy.realm.io/posts/swift-on-android/
https://blog.readdle.com/why-we-use-swift-for-android-db449f...
I'm leaning towards using [Meteor](https://www.meteor.com/) to share code between mobile apps and web apps.
Using cordova shells is pretty neat for business applications. Would be interested in thoughts.
https://hacks.mozilla.org/2019/04/crossing-the-rust-ffi-fron...
I would like to avoid C++ and so far the best candidate seems to be Nim. It transpiles to C which then can run in pretty much any platform.
I prefer the Object Pascal side but the C++ side is also powerful because it solves some of the problems that the Dropbox team talked about in their "The (not so) hidden cost of sharing code between iOS and Android" article.
However, a lot of the tooling around this is experimental, so you might want to tread carefully here.
Delphi is not cheap, but in my case the time to market, and tight release dates make it worth.
www.embarcadero.com/products
C# has all the same problems for core logic, and is all fine and good but you can't really get a native enough interface out of it.