使用具有多个 ios 目标的 Cocoapods 的 Kotlin 多平台库

Kotlin Multiplatform Library using Cocoapods with multiple ios targets

我有一个支持 Android、ios(arm64、simArm64) 的 Kotlin 多平台库。我最近添加了 ios 目标(分析)的 cocoapod 依赖项。

当仅使用一个 ios 目标(例如 arm64)时,库会按预期编译和工作。

当我添加额外的 ios 目标 simArm64 时,我得到编译器的错误:Unresolved reference: cocoapods

我猜这是因为 .dependsOn 不能很好地与 cocoapods 配合使用,有人遇到过这个问题吗?他们是如何解决的?

这是我构建的示例 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)

经过进一步检查,我设法找到了解决方案。

在cocoapods扩展中有一个'framework'字段,另外添加

kotlin.mpp.enableCInteropCommonization=true

到 gradle.properties 文件

您将得到如下所示的内容:

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"
            }
        }
    }
}