validating-compose-stability
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseValidating 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 baseline, and fail CI when a change makes anything less stable. The tool is the Compose Stability Analyzer Gradle plugin. (Authoring composables for stability — /, the compiler config file, deferred reads — is covered in the repo; this skill is purely the validation/CI layer.)
.stability@Immutable@Stablecompose-performance-skillsCompose 稳定性——即一个 composable 是否可跳过/可重启,以及其参数是否稳定——通常在生产环境中的重组性能分析器捕获问题之前都是不可见的。此 skill 将其转化为构建门禁:把每个 composable 当前的稳定性快照写入已提交的 基线,并在任何变更导致稳定性下降时让 CI 失败。该工具是 Compose Stability Analyzer Gradle 插件。(编写稳定的 composable——/、编译器配置文件、延迟读取——在 仓库中介绍;本 skill 仅负责验证/CI 层。)
.stability@Immutable@Stablecompose-performance-skillsWhen to use this skill
何时使用此 skill
- The user wants to fail CI when a composable becomes unskippable or a parameter flips stable → unstable.
./gradlew stabilityCheck - The user wants a committed, reviewable baseline of every composable's stability (files) so changes show up in PR diffs.
.stability - A recomposition regression shipped because nothing flagged a parameter or a non-
Listdata class added to a hot composable.@Immutable - The user mentions ,
stabilityDump,stabilityCheck,composeStabilityAnalyzer, or "stability baseline".@IgnoreStabilityReport - The user wants warning-only locally and hard-fail in CI.
- 用户希望 在某个 composable 变得不可跳过或参数由稳定变为不稳定时让 CI 失败。
./gradlew stabilityCheck - 用户希望为每个 composable 的稳定性维护一份已提交、可审查的基线(文件),使变更能够显示在 PR diff 中。
.stability - 某个重组回归被发布到生产环境,因为没有工具标记出热路径 composable 中新增的 参数或非
Listdata class。@Immutable - 用户提到 、
stabilityDump、stabilityCheck、composeStabilityAnalyzer或“stability baseline”。@IgnoreStabilityReport - 用户希望在本地仅显示警告,而在 CI 中严格失败。
When NOT to use this skill
何时不应使用此 skill
- The user wants to fix an unstable parameter (/
@Immutable,@Stable,kotlinx.collections.immutablefor external types, deferred state reads,stability_config.conf) — that is Compose performance authoring; see thederivedStateOfrepo's stability skills. This skill only validates and gates.compose-performance-skills - The user wants to find which recompositions are firing at runtime (Layout Inspector recomposition counts, metrics, composition tracing) — different tool, runtime not build time.
Recomposer - The user is doing UI behavioral or screenshot testing — see and
../../setup/choosing-test-rule-vs-runtest/SKILL.md.../../preview/capturing-preview-screenshots-in-ci/SKILL.md
- 用户希望修复不稳定参数(/
@Immutable、针对外部类型的@Stable、kotlinx.collections.immutable、延迟状态读取、stability_config.conf)——这属于 Compose 性能编写;请参阅derivedStateOf仓库中的稳定性相关 skill。此 skill 仅负责验证和门禁控制。compose-performance-skills - 用户希望找出运行时发生了哪些重组(Layout Inspector 重组计数、指标、composition tracing)——这需要另一类工具,属于运行时而非构建时分析。
Recomposer - 用户正在进行 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. In CI this means astabilityDump(or runningneeds: buildfirst) — running the check before compilation produces wrong results or fails.:module:compileDebugKotlin - A baseline committed to version control: (generated by
<module>/stability/<module>.stability).stabilityDump
- 在每个 Compose 模块中应用 Compose Stability Analyzer Gradle 插件(通过 plugins 块应用;坐标和版本取自项目目录或插件文档)。
- 可用的 Compose 编译器指标——该插件会分析编译产物,因此模块的 Kotlin 编译必须先于 /
stabilityCheck运行。在 CI 中,这意味着需要stabilityDump(或者先运行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: (the gate), (report only regressions, ignore improvements and newly-added stable composables), (the Compose-compiler-format file declaring external types stable), and the lists for packages/classes/modules outside the contract.
failOnStabilityChangeignoreNonRegressiveChangesstabilityConfigurationFilesignored*- 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 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 (, , …); multi-module projects get one file per module — runs them all, or target a module.
.stabilitydebugStabilityDumpreleaseStabilityCheck.stability./gradlew stabilityDump- 3. Run the check in CI, after compilation. compares the current compilation against the committed baseline and reports three change kinds:
stabilityCheckstability 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-if the change was intentional.stabilityDump
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")
)
}
}关键选项:(门禁开关)、(仅报告回归,忽略改进和新增的稳定 composable)、(采用 Compose 编译器格式、用于将外部类型声明为稳定的文件),以及用于排除不属于契约的包、类或模块的 列表。
failOnStabilityChangeignoreNonRegressiveChangesstabilityConfigurationFilesignored*- 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".stabilitydebugStabilityDumpreleaseStabilityCheck.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 file is the review artifact; the commit message is the audit trail.
.stability- 5. Exclude things that are not part of the contract. Annotate composables that should never be tracked — functions, internal scaffolding — with
@Preview; they drop out of both the dump and the check. For whole packages/classes/modules, use@IgnoreStabilityReport/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=truejobs:
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- 5. 排除不属于契约的内容。 对于不应被跟踪的 composable——例如 函数和内部脚手架——使用
@Preview进行注解;它们将同时从 dump 和 check 中移除。对于整个包、类或模块,则使用@IgnoreStabilityReport/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=truePatterns
模式
Pattern: running stabilityCheck
before compilation
stabilityCheck模式:在编译前运行 stabilityCheck
stabilityCheckyaml
undefinedyaml
undefinedWRONG
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
```yamlRIGHT
RIGHT
stability_check:
runs-on: ubuntu-latest
needs: build # or: - run: ./gradlew :app:compileDebugKotlin stabilityCheck
steps:
- uses: actions/checkout@v4
- run: ./gradlew stabilityCheck
undefinedstability_check:
runs-on: ubuntu-latest
needs: build # or: - run: ./gradlew :app:compileDebugKotlin stabilityCheck
steps:
- uses: actions/checkout@v4
- run: ./gradlew stabilityCheck
undefinedPattern: regenerating the baseline to "make CI green"
模式:重新生成基线以“让 CI 变绿”
bash
undefinedbash
undefinedWRONG — 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
```bashRIGHT — 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>"
undefinedPattern: @Preview
functions polluting the baseline
@Preview模式:@Preview
函数污染基线
@Previewkotlin
// 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 baseline files to version control — they are the shared contract; an uncommitted baseline gates nothing.
.stability - MUST run only after Kotlin compilation (CI:
stabilityChecka build job, or compile in the same Gradle invocation). The analyzer consumes compiled output.needs: - MUST treat a baseline update as a deliberate, reviewed commit with a justification in the message — never as a way to clear a failing check.
stabilityDump - MUST exclude and other non-API composables from the contract via
@Preview(or@IgnoreStabilityReport/ignoredClasses) so the baseline diffs only on real changes.ignoredPackages - MUST NOT set (or
failOnStabilityChange = false) permanently — those are for bootstrapping; once a baseline exists, the gate must fail on regressions.allowMissingBaseline = true - MUST NOT use this skill as the place to fix instability — that is Compose authoring (/
@Immutable, immutable collections, the compiler stability config); see the@Stablerepo.compose-performance-skills - PREFERRED: if the team only cares about regressions and finds
ignoreNonRegressiveChanges = true/new-stable noise distracting.+ - PREFERRED: strict in CI, warning-only locally via .
failOnStabilityChange.set(System.getenv("CI") == "true")
- 必须将生成的 基线文件提交到版本控制——它们是团队共享的契约;未提交的基线无法发挥任何门禁作用。
.stability - 必须仅在 Kotlin 编译完成后运行 (在 CI 中,
stabilityCheck依赖构建 job,或者在同一次 Gradle 调用中先完成编译)。分析器会读取编译产物。needs: - 必须将 基线更新视为有意执行且经过审查的提交,并在提交消息中说明理由——绝不能将其作为清除失败检查的手段。
stabilityDump - 必须通过 (或
@IgnoreStabilityReport/ignoredClasses)排除ignoredPackages和其他非 API composable,确保基线仅因真实变更而产生 diff。@Preview - 不得永久设置 (或
failOnStabilityChange = false)——这些选项仅用于初始引导;一旦存在基线,门禁就必须在回归发生时失败。allowMissingBaseline = true - 不得将此 skill 用作修复不稳定性的地方——那属于 Compose 编写工作(/
@Immutable、不可变集合、编译器稳定性配置);请参阅@Stable仓库。compose-performance-skills - **推荐:**如果团队只关心回归,并且认为 /新增稳定项等噪声会造成干扰,可设置
+。ignoreNonRegressiveChanges = true - **推荐:**通过 在 CI 中严格检查,而在本地仅显示警告。
failOnStabilityChange.set(System.getenv("CI") == "true")
Verification
验证
- produces
./gradlew stabilityDumpand it is committed (<module>/stability/<module>.stabilityclean after a no-op dump).git status - passes on the committed baseline and fails when a composable is deliberately made unstable (e.g. add a
./gradlew stabilityCheckparameter to a tracked composable) with a message naming it and aList<T>entry.~ - The CI workflow's stability job has (or compiles before
needs: build).stabilityCheck - / scaffolding composables carry
@Preview(or match an@IgnoreStabilityReportpattern) and do not appear in theignoredClassesfile..stability - is
failOnStabilityChange(ortrue-gated to true) andCIisallowMissingBaselineonce the baseline exists.false
- 会生成
./gradlew stabilityDump,并且该文件已提交(执行一次无变更的 dump 后,<module>/stability/<module>.stability保持干净)。git status - 对已提交的基线,能够通过;当故意使某个被跟踪的 composable 变得不稳定时(例如添加
./gradlew stabilityCheck参数),检查会失败,并给出包含该 composable 名称和List<T>条目的消息。~ - CI 工作流中的 stability job 设置了 (或者在
needs: build前完成编译)。stabilityCheck - 或脚手架 composable 带有
@Preview(或匹配@IgnoreStabilityReport模式),并且不会出现在ignoredClasses文件中。.stability - 一旦基线存在,为
failOnStabilityChange(或通过true条件设为 true),且CI为allowMissingBaseline。false
References
参考资料
- skydoves.github.io/compose-stability-analyzer/gradle-plugin/stability-validation/ — /
stabilityDumptasks, thestabilityCheckDSL and every option,composeStabilityAnalyzer { stabilityValidation { … } }baseline format,.stability/~/+change types,-, multi-module behavior.@IgnoreStabilityReport - skydoves.github.io/compose-stability-analyzer/gradle-plugin/ci-cd/ — GitHub Actions wiring, the mandatory dependency, build-failure behavior, the baseline-update workflow,
needs: build.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, immutable collections, the compiler@Stablefile) — the authoring side this gate protects.stability_configuration_path - Sibling skill: — another Compose CI gate (device-rendered preview catalog); same
../../preview/capturing-preview-screenshots-in-ci/SKILL.md-style ordering concerns.needs: build
- skydoves.github.io/compose-stability-analyzer/gradle-plugin/stability-validation/ — /
stabilityDump任务、stabilityCheckDSL 及其全部选项、composeStabilityAnalyzer { stabilityValidation { … } }基线格式、.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:— 另一个 Compose CI 门禁(设备渲染的 Preview 目录);同样存在与
../../preview/capturing-preview-screenshots-in-ci/SKILL.md类似的执行顺序要求。needs: build