Options for configuring the iosMain sourceSet in Kotlin Multiplatform Mobile

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 , .





iosMain in the hierarchy (source - https://kotlinlang.org/docs/reference/mpp-share-on-platforms.html)
iosMain (source — https://kotlinlang.org/docs/reference/mpp-share-on-platforms.html)

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 ( ).





GitHub.





:





  1. sourceSet IDE





  2. gradle-





:





  1. cInterop IDE sourceSet





  2. 1 ( iosMain appleMain ios, macos - )





  3. API sourceSet





  4. 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:





GitHub.





:





  1. iosMain





  2. cInterop





:





  1. gradle





  2. gradle iOS,





  3. 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:





GitHub.





:





  1. , sourceSet





  2. API





  3. gradle-





  4. cInterop





:





  1. 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
    }
}
      
      



:





GitHub.





:





  1. , sourceSet, symlink





  2. API





  3. cinterop Kotlin





:





  1. git symlink





  2. 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)





  3. does not work on Windows (but for iOS it is not necessary)












All Articles