TE
科技回声
首页24小时热榜最新最佳问答展示工作
GitHubTwitter
首页

科技回声

基于 Next.js 构建的科技新闻平台,提供全球科技新闻和讨论内容。

GitHubTwitter

首页

首页最新最佳问答展示工作

资源链接

HackerNews API原版 HackerNewsNext.js

© 2025 科技回声. 版权所有。

Ask HN: Best language to share code between an Android and iOS app?

116 点作者 mevorah超过 5 年前
Hello app developers of HN!<p>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&#x2F;Swift to Rust. Some core logic has also seeped into our bridging code resulting in some duplication (exactly what we were trying to avoid).<p>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)?

40 条评论

toast0超过 5 年前
Since no one else has said it, I&#x27;ll be the voice of reason.<p>Share your design specs only, and let each platform team write the implementation in the language that they think best.<p>Yes, you&#x27;ll have two teams spending time on doing the same thing. However, you&#x27;ll get important benefits.<p>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.<p>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&#x27;t obvious. Meeting on a regular basis to discuss things that worked&#x2F;didn&#x27;t on the various platforms will help them all.
评论 #21372725 未加载
评论 #21372976 未加载
评论 #21372545 未加载
评论 #21372852 未加载
评论 #21372675 未加载
评论 #21373186 未加载
pjmlp超过 5 年前
C++.<p>It is part of both SDKs, you have direct support for mixed mode debugging on the IDEs.<p>No need of extra tooling.<p>On Android JNI is always going to be a pain, but at least with C++ Studio gives you an hand on accessing JNI APIs.
评论 #21372839 未加载
评论 #21368377 未加载
评论 #21368360 未加载
评论 #21375135 未加载
skinnyasianboi超过 5 年前
I have great success using Kotlin Multiplatform to share code between iOS and Android. Have a look at my Android Jetpack Compose and iOS SwiftUI project sharing the common logic for a game <a href="https:&#x2F;&#x2F;github.com&#x2F;SimonSchubert&#x2F;Braincup" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;SimonSchubert&#x2F;Braincup</a>
评论 #21368754 未加载
me551ah超过 5 年前
Just use JavaScript.<p>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.
评论 #21368941 未加载
评论 #21368230 未加载
kerryritter超过 5 年前
I&#x27;m surprised no one has pointed out the value of Ionic Framework (<a href="https:&#x2F;&#x2F;www.ionicframework.com" rel="nofollow">https:&#x2F;&#x2F;www.ionicframework.com</a>). I&#x27;m a huge, huge proponent of this. TypeScript is a great language (in my opinion), UI code-sharing is inherent, and Capacitor (<a href="https:&#x2F;&#x2F;capacitor.ionicframework.com&#x2F;" rel="nofollow">https:&#x2F;&#x2F;capacitor.ionicframework.com&#x2F;</a>) is great for all the native work you need to do. Worth noting you can do this in Angular, React, Vue, or pure JS.<p>This won&#x27;t suit your needs for processing-heavy apps (though can WebWorkers help), but will work well for many apps (<a href="https:&#x2F;&#x2F;csform.com&#x2F;top-10-apps-built-with-ionic-framework&#x2F;" rel="nofollow">https:&#x2F;&#x2F;csform.com&#x2F;top-10-apps-built-with-ionic-framework&#x2F;</a> for a &quot;top 10&quot; list for examples).<p>I noticed after submitting that you mentioned machine-learning. Any reason you can&#x27;t offload that to a web API and take the load off the client?
mightybyte超过 5 年前
So I&#x27;m sure lots of people will think that this definitely doesn&#x27;t qualify as &quot;Best language to share code...&quot;, but Haskell has stuff for getting web, desktop, iOS, and Android out of one code base. It&#x27;s called Obelisk (<a href="https:&#x2F;&#x2F;github.com&#x2F;obsidiansystems&#x2F;obelisk" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;obsidiansystems&#x2F;obelisk</a>) and it&#x27;s actually getting to be a pretty good development experience.
评论 #21368335 未加载
_31超过 5 年前
Good thread previously here on HN about code sharing between iOS and Android: <a href="https:&#x2F;&#x2F;news.ycombinator.com&#x2F;item?id=20695806" rel="nofollow">https:&#x2F;&#x2F;news.ycombinator.com&#x2F;item?id=20695806</a>
novocaine超过 5 年前
We&#x27;re using kotlin native, after considering rust for a bit. It&#x27;s been working great, we&#x27;ve had it in production for about six months.<p>Main advantages are<p>* kotlin is a nice language with good type support<p>* has good swift bindings out of the box<p>* also compiles to js and cpp so we can use it in our Web product as well
评论 #21372770 未加载
评论 #21373432 未加载
评论 #21372262 未加载
lewisjoe超过 5 年前
We use JavaScript for sharing code between devices(iOS &amp; Android), browser as well as the server. Surprisingly, we haven&#x27;t faced any of the problems, that Dropbox claims to have faced (<a href="https:&#x2F;&#x2F;blogs.dropbox.com&#x2F;tech&#x2F;2019&#x2F;08&#x2F;the-not-so-hidden-cost-of-sharing-code-between-ios-and-android&#x2F;" rel="nofollow">https:&#x2F;&#x2F;blogs.dropbox.com&#x2F;tech&#x2F;2019&#x2F;08&#x2F;the-not-so-hidden-cos...</a>).<p>Pros:<p>1. Sharing native APIs with JS runtime and vice versa, was well supported &amp; trivial to implement.<p>2. Easy to find skill-set.<p>3. We can run the same code in browsers as well.<p>Cons:<p>1. The app will be slightly heavier in Android (as V8 has to be bundled with the app. iOS allows using native JsCore runtime.)<p>2. The shared code runs slower (Obvious, because dynamic language).
评论 #21368408 未加载
W0lf超过 5 年前
I did one cross iOS&#x2F;Android platform project using Kotlin&#x2F;Native with its MPP plugin with reasonable success. However, this was 6 months ago and the JetBrains people surely haven‘t stopped moving forward.
JaggerJo超过 5 年前
F# with Xamarin.Android + Xamarin.Ios
评论 #21368327 未加载
评论 #21368139 未加载
nfriedly超过 5 年前
FullStory is using Rust for our shared code, however the other sides are different: Java &amp; Objective-C. It seems to be working pretty well for us so far.<p>Our requirements are probably a bit different than yours, though, because we&#x27;re shipping a tool for app developers rather than the app itself. In particular, Objective-C was chosen over Swift because we didn&#x27;t want to risk potential conflicts if our customers were using a different version of Swift and&#x2F;or have to bundle a Swift runtime for older iOS versions.
njsubedi超过 5 年前
I&#x27;m not completely sure about your requirement, but as a game developer who uses Unity everyday, I would recommend it. Core graphics and UI components are available out of the box, and sometimes even when we need an app(nothing like a game) that need to access native APIs of the platform, we use Unity. It used C# as the default language, and platform-specific core logic can be separated within the code, using #directives so it&#x27;s pretty straightforward.[edit: typo]
zwetan超过 5 年前
ActionScript 3.0<p>with the Adobe AIR runtime you will be able to publish to iOS, Android, Windows and macOS, see &quot;Adobe AIR&quot; [0]<p>ActionScript is something similar to TypeScript<p>you will have access to the whole Flash stack and more, see &quot;Building Adobe AIR Applications&quot; [1]<p>with ActionScript Native Extension (ANE) you will be able to add your native code with an easy to use AS3 interface, see &quot;Extending Adobe AIR&quot; [2] and &quot;Using native extensions for Adobe AIR&quot; [3]<p>Note, the latest Adobe AIR SDK are now maintained by Harman [4]<p>[0] <a href="https:&#x2F;&#x2F;www.adobe.com&#x2F;products&#x2F;air.html" rel="nofollow">https:&#x2F;&#x2F;www.adobe.com&#x2F;products&#x2F;air.html</a><p>[1] <a href="https:&#x2F;&#x2F;help.adobe.com&#x2F;en_US&#x2F;air&#x2F;build&#x2F;index.html" rel="nofollow">https:&#x2F;&#x2F;help.adobe.com&#x2F;en_US&#x2F;air&#x2F;build&#x2F;index.html</a><p>[2] <a href="https:&#x2F;&#x2F;www.adobe.com&#x2F;devnet&#x2F;air&#x2F;articles&#x2F;extending-air.html" rel="nofollow">https:&#x2F;&#x2F;www.adobe.com&#x2F;devnet&#x2F;air&#x2F;articles&#x2F;extending-air.html</a><p>[3] <a href="https:&#x2F;&#x2F;help.adobe.com&#x2F;en_US&#x2F;air&#x2F;build&#x2F;WS597e5dadb9cc1e0253f7d2fc1311b491071-8000.html" rel="nofollow">https:&#x2F;&#x2F;help.adobe.com&#x2F;en_US&#x2F;air&#x2F;build&#x2F;WS597e5dadb9cc1e0253f...</a><p>[4] <a href="https:&#x2F;&#x2F;airsdk.harman.com&#x2F;" rel="nofollow">https:&#x2F;&#x2F;airsdk.harman.com&#x2F;</a>
评论 #21370631 未加载
shanth超过 5 年前
Dart+flutter?
评论 #21372270 未加载
评论 #21374747 未加载
评论 #21368512 未加载
karmakaze超过 5 年前
One way would be to host a script engine and run the script language, e.g. JavaScript. Another would be to create your own grammar and simultaneously translate to Kotlin and Swift. You could even use a strict subset of either Kotlin or Swift then you&#x27;d only have to deal with one translator.
tpetry超过 5 年前
Another concept would be using Haxe Lang: You wrote shared logic in haxe which will be transpiled to c++ and java (and many more languages).
zelly超过 5 年前
C++ is first-class in Android via NDK. You can write directly to your app&#x27;s memory from a C++ library without having go through FFI. I&#x27;m pretty sure it&#x27;s the same for iOS but through a C ABI.
评论 #21368428 未加载
masterwok超过 5 年前
Xamarin iOS and Xamarin Android using C#
stockkid超过 5 年前
In Go, there is an official, experimental project called gomobile [0] that allows one to generate a Java or Objective C binding to be used in mobile projects based on Go source code.<p>It comes with some restrictions and is still deemed experimental. But it could be a viable choice in the days to come.<p>[0] <a href="https:&#x2F;&#x2F;github.com&#x2F;golang&#x2F;mobile" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;golang&#x2F;mobile</a>
评论 #21368319 未加载
arcticbull超过 5 年前
You should share design specs between client teams and allow each of them to implement them within the parameters of what&#x27;s expected on each platform. Don&#x27;t force them to have the <i>exact</i> same UI at the cost of platform-specific idioms. It hurts customers on both platforms to take that approach.<p>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.<p>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&#x2F;or web front-end (via emscripten for instance), it should be. It&#x27;s usually painful to do this, but there&#x27;s usually only a small part of your codebase that should fall under this section.
mister_hn超过 5 年前
If you really want to optimize and share code across platforms, you should really go for C++.<p>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.
jamil7超过 5 年前
I&#x27;m not an expert but you could consider sharing Swift (or Kotlin) code between platforms. Nim (<a href="https:&#x2F;&#x2F;nim-lang.org" rel="nofollow">https:&#x2F;&#x2F;nim-lang.org</a>) could also be explored.
评论 #21370814 未加载
评论 #21368419 未加载
评论 #21375932 未加载
mojuba超过 5 年前
Something just crossed my mind while reading the question, that Swift could be a good candidate. Turns out there are compatibility layers that are based on Swift:<p><a href="https:&#x2F;&#x2F;www.scade.io" rel="nofollow">https:&#x2F;&#x2F;www.scade.io</a><p><a href="https:&#x2F;&#x2F;academy.realm.io&#x2F;posts&#x2F;swift-on-android&#x2F;" rel="nofollow">https:&#x2F;&#x2F;academy.realm.io&#x2F;posts&#x2F;swift-on-android&#x2F;</a><p><a href="https:&#x2F;&#x2F;blog.readdle.com&#x2F;why-we-use-swift-for-android-db449feeacaf" rel="nofollow">https:&#x2F;&#x2F;blog.readdle.com&#x2F;why-we-use-swift-for-android-db449f...</a>
santa_boy超过 5 年前
I have a similar use case where the crux of my business is really data &#x2F; analytics centric. The user experience is pretty much limited to <i>mostly</i> information consumption with a few click events (favorites, like, dislike, delete and the occasional standard input forms).<p>I&#x27;m leaning towards using [Meteor](<a href="https:&#x2F;&#x2F;www.meteor.com&#x2F;" rel="nofollow">https:&#x2F;&#x2F;www.meteor.com&#x2F;</a>) to share code between mobile apps and web apps.<p>Using cordova shells is pretty neat for business applications. Would be interested in thoughts.
cpeterso超过 5 年前
Here&#x27;s a Mozilla blog post about rewriting Firefox&#x27;s Sync client code in Rust so the same code can be used on desktop, Android, and iOS. The FFI between the shared Rust code and C++, Java, and Swift app front ends is a little hairy but well encapsulated, generated code.<p><a href="https:&#x2F;&#x2F;hacks.mozilla.org&#x2F;2019&#x2F;04&#x2F;crossing-the-rust-ffi-frontier-with-protocol-buffers&#x2F;" rel="nofollow">https:&#x2F;&#x2F;hacks.mozilla.org&#x2F;2019&#x2F;04&#x2F;crossing-the-rust-ffi-fron...</a>
pier25超过 5 年前
I&#x27;m looking at solving a similar problem but for desktop.<p>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.
omonien超过 5 年前
I do a lot of consulting for clients who use Embarcadero&#x27;s Delphi to create native iOS and Android apps from a single source base. Delphi is a commercial product, that comes with its price tag and there is, of course, a learning curve, but once you know the processes you can create great multi-platform mobile apps, with way less effort than having one team on Xcode and the other on Android Studio.
fmxexpress超过 5 年前
RAD Studio let&#x27;s you build cross platform apps targeting Android, iOS, macOS, Windows, Linux, and HTML5 with a single codebase and single UI. Choose either Object Pascal or C++.<p>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 &quot;The (not so) hidden cost of sharing code between iOS and Android&quot; article.
shams93超过 5 年前
I use c++ rather than rust because as much as I would love to use the best language its just not so well supported on mobile compared to c&#x2F;c++. I&#x27;m not sure how well flutter ffi does using rust compared to more mainstream approaches its possible that flutter ffi is more friendly to rust than jni&#x2F;kotlin.
pcstl超过 5 年前
Kotlin can generate both JVM bytecode and iOS binaries. I&#x27;ve successfully shared code between iOS and Android projects using Kotlin before. The overhead is quite low, and, all in all, I was quite satisfied with the results.<p>However, a lot of the tooling around this is experimental, so you might want to tread carefully here.
enjoiful超过 5 年前
Surprised no one has mentioned NativeScript yet. I&#x27;ve used it, admittedly only for investigation purposes, but was super impressed.<p><a href="https:&#x2F;&#x2F;www.nativescript.org&#x2F;" rel="nofollow">https:&#x2F;&#x2F;www.nativescript.org&#x2F;</a>
rsecora超过 5 年前
I have 22 years of shipping products on time with Delphi. The latest versions do a wonderful job compiling to android, iOS, macapp or Windows.<p>Delphi is not cheap, but in my case the time to market, and tight release dates make it worth.<p>www.embarcadero.com&#x2F;products
cerberusss超过 5 年前
Have you seen this answer on Stack Overflow? <a href="https:&#x2F;&#x2F;stackoverflow.com&#x2F;a&#x2F;5234868" rel="nofollow">https:&#x2F;&#x2F;stackoverflow.com&#x2F;a&#x2F;5234868</a>
earenndil超过 5 年前
D allows you to export an obj-c api. This won&#x27;t help much on the android side, but would definitely help with talking to swift on the ios side.
gagabity超过 5 年前
There is a google tool that converts Java to Objective C, it should be easy to find.
StreamBright超过 5 年前
I would say React Native. For the things you are doing it works perfectly.
gok超过 5 年前
C.
评论 #21372867 未加载
timwaagh超过 5 年前
Xamarin (C#) and Cordova (JavaScript) are the usual suspects for such multiplatform endeavours. You should not use three languages in production if one is enough.
评论 #21373503 未加载
aaronbrethorst超过 5 年前
English