多平台程序设计

    Support for multiplatform programming is one of Kotlin’s key benefits. It reduces time spent writing and maintaining the same code for while retaining the flexibility and benefits of native programming.

    This is how Kotlin Multiplatform works.

    • With Kotlin Multiplatform libraries, you can reuse the multiplatform logic in common and platform-specific code. Common code can rely on a set of libraries that cover everyday tasks such as HTTP, , and managing coroutines.
    • To interop with platforms, use platform-specific versions of Kotlin. Platform-specific versions of Kotlin (Kotlin/JVM, Kotlin/JS, Kotlin/Native) include extensions to the Kotlin language, and platform-specific libraries and tools.
    • Through these platforms you can access the platform native code (JVM, JS, and Native) and leverage all native capabilities.

    With Kotlin Multiplatform, spend less time on writing and maintaining the same code for – just share it using the mechanisms Kotlin provides:

    If you need to access platform-specific APIs from the shared code, use the Kotlin mechanism of expected and actual declarations.

    With this mechanism, a common source set defines an expected declaration, and platform source sets must provide the actual declaration that corresponds to the expected declaration. This works for most Kotlin declarations, such as functions, classes, interfaces, enumerations, properties, and annotations.

    See

    代码共享可以带来收益的另一个场景是互联应用,其中的逻辑可以在服务器与运行在浏览器中的客户端中复用。Kotlin 多平台也覆盖了这个场景。

    Ktor 框架适用于在互联系统中构建异步的服务器与客户端。

    New to Kotlin? Visit .

    • shows how to create a mobile application that works on Android and iOS with the help of the KMM plugin for Android Studio. Create, run, and test your first multiplatform mobile application.

    • teaches the concepts behind building an application that targets Kotlin/JVM and Kotlin/JS by building a client-server application that makes use of shared code, serialization, and other multiplatform paradigms. It also provides a brief introduction to working with Ktor both as a server- and client-side framework.