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

执行Gradle任务时,如何通过CLI传递Kotlin freeCompilerArgs生成Compose编译指标?

如何通过CLI直接为任意Jetpack Compose项目生成编译指标?

问题背景

我当前维护一个基于Jetpack Compose的Android项目,原本通过根项目的Gradle配置来生成Compose编译指标,配置代码如下:

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

执行./gradlew assembleRelease -PenableComposeReports=true即可正常生成指标,但我现在开发一款CLI工具,希望不依赖项目已有的Gradle配置,直接通过命令行为任意Compose项目生成编译指标。

我想确认两个问题:

  1. 是否可以直接通过CLI传递这些freeCompilerArgs?比如类似这样的命令:
./gradlew assembleRelease -Pkotlin.compiler.arguments="plugin:androidx.compose.compiler.plugins.kotlin:reportsDestination=<path>"
  1. 如果上述方式不可行,还有什么其他CLI可行的方法?

解决方案

方案1:直接通过CLI传递编译参数

可以直接通过CLI传递Compose编译插件的参数,但需要使用正确的Gradle属性名——Compose的指标参数属于Kotlin编译器的freeCompilerArgs,因此需要通过kotlin.options.freeCompilerArgs属性传递,而非kotlin.compiler.arguments。

正确命令格式

替换<项目根目录>为实际路径后执行:

./gradlew assembleRelease -Pkotlin.options.freeCompilerArgs="-P plugin:androidx.compose.compiler.plugins.kotlin:reportsDestination=<项目根目录>/build/compose_metrics/, -P plugin:androidx.compose.compiler.plugins.kotlin:metricsDestination=<项目根目录>/build/compose_metrics/"

如果遇到参数解析问题,也可以用数组格式传递(适配部分Gradle版本):

./gradlew assembleRelease \
-Pkotlin.options.freeCompilerArgs="['-P', 'plugin:androidx.compose.compiler.plugins.kotlin:reportsDestination=<项目根目录>/build/compose_metrics/', '-P', 'plugin:androidx.compose.compiler.plugins.kotlin:metricsDestination=<项目根目录>/build/compose_metrics/']"

方案2:临时注入Gradle初始化脚本

如果直接传递参数遇到多模块兼容、Gradle版本解析差异等问题,可以通过CLI临时注入一段Gradle脚本,自动为所有Kotlin编译任务添加指标参数,完全不修改项目原有配置。

命令格式

./gradlew assembleRelease -Dorg.gradle.project.composeMetricsDir=<项目根目录>/build/compose_metrics/ -c <(echo "
subprojects {
    tasks.withType(org.jetbrains.kotlin.gradle.tasks.KotlinCompile).configureEach {
        kotlinOptions {
            freeCompilerArgs += ['-P', \"plugin:androidx.compose.compiler.plugins.kotlin:reportsDestination=\${project.property('composeMetricsDir')}\"]
            freeCompilerArgs += ['-P', \"plugin:androidx.compose.compiler.plugins.kotlin:metricsDestination=\${project.property('composeMetricsDir')}\"]
        }
    }
}
")

原理说明

  • 通过-D传递系统属性composeMetricsDir,指定指标输出路径
  • 通过-c <(echo "...")临时加载一段初始化脚本,遍历所有子项目的Kotlin编译任务,自动添加Compose指标生成参数
  • 全程不需要修改项目本身的Gradle配置,适配任意Compose项目

注意事项

  • 确保目标项目已引入Jetpack Compose编译器插件(模块配置中包含kotlin-android插件及Compose相关依赖)
  • 输出路径需确保有读写权限,建议使用项目build目录下的子目录,避免权限问题
  • 不同Gradle版本对CLI参数的解析可能存在细微差异,建议在目标项目的Gradle版本下测试命令

内容的提问来源于stack exchange,提问作者Jaya Surya Thotapalli

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.25 10:55:11