validating-compose-stability

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

Validating Compose Stability — A CI Gate On Skippability And Parameter Stability

验证 Compose 稳定性——针对可跳过性和参数稳定性的 CI 门禁

Compose stability — whether a composable is skippable/restartable and whether its parameters are stable — is invisible until a recomposition profiler catches it in production. This skill makes it a build gate: snapshot the current stability of every composable into a committed
.stability
baseline, and fail CI when a change makes anything less stable. The tool is the Compose Stability Analyzer Gradle plugin. (Authoring composables for stability —
@Immutable
/
@Stable
, the compiler config file, deferred reads — is covered in the
compose-performance-skills
repo; this skill is purely the validation/CI layer.)
Compose 稳定性——即一个 composable 是否可跳过/可重启,以及其参数是否稳定——通常在生产环境中的重组性能分析器捕获问题之前都是不可见的。此 skill 将其转化为构建门禁:把每个 composable 当前的稳定性快照写入已提交的
.stability
基线,并在任何变更导致稳定性下降时让 CI 失败。该工具是 Compose Stability Analyzer Gradle 插件。(编写稳定的 composable——
@Immutable
/
@Stable
、编译器配置文件、延迟读取——在
compose-performance-skills
仓库中介绍;本 skill 仅负责验证/CI 层。)

When to use this skill

何时使用此 skill

  • The user wants
    ./gradlew stabilityCheck
    to fail CI when a composable becomes unskippable or a parameter flips stable → unstable.
  • The user wants a committed, reviewable baseline of every composable's stability (
    .stability
    files) so changes show up in PR diffs.
  • A recomposition regression shipped because nothing flagged a
    List
    parameter or a non-
    @Immutable
    data class added to a hot composable.
  • The user mentions
    stabilityDump
    ,
    stabilityCheck
    ,
    composeStabilityAnalyzer
    ,
    @IgnoreStabilityReport
    , or "stability baseline".
  • The user wants warning-only locally and hard-fail in CI.
  • 用户希望
    ./gradlew stabilityCheck
    在某个 composable 变得不可跳过或参数由稳定变为不稳定时让 CI 失败。
  • 用户希望为每个 composable 的稳定性维护一份已提交、可审查的基线(
    .stability
    文件),使变更能够显示在 PR diff 中。
  • 某个重组回归被发布到生产环境,因为没有工具标记出热路径 composable 中新增的
    List
    参数或非
    @Immutable
    data class。
  • 用户提到
    stabilityDump
    、
    stabilityCheck
    、
    composeStabilityAnalyzer
    、
    @IgnoreStabilityReport
    或“stability baseline”。
  • 用户希望在本地仅显示警告,而在 CI 中严格失败。

When NOT to use this skill

何时不应使用此 skill

  • The user wants to fix an unstable parameter (
    @Immutable
    /
    @Stable
    ,
    kotlinx.collections.immutable
    ,
    stability_config.conf
    for external types, deferred state reads,
    derivedStateOf
    ) — that is Compose performance authoring; see the
    compose-performance-skills
    repo's stability skills. This skill only validates and gates.
  • The user wants to find which recompositions are firing at runtime (Layout Inspector recomposition counts,
    Recomposer
    metrics, composition tracing) — different tool, runtime not build time.
  • The user is doing UI behavioral or screenshot testing — see
    ../../setup/choosing-test-rule-vs-runtest/SKILL.md
    and
    ../../preview/capturing-preview-screenshots-in-ci/SKILL.md
    .
  • 用户希望修复不稳定参数(
    @Immutable
    /
    @Stable
    、针对外部类型的
    kotlinx.collections.immutable
    、
    stability_config.conf
    、延迟状态读取、
    derivedStateOf
    )——这属于 Compose 性能编写;请参阅
    compose-performance-skills
    仓库中的稳定性相关 skill。此 skill 仅负责验证和门禁控制。
  • 用户希望找出运行时发生了哪些重组(Layout Inspector 重组计数、
    Recomposer
    指标、composition tracing)——这需要另一类工具,属于运行时而非构建时分析。
  • 用户正在进行 UI 行为测试或截图测试——请参阅
    ../../setup/choosing-test-rule-vs-runtest/SKILL.md
    和
    ../../preview/capturing-preview-screenshots-in-ci/SKILL.md
    。

