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

Kotlin多平台项目:shared.framework无法在Xcode中链接

问题排查:Kotlin多平台项目Xcode链接shared.framework失败

错误信息

A problem was found with the configuration of task ':shared:assembleDebugAppleFrameworkForXcodeIosX64' (type 'FrameworkCopy').

  - Type 'org.jetbrains.kotlin.gradle.plugin.mpp.apple.FrameworkCopy' property 'sourceFramework' specifies directory '/Documents/Development/multiplatform/shared/build/bin/iosX64/debugFramework/shared.framework' which doesn't exist.

项目配置

为实现模块化调整了shared模块结构,以下是相关配置文件:

:shared模块的build.gradle.kts

plugins {
    alias(libs.plugins.kotlinMultiplatform)
    alias(libs.plugins.androidLibrary)
}

kotlin {
    androidTarget {
        compilations.all {
            kotlinOptions {
                jvmTarget = "1.8"
            }
        }
    }

    listOf(
        iosX64(),
        iosArm64(),
        iosSimulatorArm64()
    ).forEach {
        it.binaries.framework {
            baseName = "shared"
            isStatic = true

            if (System.getenv("XCODE_VERSION_MAJOR") == "1500") {
                linkerOpts += "-ld64"
            }
            
            // Export modules to iOS
            export(project(":shared:core"))
        }
    }

    sourceSets {
        commonMain.dependencies {
            // Accessible by the native app
            api(project(":shared:core"))
        }
        commonTest.dependencies {
            implementation(libs.kotlin.test)
        }
    }
}

android {
    namespace = "com.my.project"
    compileSdk = 34
    defaultConfig {
        minSdk = 24
    }
    compileOptions {
        sourceCompatibility = JavaVersion.VERSION_1_8
        targetCompatibility = JavaVersion.VERSION_1_8
    }
}

:shared:core模块的build.gradle.kts

plugins {
    alias(libs.plugins.kotlinMultiplatform)
    alias(libs.plugins.androidLibrary)
}

kotlin {
    androidTarget {
        compilations.all {
            kotlinOptions {
                jvmTarget = "1.8"
            }
        }
    }

    listOf(
        iosX64(),
        iosArm64(),
        iosSimulatorArm64()
    ).forEach {
        it.binaries.framework {
            baseName = "shared"
            isStatic = true

            if (System.getenv("XCODE_VERSION_MAJOR") == "1500") {
                linkerOpts += "-ld64"
            }
     }

    sourceSets {
        commonMain.dependencies {
            // Add dependencies here
        }
        commonTest.dependencies {
            implementation(libs.kotlin.test)
        }
    }
}

android {
    namespace = "com.my.project"
    compileSdk = 34
    defaultConfig {
        minSdk = 24
    }
    compileOptions {
        sourceCompatibility = JavaVersion.VERSION_1_8
        targetCompatibility = JavaVersion.VERSION_1_8
    }
}

当前环境:Android Studio Iguana 2023.2.1、Xcode 15.2


问题原因及解决步骤

1. 语法错误导致构建中断

:shared:core模块的build.gradle.kts存在语法缺陷:iOS目标的binaries.framework配置块未闭合,缺少}来结束forEach的lambda表达式。这会直接导致Gradle构建失败,无法生成目标framework文件。

修复方式:
在:shared:core的binaries.framework配置末尾添加闭合符号:

listOf(
    iosX64(),
    iosArm64(),
    iosSimulatorArm64()
).forEach {
    it.binaries.framework {
        baseName = "shared"
        isStatic = true

        if (System.getenv("XCODE_VERSION_MAJOR") == "1500") {
            linkerOpts += "-ld64"
        }
    } // 新增这一行,闭合framework配置块
}

2. 子模块无需独立生成Framework

:shared:core作为子模块,不需要单独生成framework文件。主模块:shared已经通过export(project(":shared:core"))将子模块的API暴露给iOS,子模块重复生成同名framework会引发命名冲突,干扰主模块的framework生成流程。

优化方式:
移除:shared:core中生成framework的配置,仅保留平台目标声明:

kotlin {
    androidTarget {
        compilations.all {
            kotlinOptions {
                jvmTarget = "1.8"
            }
        }
    }

    // 只声明iOS目标,不生成独立framework
    listOf(
        iosX64(),
        iosArm64(),
        iosSimulatorArm64()
    )

    sourceSets {
        // ... 保留原有依赖配置
    }
}

3. 验证构建结果

修复完成后,先在Android Studio中执行:shared:assembleDebugIosX64任务,检查目标目录/Documents/Development/multiplatform/shared/build/bin/iosX64/debugFramework/下是否生成了shared.framework。确认文件存在后,再重新在Xcode中链接该framework。


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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.26 19:52:48