Junior — Middle
How is the cross-compilation process carried out in Kotlin Multiplatform?
sobes.tech AI
Answer from AI
In Kotlin Multiplatform, the cross-compilation process allows writing shared code, which is then compiled into different target platforms (JVM, JavaScript, Native).
Main steps:
- Targets are specified in build.gradle.kts, for example
jvm(),js(),ios(). - Common code is written in the shared module (
commonMain), and platform-specific code in the respective sourceSets. - During build, Gradle runs compilation for each target using the appropriate compilers:
- Kotlin/JVM compiles to JVM bytecode
- Kotlin/JS compiles to JavaScript
- Kotlin/Native compiles to native code for iOS, Linux, etc.
Thus, the same source code is transformed into different binary artifacts for each platform.
Example configuration in build.gradle.kts:
kotlin {
jvm()
js(IR) { browser() }
ios()
sourceSets {
val commonMain by getting
val jvmMain by getting
val jsMain by getting
val iosMain by getting
}
}