Ask HN: Best language to share code between an Android and iOS app?
Hello app developers of HN!
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)?
122 comments
[ 3.5 ms ] story [ 206 ms ] threadIt comes with some restrictions and is still deemed experimental. But it could be a viable choice in the days to come.
[0] https://github.com/golang/mobile
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.
[0] https://gioui.org
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
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.
Edit: Sorry for being unclear, was only referring to the updating JS on the fly and circumventing App Store part. Obviously (facepalm) ... ️
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"
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.
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".
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
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.
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.
https://developer.apple.com/library/archive/documentation/Co...
Sadly the new manuals are pretty lousy.
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
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.
I would never advise C unless the platform is lacking of a C++ compiler.
Just use extern "C" at the boundaries.
If nothing else it can be used for piracy.
Software Engineering should strive for quality and best practices.
Also, are you claiming that using C == “not striving for quality” or that it is not “best practice” to use C?
And on a side note, most corporations don’t care about “quality” or “best practice”, they care about shipping and selling software. Two decades in the industry has shown that those two things aren’t as highly correlated as you wish.
I am claiming both when using a language whose security reports from Google, Apple, Microsoft, ARM, Linux kernel just increase in size every year with the amount of memory corruption and UB induced security exploits.
Like those reports advise, usage of C should be constrained to environments where no other safer alternative is made available, or due to constraints on already deployed software.
What “safer” alternatives are better that give the performance needed, with tooling available and the cross platform requirements that the poster needs?
Also, you haven’t explained how a security vulnerability from a third party app can be “used for piracy”.
Where are all of the corporations that are being sued because of software quality and not using “best practices? I would say just the opposite, companies are being sued if anything for not meeting delivery dates.
The definition of enterprise software is basically poor quality software where the user is not the buyer and they have to work around bugs.
Consumer software is no better.
C++ still isn’t as easy to interop with other languages as C and the low level APIs built into operating systems are not going to use C++/STL and you still end up converting to standard C strings and arrays.
(It also always pads the full length with zeroes if necessary, so it's almost never what you actually want to do.)
The C standard library is full of these footguns.
I won't say C++ doesn't have its bad parts, and I certainly wouldn't say that it's impossible to avoid the bad parts of C (some would), but you probably aren't going to manage.
The typical error case with C is something like
If you used the “n” equivalents, c would have 8 bytes and every other function would know to stop 8 bytes - assuming you always pass in a size parameter.And why, when you have C++ strings that just handle all of that for you?
You could say that just means you have to be very careful whenever you pass a pointer to a string around without also passing in its length, but that's a lot of places.
I don't think it's worth staying there - especially since C++ is a first party supported language on pretty much all platforms.
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.
Flutter makes cross platform life so much easier - why do anything else?
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.
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.
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
https://www.scade.io
https://academy.realm.io/posts/swift-on-android/
https://blog.readdle.com/why-we-use-swift-for-android-db449f...
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...
[4] https://airsdk.harman.com/
I know it, few other dev know it too, and the rest well...
they will keep spewing stupid stuff about how flash is dead and ignore that wonderful tech
their loss