[null,null,["最后更新时间 (UTC):2025-08-11。"],[],[],null,["# Compose Preview Screenshot Testing\n\n| **Experimental:** Compose Preview Screenshot Testing is still in development. Its features and APIs are subject to change substantially during the alpha phase. Report any feedback and issues through the [issue tracker](https://issuetracker.google.com/issues/new?component=192708&template=840533).\n\nScreenshot testing is an effective way to verify how your UI looks to users. The\nCompose Preview Screenshot Testing tool combines the simplicity and features of\n[composable previews](/develop/ui/compose/tooling/previews) with the\nproductivity gains of running host-side screenshot tests. Compose Preview\nScreenshot Testing is designed to be as easy to use as composable previews.\n\nA screenshot test is an automated test that takes a screenshot of a piece of UI\nand then compares it against a previously approved reference image. If the\nimages don't match, the test fails and produces an HTML report to help you\ncompare and find the differences.\n\nWith the Compose Preview Screenshot Testing tool, you can:\n\n- Use `@PreviewTest` to create screenshot tests for existing or new composable previews.\n- Generate reference images from those composable previews.\n- Generate an HTML report that identifies changes to those previews after you make code changes.\n- Use `@Preview` parameters, such as `uiMode` or `fontScale`, and multi-previews to help you scale your tests.\n- Modularize your tests with the new `screenshotTest` source set.\n\n**Figure 1.** Example HTML report.\n\nRequirements\n------------\n\nTo use Compose Preview Screenshot Testing, you need the following:\n\n- Android Gradle plugin 8.5.0 or higher.\n- Kotlin 1.9.20 or higher. We recommend using Kotlin 2.0 or higher so you can use the Compose Compiler Gradle plugin.\n- JDK 23 or lower.\n- Compose enabled for your project. We recommend enabling Compose using\n the [Compose Compiler Gradle plugin](/develop/ui/compose/compiler).\n\n | **Note:** If you can't use the Compose Compiler Gradle plugin, you can enable Compose by [declaring a dependency on the Compose Compiler directly](/jetpack/androidx/releases/compose-kotlin#kts). Make sure you use `kotlinCompilerExtensionVersion` version 1.5.4 or higher.\n\nSetup\n-----\n\nTo enable the tool, follow these steps:\n\n1. Add the `com.android.compose.screenshot` plugin, version `0.0.1-alpha11` to your project.\n 1. Add the plugin to your version catalogs file: \n\n ```ini\n [versions]\n agp = \"8.11.0-alpha06\"\n kotlin = \"2.1.20\"\n screenshot = \"0.0.1-alpha11\"\n\n [plugins]\n screenshot = { id = \"com.android.compose.screenshot\", version.ref = \"screenshot\"}\n ```\n 2. In your module-level `build.gradle.kts` file, add the plugin in the `plugins {}` block: \n\n ```kotlin\n plugins {\n alias(libs.plugins.screenshot)\n }\n ```\n2. Enable the experimental property in your project's `gradle.properties` file. \n\n ```text\n android.experimental.enableScreenshotTest=true\n ```\n3. In the `android {}` block of your module-level `build.gradle.kts` file, enable the experimental flag to use the `screenshotTest` source set. \n\n ```\n android {\n experimentalProperties[\"android.experimental.enableScreenshotTest\"] = true\n }\n ```\n4. Add the [`screenshot-validation-api`](https://maven.google.com/web/index.html?q=screenshot-validation-api#com.android.tools.screenshot:screenshot-validation-api) and [`ui-tooling`](https://maven.google.com/web/index.html?q=tooling#androidx.compose.ui:ui-tooling) dependencies.\n 1. Add them to your version catalogs: \n\n ```ini\n [libraries]\n screenshot-validation-api = { group = \"com.android.tools.screenshot\", name = \"screenshot-validation-api\", version.ref = \"screenshot\"}\n androidx-ui-tooling = { group = \"androidx.compose.ui\", name = \"ui-tooling\"}\n ```\n 2. Add them to your module-level `build.gradle.kts` file: \n\n ```kotlin\n dependencies {\n screenshotTestImplementation(libs.screenshot.validation.api)\n screenshotTestImplementation(libs.androidx.ui.tooling)\n }\n ```\n\nDesignate composable previews to use for screenshot tests\n---------------------------------------------------------\n\nTo designate the composable previews you want to use for screenshot tests, mark\nthe previews with the `@PreviewTest` annotation. The previews must be located in\nthe new `screenshotTest` source set, for example\n`app/src/screenshotTest/kotlin/com/example/yourapp/ExamplePreviewScreenshotTest.kt`.\n\nYou can add more composables and/or previews, including multi-previews, in\nthis file or other files created in the same source set. \n\n package com.example.yourapp\n\n import androidx.compose.runtime.Composable\n import androidx.compose.ui.tooling.preview.Preview\n import com.android.tools.screenshot.PreviewTest\n import com.example.yourapp.ui.theme.MyApplicationTheme\n\n @PreviewTest\n @Preview(showBackground = true)\n @Composable\n fun GreetingPreview() {\n MyApplicationTheme {\n Greeting(\"Android!\")\n }\n }\n\nGenerate reference images\n-------------------------\n\nAfter you set up a test class, you need to generate reference images for each\npreview. These reference images are used to identify changes later, after you\nmake code changes. To generate reference images for your composable preview\nscreenshot tests, run the following Gradle task:\n\n- Linux and macOS: `./gradlew updateDebugScreenshotTest` (`./gradlew :{module}:update{Variant}ScreenshotTest`)\n- Windows: `gradlew updateDebugScreenshotTest` (`gradlew :{module}:update{Variant}ScreenshotTest`)\n\nAfter the task completes, find the reference images in\n`app/src/screenshotTestDebug/reference`\n(`{module}/src/screenshotTest{Variant}/reference`).\n| **Note:** The reference images are named with a concatenation of the fully-qualified name of the test function and a hash of the preview parameters, for example `com.sample.screenshottests.test1_da39a3ee_c2200e98_0.png`.\n\nGenerate a test report\n----------------------\n\nOnce the reference images exist, run the validate task to take a new screenshot\nand compare it with the reference image:\n\n- Linux and macOS: `./gradlew validateDebugScreenshotTest` (`./gradlew :{module}:validate{Variant}ScreenshotTest`)\n- Windows: `gradlew validateDebugScreenshotTest` (`gradlew :{module}:validate{Variant}ScreenshotTest`)\n\nThe verification task creates an HTML report at\n`{module}/build/reports/screenshotTest/preview/{variant}/index.html`.\n\nKnown issues\n------------\n\nYou can find the current list of known issues in the tool's\n[issue tracker component](https://issuetracker.google.com/issues?q=status:open%20componentid:1581441&s=created_time:desc). Report any other feedback and issues\nthrough the [issue tracker](https://issuetracker.google.com/issues/new?component=192708&template=840533).\n\nRelease updates\n---------------\n\n### 0.0.1-alpha11\n\nThis release introduces:\n\n- Compatibility with Android Gradle Plugin (AGP) 8.13.\n- Added support for parsing XML drawables with decimal values regardless of the host machine's locale.\n- For a host machine using JDK 24 or higher, compatible JDK (11-23) will be picked up, provided one is installed.\n\n### 0.0.1-alpha10\n\nThis release introduces:\n\n- From this version, you need to mark all of your preview functions with the\n `@PreviewTest` annotation. Previews without the annotation won't be executed.\n\n- Reference image directory is changed from\n `{module}/src/{variant}/screenshotTest/reference` to\n `{module}/src/screenshotTest{Variant}/reference`. This is to make sure those\n generated reference images won't be part of the production code, and to be\n aligned with the [directory structure](/studio/test/advanced-test-setup#create-instrumented-test-for-build-variant) of other test types.\n\n- The `{variant}PreviewScreenshotRender` task is removed. Image rendering is\n migrated into JUnit Test Engine.\n\n- The `update{Variant}ScreenshotTest` task will compare new rendering images to\n reference images before updating. It will only update images that have\n differences greater than a specified threshold. `--updateFilter` commandline\n flag was removed.\n\n### 0.0.1-alpha06\n\nThis release introduces:\n\nImage Difference Threshold: This new global threshold setting will allow you to\ngain finer control over screenshot comparisons. To configure, update your\nmodule's build.gradle.kts: \n\n android {\n testOptions {\n screenshotTests {\n imageDifferenceThreshold = 0.0001f // 0.01%\n }\n }\n }\n\nThis threshold will be applied to all screenshot tests defined in the module.\n\n- Bug Fixes: Some Compose Renderer bugs and added support for empty compose\n- Performance Enhancements: Image diffing algorithm was updated to be faster"]]