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

使用JetBrains Compose for iOS时遭遇构建失败问题求助

问题描述

我在使用JetBrains提供的iOS和Android跨平台模板添加Compose Multiplatform时,遭遇了构建失败,错误信息如下:

FAILURE: Build completed with 4 failures.

1: Task failed with an exception.
-----------
* What went wrong:
Execution failed for task ':kmm:shared:compileCommonMainKotlinMetadata'.
> A failure occurred while executing org.jetbrains.kotlin.compilerRunner.GradleCompilerRunnerWithWorkers$GradleKotlinCompilerWorkAction
   > Internal compiler error. See log for more details

* Try:
> Run with --stacktrace option to get the stack trace.
> Run with --info or --debug option to get more log output.
> Run with --scan to get full insights.
==============================================================================

2: Task failed with an exception.
-----------
* What went wrong:
Execution failed for task ':kmm:shared:linkDebugFrameworkIosArm64'.
> Compilation finished with errors

* Try:
> Run with --stacktrace option to get the stack trace.
> Run with --info or --debug option to get more log output.
> Run with --scan to get full insights.
==============================================================================

3: Task failed with an exception.
-----------
* What went wrong:
Execution failed for task ':kmm:shared:linkDebugFrameworkIosX64'.
> Compilation finished with errors

* Try:
> Run with --stacktrace option to get the stack trace.
> Run with --info or --debug option to get more log output.
> Run with --scan to get full insights.
==============================================================================

4: Task failed with an exception.
-----------
* What went wrong:
Execution failed for task ':kmm:shared:linkDebugFrameworkIosSimulatorArm64'.
> Compilation finished with errors

我已经完成了以下配置:

  1. 在仓库配置中添加了 maven("https://maven.pkg.jetbrains.space/public/p/compose/dev")
  2. 版本配置如下:
composeVersion="1.4.0"
agp="7.4.2"
kotlin="1.8.20"
kotlinCompilerExtensionVersion = "1.4.6"
  1. 在shared/build.gradle.kts的依赖中添加了Compose相关依赖:
kotlin {
  ....
  sourceSets{
    val commonMain by getting{
       dependencies {
         ...
         implementation(compose.runtime)
         implementation(compose.foundation)
         implementation(compose.material)            
         @OptIn(org.jetbrains.compose.ExperimentalComposeLibrary::class)
         implementation(compose.components.resources)
       }
       ....
    }
    ....
  }
  .....
}

想问的是:除了上述配置外,使用Compose开发iOS和Android跨平台应用还需要哪些额外配置?


解决方案

你还缺少几个关键的Compose Multiplatform配置步骤,补全后应该能解决构建失败问题:

1. 根目录build.gradle.kts中引入Compose插件

在根项目的build.gradle.kts里添加Compose Multiplatform的Gradle插件管理:

plugins {
    // 保留原有插件...
    id("org.jetbrains.compose") version "1.4.0" apply false
}

2. 在shared/build.gradle.kts中启用Compose插件

在shared模块的构建脚本顶部添加插件引用:

plugins {
    kotlin("multiplatform")
    id("com.android.library")
    id("org.jetbrains.compose") // 新增该行
}

3. 配置iOS目标的Compose框架链接

在shared/build.gradle.kts的kotlin块中,为所有iOS目标添加编译器参数,确保正确链接Compose框架:

kotlin {
    iosX64()
    iosArm64()
    iosSimulatorArm64()

    targets.withType<org.jetbrains.kotlin.gradle.plugin.mpp.KotlinNativeTarget> {
        binaries.all {
            freeCompilerArgs += "-linker-option=-framework"
            freeCompilerArgs += "-linker-option=ComposeMultiplatform"
        }
    }

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

4. 优化构建环境(可选但建议)

如果仍出现编译器内部错误,可在gradle.properties中增加JVM内存分配,避免内存不足导致的编译失败:

org.gradle.jvmargs=-Xmx4096m -XX:MaxMetaspaceSize=512m
kotlin.daemon.jvm.options=-Xmx4096m

5. 清理缓存后重新构建

完成所有配置后,执行以下命令清理缓存并重新构建:

./gradlew clean build

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.22 20:05:33