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

Android应用启用Compose Metrics遇多值错误,求解决方案

解决Compose Metrics与Kapt/Hilt冲突的编译错误

问题场景

按照Compose官方文档配置根build.gradle启用编译Metrics后,执行命令:

./gradlew assembleRelease -PcomposeCompilerReports=true --rerun-tasks

无kapt和Hilt插件配置的模块可正常生成报告,但包含这两项配置的模块报以下错误:

e: Multiple values are not allowed for plugin option androidx.compose.compiler.plugins.kotlin:metricsDestination

且报错模块不固定,最终还会触发StackOverflow错误;移除Hilt配置后编译可完成,但应用无法正常运行,当前使用Gradle插件版本7.4.1。

问题原因

kaptGenerateStubsKotlin任务也属于KotlinCompile类型,根配置中遍历所有KotlinCompile任务时,会给该任务重复添加Compose Compiler的参数,导致插件检测到重复的metricsDestination/reportsDestination配置,触发报错。

解决步骤

1. 修改根build.gradle配置,排除kapt相关任务

更新根目录下的build.gradle,在遍历KotlinCompile任务时跳过kapt生成桩代码的任务:

subprojects {
    tasks.withType<org.jetbrains.kotlin.gradle.tasks.KotlinCompile> {
        // 排除kapt生成stubs的任务,避免重复添加compose编译参数
        if (name.contains("kaptGenerateStubs")) {
            return@withType
        }
        kotlinOptions {
            if (project.findProperty("composeCompilerReports") == "true") {
                val reportsDir = "${project.buildDir.absolutePath}/compose_reports"
                val metricsDir = "${project.buildDir.absolutePath}/compose_metrics"
                kotlinOptions.freeCompilerArgs += listOf(
                    "-P", "plugin:androidx.compose.compiler.plugins.kotlin:reportsDestination=$reportsDir",
                    "-P", "plugin:androidx.compose.compiler.plugins.kotlin:metricsDestination=$metricsDir"
                )
            }
        }
    }
}

2. 清理并重新执行编译

执行以下命令确保清理旧缓存并重新编译:

./gradlew clean assembleRelease -PcomposeCompilerReports=true --rerun-tasks

3. 残留StackOverflow错误处理

若仍出现StackOverflow错误,可尝试:

  • 升级Gradle插件到7.4.x系列的最新版本(如7.4.2),修复已知的任务递归问题
  • 检查模块间是否存在循环依赖,避免Gradle任务图出现异常

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.31 13:20:42