Prerequisites

前置条件

  • The Compose Stability Analyzer Gradle plugin applied to each Compose module (apply via the plugins block; coordinates and version from the project's catalog / the plugin docs).
  • Compose compiler metrics available — the plugin analyzes compiled output, so the module's Kotlin compilation must run before
    stabilityCheck
    /
    stabilityDump
    . In CI this means a
    needs: build
    (or running
    :module:compileDebugKotlin
    first) — running the check before compilation produces wrong results or fails.
  • A baseline committed to version control:
    <module>/stability/<module>.stability
    (generated by
    stabilityDump
    ).
  • 在每个 Compose 模块中应用 Compose Stability Analyzer Gradle 插件(通过 plugins 块应用;坐标和版本取自项目目录或插件文档)。
  • 可用的 Compose 编译器指标——该插件会分析编译产物,因此模块的 Kotlin 编译必须先于
    stabilityCheck
    /
    stabilityDump
    运行。在 CI 中,这意味着需要
    needs: build
    (或者先运行
    :module:compileDebugKotlin
    )——在编译前运行检查会得到错误结果或直接失败。
  • 一份已提交到版本控制的基线:
    <module>/stability/<module>.stability
    (由
    stabilityDump
    生成)。

Workflow

工作流

  • 1. Configure the validation block. In each Compose module's
    build.gradle.kts
    :
kotlin
composeStabilityAnalyzer {
    stabilityValidation {
        enabled.set(true)
        outputDir.set(layout.projectDirectory.dir("stability"))   // where the .stability baseline lives
        includeTests.set(false)
        ignoredPackages.set(listOf("com.example.internal"))
        ignoredClasses.set(listOf("PreviewComposables"))
        ignoredProjects.set(listOf("benchmarks", "examples"))
        failOnStabilityChange.set(true)            // build fails on stability changes; false => warning-only
        ignoreNonRegressiveChanges.set(false)      // true => only regressions count; new-stable / improvements ignored
        allowMissingBaseline.set(false)            // true => no baseline yet is not a failure (bootstrap only)
        stabilityConfigurationFiles.add(           // same format as the Compose compiler's stability config
            rootProject.layout.projectDirectory.file("stability_config.conf")
        )
    }
}
Key knobs:
failOnStabilityChange
(the gate),
ignoreNonRegressiveChanges
(report only regressions, ignore improvements and newly-added stable composables),
stabilityConfigurationFiles
(the Compose-compiler-format file declaring external types stable), and the
ignored*
lists for packages/classes/modules outside the contract.
  • 2. Generate the baseline once and commit it. With the module compiled:
bash
./gradlew :app:compileDebugKotlin
./gradlew :app:stabilityDump          # writes app/stability/app.stability
git add app/stability/app.stability
git commit -m "Add Compose stability baseline"
The
.stability
file is human-readable: per composable it lists the fully-qualified signature, skippable/restartable status, and each parameter's stability classification with the reason. Android projects get variant-specific tasks (
debugStabilityDump
,
releaseStabilityCheck
, …); multi-module projects get one
.stability
file per module —
./gradlew stabilityDump
runs them all, or target a module.
  • 3. Run the check in CI, after compilation.
    stabilityCheck
    compares the current compilation against the committed baseline and reports three change kinds:
    ~
    stability regression (a composable/parameter became less stable),
    +
    a composable added,
    -
    a composable removed. On a regression the build fails with a message naming the affected composables and how their stability changed, and tells you to run
    stabilityDump
    if the change was intentional.
yaml
undefined
  • 1. 配置验证块。 在每个 Compose 模块的
    build.gradle.kts
    中:
