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

如何在Groovy版Gradle中启用Jetpack Compose强跳过模式?

启用Jetpack Compose Strong Skipping模式(Groovy Gradle)的问题与解决

问题背景

我尝试通过启用Strong Skipping模式优化Jetpack Compose UI性能,已知该模式的优势,但不清楚如何在Groovy版Gradle(非KTS)项目中启用。我在app/build.gradle中添加了如下配置:

android { ... }

composeCompiler {
   enableStrongSkippingMode = true
}

同步项目时出现Gradle错误:

A problem occurred evaluating project ':app'.
> Could not find method composeCompiler() for arguments [build_duzdc886dgg5uy4aejl1ily6m$_run_closure6@4fe2ec36] on project 

项目完整配置

根目录build.gradle

buildscript {
   kotlinVersion = "1.9.24"
   composeBOMVersion = "2024.06.00"
   composeCompilerVersion = "1.5.14"


   dependencies {
        classpath 'com.android.tools.build:gradle:8.5.1'
        classpath 'com.google.gms:google-services:4.4.2'
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlinVersion"
        classpath("com.google.firebase:firebase-crashlytics-gradle:3.0.2")
    }
 }

subprojects {
    tasks.withType(org.jetbrains.kotlin.gradle.tasks.KotlinCompile).configureEach {
        kotlinOptions {

            freeCompilerArgs += [
                    "-P",
                    "plugin:androidx.compose.compiler.plugins.kotlin:stabilityConfigurationPath=" +
                            project.absolutePath + "/compose_compiler_config.conf"
            ]


            if (project.findProperty("composeCompilerReports") == "true") {
                freeCompilerArgs += [
                        "-P",
                        "plugin:androidx.compose.compiler.plugins.kotlin:reportsDestination=" +
                                project.buildDir.absolutePath + "/compose_compiler"
                ]
            }
            if (project.findProperty("composeCompilerMetrics") == "true") {
                freeCompilerArgs += [
                        "-P",
                        "plugin:androidx.compose.compiler.plugins.kotlin:metricsDestination=" +
                                project.buildDir.absolutePath + "/compose_compiler"
                ]
            }
        }
    }
}

app/build.gradle

android {
    ...
    compileSdk rootProject.ext.compileSdkVersion

    buildFeatures {
        compose true
    }

   composeOptions {
        kotlinCompilerExtensionVersion = "${composeCompilerVersion}"
    }
   ...
}

composeCompiler {
    enableStrongSkippingMode = true
}

解决方案

在Groovy版Gradle中,composeCompiler配置块不能直接放在android块外部,需要通过以下两种方式配置:

方法1:使用Android块内的compose编译器扩展(推荐)

你的项目Android Gradle插件版本为8.5.1(≥8.2.0,满足要求),直接修改app/build.gradle,将Strong Skipping配置放到android块内的composeOptions中:

android {
    ...
    composeOptions {
        kotlinCompilerExtensionVersion = "${composeCompilerVersion}"
        // 添加Strong Skipping配置
        compiler {
            enableStrongSkippingMode.set(true)
        }
    }
    ...
}

方法2:通过Kotlin编译参数传递配置

如果方法1不生效,可直接在Kotlin编译参数中添加对应配置项,修改根目录build.gradle的subprojects块:

subprojects {
    tasks.withType(org.jetbrains.kotlin.gradle.tasks.KotlinCompile).configureEach {
        kotlinOptions {
            freeCompilerArgs += [
                    "-P",
                    "plugin:androidx.compose.compiler.plugins.kotlin:stabilityConfigurationPath=" +
                            project.absolutePath + "/compose_compiler_config.conf",
                    // 新增Strong Skipping启用参数
                    "-P",
                    "plugin:androidx.compose.compiler.plugins.kotlin:enableStrongSkippingMode=true"
            ]

            // 原有报告、指标配置保持不变
            if (project.findProperty("composeCompilerReports") == "true") {
                freeCompilerArgs += [
                        "-P",
                        "plugin:androidx.compose.compiler.plugins.kotlin:reportsDestination=" +
                                project.buildDir.absolutePath + "/compose_compiler"
                ]
            }
            if (project.findProperty("composeCompilerMetrics") == "true") {
                freeCompilerArgs += [
                        "-P",
                        "plugin:androidx.compose.compiler.plugins.kotlin:metricsDestination=" +
                                project.buildDir.absolutePath + "/compose_compiler"
                ]
            }
        }
    }
}

验证配置

同步Gradle后,可通过生成编译器报告确认模式是否启用:

  1. 执行命令:./gradlew assembleDebug -PcomposeCompilerReports=true
  2. 在app/build/compose_compiler目录下的报告中,可查看Strong Skipping的启用状态。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.20 08:03:11