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

启用Compose Metrics时遇reportsDestination多值错误求助

问题描述

使用Android Studio + Kotlin开发多模块Android项目(40+模块,涵盖Java模块与Android模块),环境版本信息如下:

Kotlin Version = "1.8.20"
Hilt Version = "2.45"
Compose BOM Version = "2023.01.00"
Compose Compiler Version = "1.4.3"
com.android.library = "7.4.1"
com.android.application = "7.4.1"

为启用Compose Metrics,在根build.gradle中配置全局编译参数:

buildscript {
}// Top-level build file where you can add configuration options common to all sub-projects/modules.
plugins {
    id 'com.android.application' version '7.4.1' apply false
    id 'com.android.library' version '7.4.1' apply false
    id 'org.jetbrains.kotlin.android' version '1.8.10' apply false
    id 'com.google.dagger.hilt.android' version '2.45' apply false
}

subprojects {
    tasks.withType(org.jetbrains.kotlin.gradle.tasks.KotlinCompile).configureEach {
        kotlinOptions {
            if (project.findProperty("myapp.enableComposeCompilerReports") == "true") {
                freeCompilerArgs += [
                        "-P",
                        "plugin:androidx.compose.compiler.plugins.kotlin:reportsDestination=" +
                                project.buildDir.absolutePath + "/compose_metrics"
                ]
                freeCompilerArgs += [
                        "-P",
                        "plugin:androidx.compose.compiler.plugins.kotlin:metricsDestination=" +
                                project.buildDir.absolutePath + "/compose_metrics"
                ]
            }
        }
    }
}

执行以下编译命令时触发错误:

./gradlew assembleDebug -Pmyapp.enableComposeCompilerReports=true

错误信息:

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

Plugin "androidx.compose.compiler.plugins.kotlin" usage:
  liveLiterals <true|false>  Enable Live Literals code generation
  liveLiteralsEnabled <true|false>
                             Enable Live Literals code generation (with per-file enabled flags)
  generateFunctionKeyMetaClasses <true|false>
                             Generate function key meta classes with annotations indicating the functions and their group keys. Generally used for tooling.
  sourceInformation <true|false>
                             Include source information in generated code
  metricsDestination <path>  Save compose build metrics to this folder
  reportsDestination <path>  Save compose build reports to this folder
  intrinsicRemember <true|false>
                             Include source information in generated code
  suppressKotlinVersionCompatibilityCheck <true|false>
                             Suppress Kotlin version compatibility check
  generateDecoys <true|false>
                             Generate decoy methods in IR transform

复现条件:在根build.gradle添加id 'org.jetbrains.kotlin.kapt' version '1.8.10' apply false,并在app模块build.gradle中应用id 'org.jetbrains.kotlin.kapt',执行上述编译命令即可触发错误。

问题原因

启用kapt后,项目中会生成多组KotlinCompile任务(包括常规Kotlin编译任务、kapt关联的Kotlin编译任务),全局配置的Compose编译器参数会被重复添加到不同任务中,导致触发“不允许为插件选项设置多个值”的校验错误。

解决方案

方案1:过滤kapt相关编译任务

修改根build.gradle的subprojects配置,跳过kapt关联的KotlinCompile任务,避免重复添加参数:

subprojects {
    tasks.withType(org.jetbrains.kotlin.gradle.tasks.KotlinCompile).configureEach {
        // 跳过kapt相关的Kotlin编译任务
        if (name.contains("kapt")) return@configureEach
        
        kotlinOptions {
            if (project.findProperty("myapp.enableComposeCompilerReports") == "true") {
                freeCompilerArgs += [
                        "-P",
                        "plugin:androidx.compose.compiler.plugins.kotlin:reportsDestination=" +
                                project.buildDir.absolutePath + "/compose_metrics"
                ]
                freeCompilerArgs += [
                        "-P",
                        "plugin:androidx.compose.compiler.plugins.kotlin:metricsDestination=" +
                                project.buildDir.absolutePath + "/compose_metrics"
                ]
            }
        }
    }
}

方案2:检查参数存在性后添加

通过判断参数是否已存在,避免重复注入相同的Compose编译参数:

subprojects {
    tasks.withType(org.jetbrains.kotlin.gradle.tasks.KotlinCompile).configureEach {
        kotlinOptions {
            if (project.findProperty("myapp.enableComposeCompilerReports") == "true") {
                def reportsArg = "-P"
                def reportsValue = "plugin:androidx.compose.compiler.plugins.kotlin:reportsDestination=" + project.buildDir.absolutePath + "/compose_metrics"
                def metricsArg = "-P"
                def metricsValue = "plugin:androidx.compose.compiler.plugins.kotlin:metricsDestination=" + project.buildDir.absolutePath + "/compose_metrics"
                
                // 仅当参数不存在时才添加
                if (!freeCompilerArgs.contains(reportsArg) || !freeCompilerArgs.contains(reportsValue)) {
                    freeCompilerArgs += [reportsArg, reportsValue]
                }
                if (!freeCompilerArgs.contains(metricsArg) || !freeCompilerArgs.contains(metricsValue)) {
                    freeCompilerArgs += [metricsArg, metricsValue]
                }
            }
        }
    }
}

补充优化

当前根配置中Kotlin插件版本(1.8.10)与项目实际使用的Kotlin版本(1.8.20)不一致,建议统一版本以避免潜在兼容性问题:

// 根build.gradle的plugins块中修改
id 'org.jetbrains.kotlin.android' version '1.8.20' apply false
id 'org.jetbrains.kotlin.kapt' version '1.8.20' apply false

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.30 16:30:44