Search code examples
ioskotlincocoapodskotlin-multiplatformkotlin-multiplatform-mobile

Kotlin Multiplatform Library using Cocoapods with multiple ios targets


I have a Kotlin Multiplatform Library which supports Android, ios(arm64, simArm64). I recently added a cocoapod dependancy of the ios target (Analytics).

When using only one ios target(for example arm64) then the library compiles and works as expected.

When I add the additional ios target simArm64, i get the error that the compiler: Unresolved reference: cocoapods

My guess this is because .dependsOn doesnt play nicely with cocoapods, has anybody come across this issue and how they got around it?

Here is an example of my build gradle.

    iosSimulatorArm64 {
        binaries.framework {
            baseName = frameworkName
            xcf.add(this)
        }
    }
    iosArm64("ios") {
        binaries.framework {
            baseName = frameworkName
            xcf.add(this)
        }
    }
    cocoapods {
        ios.deploymentTarget = "10.0"
        summary = "Some description for a Kotlin/Native module"
        homepage = "Link to a Kotlin/Native module homepage"

        pod("Analytics") {
            version = "~> 4.1.6"
            moduleName = "Segment"
            source = git("https://github.com/Reedyuk/analytics-ios.git") {
                branch = "master"
            }
        }
    }
....
        val iosMain by getting {
            dependencies {
                implementation("io.ktor:ktor-client-ios:$ktor_version")
            }
        }
        val iosSimulatorArm64Main by getting
        iosSimulatorArm64Main.dependsOn(iosMain)
        val iosTest by getting
        val iosSimulatorArm64Test by getting
        iosSimulatorArm64Test.dependsOn(iosTest)


Solution

  • After further inspection, i managed to find a solution.

    In the cocoapods extension there is a 'framework' field, in addition adding

    kotlin.mpp.enableCInteropCommonization=true

    to the gradle.properties file

    You will have something that looks like this:

    kotlin {
        android {
            publishAllLibraryVariants()
        }
    
        val xcf = XCFramework()
        iosSimulatorArm64 {
            binaries.framework {
                baseName = frameworkName
                xcf.add(this)
            }
        }
        iosArm64("ios") {
            binaries.framework {
                baseName = frameworkName
                xcf.add(this)
            }
        }
    
        sourceSets {
            val commonMain by getting
            val commonTest by getting
            val androidMain by getting {
                dependencies {
                    api("com.segment.analytics.android:analytics:4.10.3")
                }
            }
            val androidTest by getting
    
            val iosMain by getting {}
            val iosSimulatorArm64Main by getting
            iosSimulatorArm64Main.dependsOn(iosMain)
            val iosTest by getting
            val iosSimulatorArm64Test by getting
            iosSimulatorArm64Test.dependsOn(iosTest)
        }
    
    }
    
    kotlin {
        cocoapods {
            ios.deploymentTarget = "10.0"
            summary = "Some description for a Kotlin/Native module"
            homepage = "Link to a Kotlin/Native module homepage"
            framework {  }
            pod("Analytics") {
                version = "~> 4.1.6"
                moduleName = "Segment"
                source = git("https://github.com/Reedyuk/analytics-ios.git") {
                    branch = "master"
                }
            }
        }
    }