When using Kotlin Multiplatform Mobile, you come across an unusual feature - the iOS code is considered by the compiler in several versions: iosArm64 and iosX64, as well as iosArm32 (to support devices released before iPhone 5s). When developing for iOS in Swift, you don't think about these features, since it is hidden in the headers of the system libraries by the preprocessor conditions.
, ( , iosArm64 iosX64). , — iosMain. ios sourceSet, .
Commonizer Kotlin 1.4
Kotlin Multiplatform KotlinSourceSet’. , sourceSet ios , .
ios iosMain sourceSet. , Kotlin 1.4 IDE , — Arm64 X64. IDE ( ):
Kotlin 1.4 IDE — Commonizer. iosArm64Main iosX64Main iosMain klib, , IDE klib. commonizer Kotlin/Native.
build.gradle.kts:
plugins {
kotlin("multiplatform")
}
kotlin {
ios {
binaries {
framework {
baseName = "shared"
}
}
}
sourceSets {
val commonMain by getting
val iosMain by getting
}
}
commonizer gradle.properties:
kotlin.mpp.enableGranularSourceSetsMetadata=true
kotlin.native.enableDependencyPropagation=false
iOS IDE.
— iOS API iosMain. , UITextFieldDelegateProtocol :
public expect interface UITextFieldDelegateProtocol : platform.darwin.NSObjectProtocol {
}
iosX64Main/iosArm64Main :
public interface UITextFieldDelegateProtocol : platform.darwin.NSObjectProtocol {
public open fun textField(textField: platform.UIKit.UITextField, shouldChangeCharactersInRange: kotlinx.cinterop.CValue<platform.Foundation.NSRange>, replacementString: kotlin.String): kotlin.Boolean
public open fun textFieldDidBeginEditing(textField: platform.UIKit.UITextField): kotlin.Unit
...
}
- cinterop ( cocoapods Kotlin) iosMain IDE ( ).
:
sourceSet IDE
gradle-
:
cInterop IDE sourceSet
1 ( iosMain appleMain ios, macos - )
API sourceSet
sourceSet ( — )
sourceSet iOS
Kotlin Multiplatform Mobile. gradle — iosX64 iosArm64. SDKNAME — Xcode . Xcode.
:
import org.jetbrains.kotlin.gradle.plugin.mpp.KotlinNativeTarget
plugins {
kotlin("multiplatform")
}
kotlin {
val iosTarget: (String, KotlinNativeTarget.() -> Unit) -> KotlinNativeTarget =
if (System.getenv("SDK_NAME")?.startsWith("iphoneos") == true)
::iosArm64
else
::iosX64
iosTarget("ios") {
binaries {
framework {
baseName = "shared"
}
}
}
sourceSets {
val commonMain by getting
val iosMain by getting
}
}
iosMain IDE cInterop:
:
iosMain
cInterop
:
gradle
gradle iOS,
Xcode
Arm64 sourceSet depends on X64
sourceSet . iosArm64Main iosX64Main.
:
plugins {
kotlin("multiplatform")
}
kotlin {
val ios = listOf(iosX64(), iosArm64())
configure(ios) {
binaries {
framework {
baseName = "shared"
}
}
}
sourceSets {
val commonMain by getting
val iosX64Main by getting
val iosArm64Main by getting {
dependsOn(iosX64Main)
}
}
}
iosX64Main:
:
, sourceSet
API
gradle-
cInterop
:
Kotlin 1.4 cInterop ( )
symlink Arm64 to X64
, IceRock, , API cInterop, . symlink ios sourceSet:
ln -s iosX64Main iosArm64Main
gradle ios :
plugins {
kotlin("multiplatform")
}
kotlin {
val ios = listOf(iosX64(), iosArm64())
configure(ios) {
binaries {
framework {
baseName = "shared"
}
}
}
sourceSets {
val commonMain by getting
}
}
:
:
, sourceSet, symlink
API
cinterop Kotlin
:
git symlink
The IDE does not automatically notice changes to symlink files (you need to do a reload directory or just always work in the same sortset)
does not work on Windows (but for iOS it is not necessary)