You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

添加KMMBridge插件后含依赖的KMM构建失败求助

KMMBridge发布CocoaPods时添加HTTP/JSON依赖后构建失败

我正在开发一个通过KMMBridge发布到CocoaPods的KMM项目。基础项目(仅返回字符串)能正常运行,但添加HTTP请求或JSON解析相关依赖后,构建出现问题。

按照《Get started with Kotlin Multiplatform Mobile》教程完成基础配置后,我在Gradle脚本中添加了KMMBridge插件:
id("co.touchlab.faktory.kmmbridge") version "0.3.7"

此时构建失败,报错如下:

Caused by: java.lang.IllegalArgumentException: This fat framework already has a binary for architecture `arm64` (shared for target `ios_arm64`)

项目地址:https://github.com/ionel71089/KotlinMultiplatformSandbox

以下是shared模块的build.gradle.kts文件:

val ktorVersion = "2.2.4"

version = "0.1"

plugins {
    kotlin("multiplatform")
    id("com.android.library")
    kotlin("plugin.serialization") version "1.8.20"
    kotlin("native.cocoapods")

    id("co.touchlab.faktory.kmmbridge") version "0.3.4" // breaks build
//    `maven-publish`
}

kotlin {
    android {
        compilations.all {
            kotlinOptions {
                jvmTarget = "1.8"
            }
        }
    }
    
    listOf(
        iosX64(),
        iosArm64(),
        iosSimulatorArm64()
    ).forEach {
        it.binaries.framework {
            baseName = "shared"
        }
    }

    sourceSets {
        val commonMain by getting {
            dependencies {
                implementation("org.jetbrains.kotlinx:kotlinx-datetime:0.4.0")
                implementation("io.ktor:ktor-client-core:$ktorVersion")
                implementation("io.ktor:ktor-client-content-negotiation:$ktorVersion")
                implementation("io.ktor:ktor-serialization-kotlinx-json:$ktorVersion")
            }
        }

        val commonTest by getting {
            dependencies {
                implementation(kotlin("test"))
                implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.6.4")
            }
        }

        val androidMain by getting {
            dependencies {
                implementation("io.ktor:ktor-client-android:$ktorVersion")
            }
        }
        val androidUnitTest by getting

        val iosX64Main by getting
        val iosArm64Main by getting
        val iosSimulatorArm64Main by getting

        val iosMain by creating {
            dependsOn(commonMain)
            iosX64Main.dependsOn(this)
            iosArm64Main.dependsOn(this)
            iosSimulatorArm64Main.dependsOn(this)
            dependencies {
                implementation("io.ktor:ktor-client-darwin:$ktorVersion")
            }
        }

        val iosX64Test by getting
        val iosArm64Test by getting
        val iosSimulatorArm64Test by getting

        val iosTest by creating {
            dependsOn(commonTest)
            iosX64Test.dependsOn(this)
            iosArm64Test.dependsOn(this)
            iosSimulatorArm64Test.dependsOn(this)
        }
    }

    cocoapods {
        summary = "KMMBridgeSampleKotlin"
        homepage = "https://touchlab.dev"
        ios.deploymentTarget = "13"
    }
}

android {
    namespace = "com.example.kotlinmultiplatformsandbox"
    compileSdk = 33
    defaultConfig {
        minSdk = 24
        targetSdk = 33
    }
}

/*
addGithubPackagesRepository()
kmmbridge {
    mavenPublishArtifacts()
    githubReleaseVersions()
    spm()
    cocoapods("git@github.com:ionel71089/PublicPodspecs.git")
    versionPrefix.set("0.8")
}
 */

注意:我在简易KMMBridge示例项目中添加相同依赖后,同样无法正常构建,项目地址:https://github.com/ionel71089/KMMBridgeSampleKotlin


解决办法

这个报错的原因很明确:KMMBridge默认会生成包含所有iOS架构的胖framework,而你手动给每个iOS目标都单独创建了framework,导致arm64架构(来自真机的iosArm64和模拟器的iosSimulatorArm64)被重复打包,触发了冲突。

具体修复步骤:

  1. 删掉逐个目标的framework配置
    把这段代码移除:

    listOf(
        iosX64(),
        iosArm64(),
        iosSimulatorArm64()
    ).forEach {
        it.binaries.framework {
            baseName = "shared"
        }
    }
    
  2. 改用统一的iOS framework配置
    在kotlin代码块里添加下面的配置,让Kotlin自动生成兼容所有架构的胖framework,同时适配KMMBridge的需求:

    ios {
        binaries.framework {
            baseName = "shared"
            isStatic = false // 按需设为true也可以,KMMBridge两种模式都支持
        }
    }
    
  3. 清理并重新构建
    执行./gradlew clean podInstall,或者在Android Studio里同步Gradle后重新运行构建任务。

额外提示:

  • KMMBridge会自动处理iOS架构的合并工作,完全不需要你手动为每个目标单独声明framework。
  • 如果需要自定义iOS目标列表,可以这么写:
    ios {
        targets = listOf(
            iosX64(),
            iosArm64(),
            iosSimulatorArm64()
        )
        binaries.framework {
            baseName = "shared"
        }
    }
    

内容的提问来源于stack exchange,提问作者Lescai Ionel

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.25 04:14:59