kotlin
composeStabilityAnalyzer {
    stabilityValidation {
        enabled.set(true)
        outputDir.set(layout.projectDirectory.dir("stability"))   // where the .stability baseline lives
        includeTests.set(false)
        ignoredPackages.set(listOf("com.example.internal"))
        ignoredClasses.set(listOf("PreviewComposables"))
        ignoredProjects.set(listOf("benchmarks", "examples"))
        failOnStabilityChange.set(true)            // build fails on stability changes; false => warning-only
        ignoreNonRegressiveChanges.set(false)      // true => only regressions count; new-stable / improvements ignored
        allowMissingBaseline.set(false)            // true => no baseline yet is not a failure (bootstrap only)
        stabilityConfigurationFiles.add(           // same format as the Compose compiler's stability config
            rootProject.layout.projectDirectory.file("stability_config.conf")
        )
    }
}
关键选项:
failOnStabilityChange
(门禁开关)、
ignoreNonRegressiveChanges
(仅报告回归,忽略改进和新增的稳定 composable)、
stabilityConfigurationFiles
(采用 Compose 编译器格式、用于将外部类型声明为稳定的文件),以及用于排除不属于契约的包、类或模块的
ignored*
列表。
  • 2. 生成一次基线并提交。 模块完成编译后:
bash
./gradlew :app:compileDebugKotlin
./gradlew :app:stabilityDump          # writes app/stability/app.stability
git add app/stability/app.stability
git commit -m "Add Compose stability baseline"
.stability
文件易于阅读:它会针对每个 composable 列出全限定签名、可跳过/可重启状态,以及每个参数的稳定性分类和原因。Android 项目会获得与构建变体对应的任务(
debugStabilityDump
、
releaseStabilityCheck
等);多模块项目每个模块会生成一个
.stability
文件——
./gradlew stabilityDump
会运行所有模块的任务,也可以指定某个模块。
  • 3. 在 CI 中编译后运行检查。
    stabilityCheck
    会将当前编译结果与已提交的基线进行比较,并报告三种变更:
    ~
    稳定性回归(composable 或参数变得更不稳定)、
    +
    新增 composable、
    -
    移除 composable。发生回归时,构建会失败,错误消息会指出受影响的 composable 及其稳定性变化,并提示如果变更是有意为之,应运行
    stabilityDump
    。
yaml
undefined

.github/workflows/ci.yml

.github/workflows/ci.yml

jobs: build: runs-on: ubuntu-latest steps: - uses: actions/checkout@v4 - uses: actions/setup-java@v4 with: { java-version: '21', distribution: 'zulu' } - run: ./gradlew :app:compileDebugKotlin
stability_check: name: Compose Stability Check runs-on: ubuntu-latest needs: build # MANDATORY — the analysis reads compiled output steps: - uses: actions/checkout@v4 - uses: actions/setup-java@v4 with: { java-version: '21', distribution: 'zulu' } - run: ./gradlew stabilityCheck

