Kotlin 多平台

Kotlin 多平台处于 Beta 阶段。它几乎是稳定的,但是将来可能需要迁移步骤。 我们会尽最大努力来减少开发者需要做的变更。

Kotlin 多平台技术旨在简化跨平台项目的开发。 它减少了为不同平台编写与维护相同代码所花的时间, 同时保留了原生编程的灵活性与优势。

Kotlin 多平台使用场景

Android 与 iOS 应用程序

Sharing code between mobile platforms is one of the major Kotlin Multiplatform use cases. With Kotlin Multiplatform Mobile, you can build cross-platform mobile applications and share common code between Android and iOS, such as business logic, connectivity, and more.

Check out the Get started with Kotlin Multiplatform Mobile and Create a multiplatform app using Ktor and SQLDelight tutorials, where you will create applications for Android and iOS that include a module with shared code for both platforms.

全栈 web 应用程序

Another scenario when code sharing may bring benefits is a connected application where the logic can be reused on both the server and the client side running in the browser. This is covered by Kotlin Multiplatform as well.

See Build a full-stack web app with Kotlin Multiplatform tutorial, where you will create a connected application consisting of a server part, using Kotlin/JVM and a web client, using Kotlin/JS.


Kotlin Multiplatform is also useful for library authors. You can create a multiplatform library with common code and its platform-specific implementations for JVM, JS, and Native platforms. Once published, a multiplatform library can be used in other cross-platform projects as a dependency.

See the Create and publish a multiplatform library tutorial, where you will create a multiplatform library, test it, and publish it to Maven.

移动端与 web 应用程序的公共代码

One more popular case for using Kotlin Multiplatform is sharing the same code across Android, iOS, and web apps. It reduces the amount of business logic coded by frontend developers and helps implement products more efficiently, decreasing the coding and testing efforts.

See the RSS Reader sample project — a cross-platform application for iOS and Android with desktop and web clients implemented as experimental features.

Kotlin 多平台的工作原理

Kotlin 多平台

  • 公共 Kotlin 包括语言、核心库与基本工具。 用公共 Kotlin 编写的代码适用于所有平台的各个地方。
  • 借助 Kotlin 多平台库,可以在公共代码以及平台相关代码中复用多平台逻辑。 公共代码可以依赖一组涵盖日常任务的库,例如 HTTP序列化(serialization)管理协程(coroutines)
  • 如需与平台互操作,请使用平台相关的 Kotlin 版本。平台相关的 Kotlin 版本 (Kotlin/JVM、 Kotlin/JS、 Kotlin/Native)包含对 Kotlin 语言的扩展以及平台相关的库与工具。
  • 通过这些平台可以访问平台原生代码(JVM、 JS 与 Native)并利用所有原生功能。


使用 Kotlin 多平台,花更少的时间为不同平台编写并维护相同的代码 ——只需使用 Kotlin 提供的机制进行共享即可:


New to Kotlin? Take a look at Getting started with Kotlin.


浏览跨平台应用程序样例以了解 Kotlin 多平台的工作原理: