KMP&CMP Community Boosts ArkTS-Kotlin Interop with Advanced Syntax & Compile-Time Type Solidification

Kuaishou contributes to the KMP&CMP community, expanding cross-language syntax support to 78% coverage with complex generics, function expressions, abstract classes, and typealias, while introducing KSP-powered compile-time type solidification to solve runtime generic erasure in ArkTS-Kotlin interop.

HarmonyOS Developer Technology
HarmonyOS Developer Technology
HarmonyOS Developer Technology
KMP&CMP Community Boosts ArkTS-Kotlin Interop with Advanced Syntax & Compile-Time Type Solidification

In KMP (Kotlin Multiplatform) development for HarmonyOS, cross-language interaction between ArkTS and Kotlin is a core scenario. ArkTS has accumulated native base capabilities that KMP must call to avoid duplicate development, while ArkTS business code needs to invoke KMP-encapsulated cross-platform capabilities (network requests, data parsing) to ensure multi-platform logic consistency.

However, the native NAPI interaction chain is verbose: developers must sequentially load modules, obtain objects, extract methods, convert parameters, and invoke methods. The KMP community edition aims to provide a better cross-language development experience and performance. Kuaishou, as a community member, actively contributes to syntax expansion and annotation enhancement.

Syntax Completeness Improvement: 28% → 78%

The cross-language library now supports additional high-level Kotlin syntax:

Complex generics — e.g.,

@KNExportFunction fun <T : BaseViewModel> consumeElement(element: GenericElement<in T, *>)

Function expressions — return type as lambda callable from ArkTS:

@KNExportFunction fun returnKtBlock(a: Int, b: Int): (Int) -> String { return { c: Int -> val sum = a + b + c; "$sum" } }

; parameter type as lambda invokable from ArkTS:

@KNExportFunction fun invokeTsBlock(cb: (arg: Int) -> String): String { return cb(1) }

Abstract class inheritance — exported abstract classes can be subclassed in ArkTS:

@KNExportClass abstract class AbstracClassTest(val x: Int, val y: Int) { abstract fun caclate(): Double; fun sum(): Int = x + y }

typealias —

@KNExportTypeAlias typealias myLambda = (Int, (Int?) -> String?) -> String?

These extensions improve cross-language development convenience and enable the library to adapt to more real-world business code scenarios.

Annotation Capability Enhancement: Compile-Time Type Solidification

Manual low-level conversion logic is time-consuming and error-prone. Declarative annotations shift complex conversion logic to auto-generated bridge code. For example, adding @KNExportFunction to a Kotlin function enables direct invocation from ArkTS.

This contribution introduces Kuaishou's KSP (Kotlin Symbol Processing) bridge code generation capability, achieving compile-time type solidification. Two new annotations were added: @KNExportTypeAlias — supports exporting typealias definitions @KNHide — hides specific declarations from cross-language exposure

Traditional cross-language interaction suffers from generic erasure at runtime. This work solidifies Kotlin-side types at compile time so runtime carries complete type information. The core technical pipeline:

KSP (sole type window) — captures full KSType at compile time; generic parameters, nullability, and nested structures propagate layer by layer.

typeOf<T> (type information solidification) — writes KType into machine code, bypassing generic erasure, serving as the compile-time to runtime type bridge.

KNapiInvokable (runtime carrier) — TypeConverter dispatches precisely based on classifier and recursively restores generic parameters without reflection.

The article includes a link to the cross-language library usage guide:

https://atomgit.com/CPF-KMP-CMP/docs/blob/main/zh-cn/深入开发/跨语言调用用户手册.md
Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

HarmonyOSgenericsArkTSKotlin MultiplatformKSPcross-languageKMPtype-solidification
HarmonyOS Developer Technology
Written by

HarmonyOS Developer Technology

HarmonyOS developers provide key technology analysis, version updates, Codelabs practice, and event information for HarmonyOS. Welcome developers to join the HarmonyOS ecosystem and create infinite possibilities together!

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.