- [ ] **4. Accept an intentional stability change deliberately.** When a regression is real and accepted (a justified `List` parameter, an external type that genuinely can't be stable), update the baseline as a documented commit — never silently:

```bash
./gradlew :app:compileDebugKotlin
./gradlew :app:stabilityDump
git add app/stability/app.stability
git commit -m "Update stability baseline: PokemonList now takes List<Pokemon> — justified by [reason]"
The diff in the
.stability
file is the review artifact; the commit message is the audit trail.
  • 5. Exclude things that are not part of the contract. Annotate composables that should never be tracked —
    @Preview
    functions, internal scaffolding — with
    @IgnoreStabilityReport
    ; they drop out of both the dump and the check. For whole packages/classes/modules, use
    ignoredPackages
    /
    ignoredClasses
    /
    ignoredProjects
    .
kotlin
@IgnoreStabilityReport
@Preview
@Composable
fun UserCardPreview() { UserCard(user = User("John", 30)) }
  • 6. (Optional) strict in CI, warning-only locally. Gate hard on the CI server, stay non-blocking on a developer machine:
kotlin
composeStabilityAnalyzer {
    stabilityValidation { failOnStabilityChange.set(System.getenv("CI") == "true") }
}
Most CI platforms set
CI=true
.
jobs: build: runs-on: ubuntu-latest steps: - uses: actions/checkout@v4 - uses: actions/setup-java@v4 with: { java-version: '21', distribution: 'zulu' } - run: ./gradlew :app:compileDebugKotlin
stability_check: name: Compose Stability Check runs-on: ubuntu-latest needs: build # MANDATORY — the analysis reads compiled output steps: - uses: actions/checkout@v4 - uses: actions/setup-java@v4 with: { java-version: '21', distribution: 'zulu' } - run: ./gradlew stabilityCheck

- [ ] **4. 有意接受稳定性变更。** 当回归确实存在且团队决定接受时(例如采用有合理依据的 `List` 参数,或使用确实无法保持稳定的外部类型),应通过有文档记录的提交更新基线——绝不能静默处理:

```bash
./gradlew :app:compileDebugKotlin
./gradlew :app:stabilityDump
git add app/stability/app.stability
git commit -m "Update stability baseline: PokemonList now takes List<Pokemon> — justified by [reason]"
.stability
文件中的 diff 是供审查的产物;提交消息则是审计记录。
  • 5. 排除不属于契约的内容。 对于不应被跟踪的 composable——例如
    @Preview
    函数和内部脚手架——使用
    @IgnoreStabilityReport
    进行注解;它们将同时从 dump 和 check 中移除。对于整个包、类或模块,则使用
    ignoredPackages
    /
    ignoredClasses
    /
    ignoredProjects
    。
kotlin
@IgnoreStabilityReport
@Preview
@Composable
fun UserCardPreview() { UserCard(user = User("John", 30)) }
  • 6.(可选)在 CI 中严格检查,在本地仅警告。 在 CI 服务器上严格执行门禁,在开发者机器上保持非阻塞:
kotlin
composeStabilityAnalyzer {
    stabilityValidation { failOnStabilityChange.set(System.getenv("CI") == "true") }
}
大多数 CI 平台都会设置
CI=true
。

Patterns

模式

Pattern: running
stabilityCheck
before compilation

模式:在编译前运行
stabilityCheck

yaml
undefined
yaml
undefined

WRONG

WRONG

stability_check: runs-on: ubuntu-latest steps: - uses: actions/checkout@v4 - run: ./gradlew stabilityCheck # no compiled output yet
stability_check: runs-on: ubuntu-latest steps: - uses: actions/checkout@v4 - run: ./gradlew stabilityCheck # no compiled output yet

WRONG because: the analyzer reads the Compose compiler's output. With nothing compiled it

WRONG because: the analyzer reads the Compose compiler's output. With nothing compiled it

either fails or reports incorrect results. The check must depend on a build step.

either fails or reports incorrect results. The check must depend on a build step.


```yaml

```yaml

RIGHT

RIGHT

stability_check: runs-on: ubuntu-latest needs: build # or: - run: ./gradlew :app:compileDebugKotlin stabilityCheck steps: - uses: actions/checkout@v4 - run: ./gradlew stabilityCheck
undefined
stability_check: runs-on: ubuntu-latest needs: build # or: - run: ./gradlew :app:compileDebugKotlin stabilityCheck steps: - uses: actions/checkout@v4 - run: ./gradlew stabilityCheck
undefined

Pattern: regenerating the baseline to "make CI green"

模式:重新生成基线以“让 CI 变绿”

bash
undefined
bash
undefined

WRONG — CI failed on a stability regression, so:

WRONG — CI failed on a stability regression, so:

./gradlew stabilityDump && git commit -am "fix stability check"
./gradlew stabilityDump && git commit -am "fix stability check"

WRONG because: stabilityDump rewrites the baseline to whatever the code currently produces, so

WRONG because: stabilityDump rewrites the baseline to whatever the code currently produces, so

the check trivially passes — the regression ships, undocumented. The dump is for accepting a

the check trivially passes — the regression ships, undocumented. The dump is for accepting a

change you understand, with a commit message that says why; it is not a way to silence the gate.

change you understand, with a commit message that says why; it is not a way to silence the gate.


```bash

```bash

RIGHT — investigate the ~ entries first; only then, if the change is justified:

RIGHT — investigate the ~ entries first; only then, if the change is justified:

./gradlew :app:compileDebugKotlin :app:stabilityDump git add app/stability/app.stability git commit -m "Update stability baseline: <composable> <what changed> — justified by <reason>"
undefined
./gradlew :app:compileDebugKotlin :app:stabilityDump git add app/stability/app.stability git commit -m "Update stability baseline: <composable> <what changed> — justified by <reason>"
undefined

Pattern:
@Preview
functions polluting the baseline

模式:
@Preview
函数污染基线

kotlin
// WRONG — preview functions in the .stability baseline, churning it on every preview edit
@Preview @Composable fun ProfilePreview() { Profile(sampleUser) }
// WRONG because: previews are tooling entry points, not API. Tracking them means the baseline
// diffs every time someone tweaks a preview, drowning real regressions.
kotlin
// RIGHT — annotate them out (or ignoredClasses = listOf("...Preview...") for a naming convention)
@IgnoreStabilityReport @Preview @Composable fun ProfilePreview() { Profile(sampleUser) }
kotlin
// WRONG — preview functions in the .stability baseline, churning it on every preview edit
@Preview @Composable fun ProfilePreview() { Profile(sampleUser) }
// WRONG because: previews are tooling entry points, not API. Tracking them means the baseline
// diffs every time someone tweaks a preview, drowning real regressions.
kotlin
// RIGHT — annotate them out (or ignoredClasses = listOf("...Preview...") for a naming convention)
@IgnoreStabilityReport @Preview @Composable fun ProfilePreview() { Profile(sampleUser) }

Mandatory rules

强制规则

  • MUST commit the generated
    .stability
    baseline files to version control — they are the shared contract; an uncommitted baseline gates nothing.
  • MUST run
    stabilityCheck
    only after Kotlin compilation (CI:
    needs:
    a build job, or compile in the same Gradle invocation). The analyzer consumes compiled output.
  • MUST treat a
    stabilityDump
    baseline update as a deliberate, reviewed commit with a justification in the message — never as a way to clear a failing check.
  • MUST exclude
    @Preview
    and other non-API composables from the contract via
    @IgnoreStabilityReport
    (or
    ignoredClasses
    /
    ignoredPackages
    ) so the baseline diffs only on real changes.
  • MUST NOT set
    failOnStabilityChange = false
    (or
    allowMissingBaseline = true
    ) permanently — those are for bootstrapping; once a baseline exists, the gate must fail on regressions.
  • MUST NOT use this skill as the place to fix instability — that is Compose authoring (
    @Immutable
    /
    @Stable
    , immutable collections, the compiler stability config); see the
    compose-performance-skills
    repo.
  • PREFERRED:
    ignoreNonRegressiveChanges = true
    if the team only cares about regressions and finds
    +
    /new-stable noise distracting.
  • PREFERRED: strict in CI, warning-only locally via
    failOnStabilityChange.set(System.getenv("CI") == "true")
    .
  • 必须将生成的
    .stability
    基线文件提交到版本控制——它们是团队共享的契约;未提交的基线无法发挥任何门禁作用。
  • 必须仅在 Kotlin 编译完成后运行
    stabilityCheck
    (在 CI 中,
    needs:
    依赖构建 job,或者在同一次 Gradle 调用中先完成编译)。分析器会读取编译产物。
  • 必须将
    stabilityDump
    基线更新视为有意执行且经过审查的提交,并在提交消息中说明理由——绝不能将其作为清除失败检查的手段。
  • 必须通过
    @IgnoreStabilityReport
    (或
    ignoredClasses
    /
    ignoredPackages
    )排除
    @Preview
    和其他非 API composable,确保基线仅因真实变更而产生 diff。
  • 不得永久设置
    failOnStabilityChange = false
    (或
    allowMissingBaseline = true
    )——这些选项仅用于初始引导;一旦存在基线,门禁就必须在回归发生时失败。
  • 不得将此 skill 用作修复不稳定性的地方——那属于 Compose 编写工作(
    @Immutable
    /
    @Stable
    、不可变集合、编译器稳定性配置);请参阅
    compose-performance-skills
    仓库。
  • **推荐:**如果团队只关心回归,并且认为
    +
    /新增稳定项等噪声会造成干扰,可设置
    ignoreNonRegressiveChanges = true
    。
  • **推荐:**通过
    failOnStabilityChange.set(System.getenv("CI") == "true")
    在 CI 中严格检查,而在本地仅显示警告。

Verification

验证

  • ./gradlew stabilityDump
    produces
    <module>/stability/<module>.stability
    and it is committed (
    git status
    clean after a no-op dump).
  • ./gradlew stabilityCheck
    passes on the committed baseline and fails when a composable is deliberately made unstable (e.g. add a
    List<T>
    parameter to a tracked composable) with a message naming it and a
    ~
    entry.
  • The CI workflow's stability job has
    needs: build
    (or compiles before
    stabilityCheck
    ).
  • @Preview
    / scaffolding composables carry
    @IgnoreStabilityReport
    (or match an
    ignoredClasses
    pattern) and do not appear in the
    .stability
    file.
  • failOnStabilityChange
    is
    true
    (or
    CI
    -gated to true) and
    allowMissingBaseline
    is
    false
    once the baseline exists.
  • ./gradlew stabilityDump
    会生成
    <module>/stability/<module>.stability
    ,并且该文件已提交(执行一次无变更的 dump 后,
    git status
    保持干净)。
  • 对已提交的基线,
    ./gradlew stabilityCheck
    能够通过;当故意使某个被跟踪的 composable 变得不稳定时(例如添加
    List<T>
    参数),检查会失败,并给出包含该 composable 名称和
    ~
    条目的消息。
  • CI 工作流中的 stability job 设置了
    needs: build
    (或者在
    stabilityCheck
    前完成编译)。
  • @Preview
    或脚手架 composable 带有
    @IgnoreStabilityReport
    (或匹配
    ignoredClasses
    模式),并且不会出现在
    .stability
    文件中。
  • 一旦基线存在,
    failOnStabilityChange
    为
    true
    (或通过
    CI
    条件设为 true),且
    allowMissingBaseline
    为
    false
    。

References

参考资料

  • skydoves.github.io/compose-stability-analyzer/gradle-plugin/stability-validation/ —
    stabilityDump
    /
    stabilityCheck
    tasks, the
    composeStabilityAnalyzer { stabilityValidation { … } }
    DSL and every option,
    .stability
    baseline format,
    ~
    /
    +
    /
    -
    change types,
    @IgnoreStabilityReport
    , multi-module behavior.
  • skydoves.github.io/compose-stability-analyzer/gradle-plugin/ci-cd/ — GitHub Actions wiring, the mandatory
    needs: build
    dependency, build-failure behavior, the baseline-update workflow,
    failOnStabilityChange.set(System.getenv("CI") == "true")
    .
  • github.com/skydoves/compose-stability-analyzer — the plugin source, coordinates, and full configuration reference.
  • developer.android.com/develop/ui/compose/performance/stability — Compose stability concepts (skippable/restartable, stable parameters) the baseline is built on.
  • developer.android.com/develop/ui/compose/performance/stability/fix — fixing instability (
    @Immutable
    /
    @Stable
    , immutable collections, the compiler
    stability_configuration_path
    file) — the authoring side this gate protects.
  • Sibling skill:
    ../../preview/capturing-preview-screenshots-in-ci/SKILL.md
    — another Compose CI gate (device-rendered preview catalog); same
    needs: build
    -style ordering concerns.
  • skydoves.github.io/compose-stability-analyzer/gradle-plugin/stability-validation/ —
    stabilityDump
    /
    stabilityCheck
    任务、
    composeStabilityAnalyzer { stabilityValidation { … } }
    DSL 及其全部选项、
    .stability
    基线格式、
    ~
    /
    +
    /
    -
    变更类型、
    @IgnoreStabilityReport
    和多模块行为。
  • skydoves.github.io/compose-stability-analyzer/gradle-plugin/ci-cd/ — GitHub Actions 接线、强制要求的
    needs: build
    依赖、构建失败行为、基线更新流程,以及
    failOnStabilityChange.set(System.getenv("CI") == "true")
    。
  • github.com/skydoves/compose-stability-analyzer — 插件源代码、坐标和完整配置参考。
  • developer.android.com/develop/ui/compose/performance/stability — Compose 稳定性概念(可跳过/可重启、稳定参数),这是基线所依据的原理。
  • developer.android.com/develop/ui/compose/performance/stability/fix — 修复不稳定性的方法(
    @Immutable
    /
    @Stable
    、不可变集合、编译器
    stability_configuration_path
    文件),即此门禁所保护的编写侧。
  • 关联 skill:
    ../../preview/capturing-preview-screenshots-in-ci/SKILL.md
    — 另一个 Compose CI 门禁(设备渲染的 Preview 目录);同样存在与
    needs: build
    类似的执行顺序要求。