Kotlin Multiplatform Mobile (KMM) 是由 JetBrains 創建的跨平臺移動開發 SDK,它應用了 Kotlin 的多平臺功能,開發者能夠使用它在移動應用中共享業務邏輯,具體來說是可通過 KMM 在 iOS 和 Android 應用中使用相同的業務邏輯代碼,
KMM 現在為 Android Studio 提供了新的 KMM 插件,允許開發者在同一個 IDE 中撰寫、運行、測驗和除錯共享代碼,
因此,開發者不再需要切換 IDE 并運行 Xcode 來檢查代碼是否在兩個平臺上都能正常運行,使用新插件,可以直接在 Android Studio 中與 iOS 設備和模擬器集成,對于應用程式和測驗,都可以在 iOS 上設定斷點并除錯代碼,
同時,由于 Kotlin/Native 與 Objective-C/Swift 的互操作性、集成 CocoaPods 依賴管理器,以及具備呼叫平臺部分特有 API(expect/actual 模式)的能力,KMM 提供了與 iOS 開發的緊密集成,
開發者能夠將 KMM 與移動開發專案無縫集成,用 Kotlin 撰寫的共享代碼可以通過 Kotlin/JVM 被編譯成 JVM 位元組碼,通過 Kotlin/Native 被編譯成原生二進制檔案,因此開發者可以像使用其他常見的移動開發庫一樣使用 KMM 業務邏輯模塊,
Kotlin Multiplatform Mobile Goes Alpha
Posted on by Ekaterina Petrova
Kotlin Multiplatform Mobile (KMM) is an SDK that allows you to use the same business logic code in both iOS and Android applications. Today KMM goes Alpha, and you can start sharing business logic in your mobile apps with it right away. It includes the new KMM Plugin for Android Studio, which allows you to write, run, test, and debug shared code in the same IDE. Join such great teams as VMWare, Autodesk, and Yandex who are sharing code in their mobile apps using Kotlin. Reusing code between mobile platforms has never been so easy!
Multiplatform programming
This is how Kotlin Multiplatform works.
Common Kotlin includes the language, core libraries, and basic tools. Code written in common Kotlin works everywhere on all platforms.
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, serialization, 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 different platforms – just share it using the mechanisms Kotlin provides:
Share code among all platforms used in your project. Use it for sharing the common business logic that applies to all platforms.
Share code among some platforms included in your project but not all. Do this when you can reuse much of the code in similar platforms.
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.
//Common
expect fun randomUUID(): String
//Android
import java.util.*
actual fun randomUUID() = UUID.randomUUID().toString()
//iOS
import platform.Foundation.NSUUID
actual fun randomUUID(): String = NSUUID().UUIDString()
Use cases
Android — iOS
Sharing code between mobile platforms is one of the major Kotlin Multiplatform use cases. With Kotlin Multiplatform Mobile (KMM), you can build multiplatform mobile applications sharing code, such as business logic, connectivity, and more, between Android and iOS.
See KMM features, case studies and examples
Client — Server
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.
The Ktor framework is suitable for building asynchronous servers and clients in connected systems.
What's next?
New to Kotlin? Visit Getting started with Kotlin.
Documentation
Get started with Kotlin Multiplatform Mobile (KMM)
Create a multiplatform project
Share code on multiple platforms
Connect to platform-specific APIs
Tutorials
Creating a KMM application 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.
Creating a multiplatform Kotlin library teaches how to create a multiplatform library available for JVM, JS, and Native and which can be used from any other common code (for example, shared with Android and iOS). It also shows how to write tests which will be executed on all platforms and use an efficient implementation provided by a specific platform.
Building a full stack web app with Kotlin Multiplatform 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.
Sample projects
Kotlin Multiplatform Mobile samples
KotlinConf app
KotlinConf Spinner app
參考資料:
https://kotlinlang.org/docs/reference/multiplatform.html
https://kotlinlang.org/lp/mobile/
https://www.jetbrains.com/lp/mobilecrossplatform/
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/76439.html
標籤:其他
上一篇:Flutter安裝
